import 'dart:convert'; import 'dart:typed_data'; import 'package:barcode/barcode.dart'; /// Information about a pass’s 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 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 doesn’t scan. final String? altText; const PassBarcode({ required this.format, required this.message, required this.messageEncoding, required this.altText, }); factory PassBarcode.fromJson(Map 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)); }