dart_pkpass/lib/pkpass/models/barcode.dart
The one with the braid 6e7f19a764 feat: implement web service
Signed-off-by: The one with the braid <info@braid.business>
2023-11-26 19:42:29 +01:00

89 lines
2.8 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'dart:convert';
import 'dart:typed_data';
import 'package:barcode/barcode.dart';
/// Information about a passs barcode.
///
/// Use with Flutter:
///
/// ```dart
/// BarcodeWidget.fromBytes(
/// barcode: Barcode.fromType(myPassBarcode.format),
/// data: myPassBarcode.barcodeData,
/// )
/// ```
///
/// Please not the spec requires you to display the [altText] next to the
/// barcode in case available.
class PassBarcode {
/// the [Encoding] supported for [messageEncoding]. Can be expanded at runtime.
///
/// Default values are
/// - iso-8859-1: [Latin1Codec]
/// - utf-8: [Utf8Codec]
static Map<String, Encoding> supportedCodecs = {
'iso-8859-1': Latin1Codec(),
'iso-8859': Latin1Codec(),
'iso8859': Latin1Codec(),
'utf-8': Utf8Codec(),
'utf8': Utf8Codec(),
};
static const _allowedFormats = {
'PKBarcodeFormatQR': BarcodeType.QrCode,
'PKBarcodeFormatPDF417': BarcodeType.PDF417,
'PKBarcodeFormatAztec': BarcodeType.Aztec,
'PKBarcodeFormatCode128': BarcodeType.Code128,
};
/// Barcode format. For the barcode dictionary, you can use only the
/// following values: PKBarcodeFormatQR, PKBarcodeFormatPDF417, or
/// PKBarcodeFormatAztec. For dictionaries in the barcodes array, you may
/// also use PKBarcodeFormatCode128.
///
/// The spec defined keys are converted into the corresponding [BarcodeType]
/// representations.
final BarcodeType format;
/// Message or payload to be displayed as a barcode.
///
/// Do not use directly, use the encoded [barcodeData] instead.
final String message;
/// Text encoding that is used to convert the message from the string
/// representation to a data representation to render the barcode.
///
/// The value is typically iso-8859-1, but you may use another encoding that
/// is supported by your barcode scanning infrastructure.
///
/// Only supported values by this packages are:
/// - iso-8859-1
/// - utf-8
///
/// Custom codecs can be provided as [Codec] in [PassBarcode.supportedCodecs].
final Encoding messageEncoding;
/// Text displayed near the barcode. For example, a human-readable version
/// of the barcode data in case the barcode doesnt scan.
final String? altText;
const PassBarcode({
required this.format,
required this.message,
required this.messageEncoding,
required this.altText,
});
factory PassBarcode.fromJson(Map<String, Object?> json) => PassBarcode(
format: _allowedFormats[json['format']]!,
message: json['message'] as String,
messageEncoding:
supportedCodecs[(json['messageEncoding'] as String).toLowerCase()]!,
altText: json['altText'] as String?,
);
/// Correctly encoded byte list to be displayed in the [barcode].
Uint8List get barcodeData =>
Uint8List.fromList(messageEncoding.encode(message));
}