printStandardReceipt static method
Print a standard receipt template with common sections.
This is a high-level convenience method that builds and prints a receipt with standard formatting.
headerText: Title of the receiptbusinessInfo: Business name, address, etc.items: Line items to printsubtotal: Subtotal amounttax: Tax amounttotal: Total amountpaymentInfo: Payment method, transaction ID, etc.footerText: Thank you message, return policy, etc.logoPath: Optional logo image pathincludeBarcodeFooter: Whether to include a barcode at the bottombarcodeData: Data to encode in the barcode (receipt ID, etc.)
Returns true if printing was successful.
Implementation
static Future<bool> printStandardReceipt({
required String headerText,
required String businessInfo,
required List<Map<String, dynamic>> items,
required String subtotal,
required String tax,
required String total,
required String paymentInfo,
required String footerText,
String? logoPath,
bool includeBarcodeFooter = false,
String? barcodeData,
}) async {
// Create a list of receipt content items
List<ReceiptContent> contents = [];
// Add logo if provided
if (logoPath != null) {
contents.add(ReceiptContent.imageFromPath(
logoPath,
alignment: Alignment.center,
));
contents.add(ReceiptContent.blankLines(1));
}
// Add header
contents.add(ReceiptContent.centeredText(
headerText,
fontSize: 28,
fontWeight: FontWeight.bold,
));
// Add business info
contents.add(ReceiptContent.centeredText(
businessInfo,
fontSize: 20,
));
contents.add(ReceiptContent.blankLines(1));
// Add date and time
final now = DateTime.now();
final dateStr =
'${now.year}-${now.month.toString().padLeft(2, '0')}-${now.day.toString().padLeft(2, '0')}';
final timeStr =
'${now.hour.toString().padLeft(2, '0')}:${now.minute.toString().padLeft(2, '0')}';
contents.add(ReceiptContent.text(
'Date: $dateStr Time: $timeStr',
fontSize: 18,
));
contents.add(ReceiptContent.blankLines(1));
// Add separator
contents.add(ReceiptContent.text(
'----------------------------------------',
fontSize: 18,
));
// Add column headers
contents.add(ReceiptContent.multiColumn([
PrintColumn(
text: 'Item',
x: 0,
fontSize: 18,
fontWeight: FontWeight.bold,
),
PrintColumn(
text: 'Qty',
x: 360,
align: TextAlign.center,
fontSize: 18,
fontWeight: FontWeight.bold,
),
PrintColumn(
text: 'Price',
x: 500,
align: TextAlign.right,
fontSize: 18,
fontWeight: FontWeight.bold,
),
]));
// Add separator
contents.add(ReceiptContent.text(
'----------------------------------------',
fontSize: 18,
));
// Add items
for (final item in items) {
contents.add(ReceiptContent.multiColumn([
PrintColumn(
text: item['name'] as String,
x: 0,
fontSize: 18,
),
PrintColumn(
text: item['quantity'].toString(),
x: 360,
align: TextAlign.center,
fontSize: 18,
),
PrintColumn(
text: item['price'] as String,
x: 500,
align: TextAlign.right,
fontSize: 18,
),
]));
}
// Add separator
contents.add(ReceiptContent.text(
'----------------------------------------',
fontSize: 18,
));
// Add totals
contents.add(ReceiptContent.multiColumn([
PrintColumn(
text: 'Subtotal',
x: 0,
fontSize: 18,
),
PrintColumn(
text: subtotal,
x: 500,
align: TextAlign.right,
fontSize: 18,
),
]));
contents.add(ReceiptContent.multiColumn([
PrintColumn(
text: 'Tax',
x: 0,
fontSize: 18,
),
PrintColumn(
text: tax,
x: 500,
align: TextAlign.right,
fontSize: 18,
),
]));
contents.add(ReceiptContent.multiColumn([
PrintColumn(
text: 'TOTAL',
x: 0,
fontSize: 20,
fontWeight: FontWeight.bold,
),
PrintColumn(
text: total,
x: 500,
align: TextAlign.right,
fontSize: 20,
fontWeight: FontWeight.bold,
),
]));
// Add payment info
contents.add(ReceiptContent.blankLines(1));
contents.add(ReceiptContent.text(
paymentInfo,
fontSize: 18,
));
// Add footer
contents.add(ReceiptContent.blankLines(1));
contents.add(ReceiptContent.centeredText(
footerText,
fontSize: 18,
));
// Add barcode if requested
if (includeBarcodeFooter && barcodeData != null) {
contents.add(ReceiptContent.blankLines(1));
contents.add(ReceiptContent.barcode(
barcodeData,
type: BarcodeType.code128,
height: 80,
alignment: Alignment.center,
));
}
// Print the receipt
return await printComplexReceipt(
contents: contents,
cutPaperAfter: true,
);
}