printBarcodeWithCaption static method

Future<bool> printBarcodeWithCaption(
  1. String data, {
  2. required String caption,
  3. BarcodeType type = BarcodeType.code128,
  4. int height = 100,
  5. int width = 2,
  6. bool showBarcodeText = true,
})

Print a barcode with a caption.

Prints a barcode followed by a descriptive caption.

  • data: Content to encode in the barcode
  • caption: Descriptive text to print below the barcode
  • type: Barcode type (CODE128, QR, etc.)
  • height: Barcode height in dots
  • width: Barcode module width in dots
  • showBarcodeText: Whether to show the barcode data as text

Returns true if printing was successful.

Implementation

static Future<bool> printBarcodeWithCaption(
  String data, {
  required String caption,
  BarcodeType type = BarcodeType.code128,
  int height = 100,
  int width = 2,
  bool showBarcodeText = true,
}) async {
  try {
    // First print the barcode
    final barcodeResult = await printBarcode(
      data,
      type,
      height: height,
      width: width,
      alignment: Alignment.center,
      textPosition: showBarcodeText
          ? BarcodeTextPosition.below
          : BarcodeTextPosition.none,
    );

    if (!barcodeResult) {
      return false;
    }

    // Then print the caption (using imported PrinterText)
    return await PrinterText.printCenteredText(
      text: caption,
      fontSize: 20,
    );
  } catch (e) {
    print('Error printing barcode with caption: $e');
    return false;
  }
}