printCode128Barcode static method
Print a Code 128 barcode with custom parameters.
Creates and prints a customized Code 128 barcode, which can include additional text formatting and positioning options.
barcodeData: Content to encode in the barcodedisplayText: Text to display under the barcode (defaults to barcodeData)leftText: Text to display to the left of the barcodeheight: Barcode height in dotsdisplayValue: Whether to show the human-readable texttextSize: Size of the human-readable textleftTextWidth: Width allocated for the left textleftTextPosition: X position for the left textprinterWidth: Printer width in dots
Returns true if printing was successful.
Implementation
static Future<bool> printCode128Barcode({
required String barcodeData,
String displayText = "",
String leftText = "",
double height = 38,
bool displayValue = true,
int textSize = 24,
double leftTextWidth = 200,
double leftTextPosition = 155,
int printerWidth = 576, // Default printer width
}) async {
try {
final directory = await getTemporaryDirectory();
final tempPath =
'${directory.path}/barcode_${DateTime.now().millisecondsSinceEpoch}.png';
final recorder = PictureRecorder();
final canvas = Canvas(recorder);
double totalHeight = height + (displayValue ? textSize * 1.2 : 0);
// Create white background
canvas.drawRect(
Rect.fromLTWH(0, 0, printerWidth.toDouble(), totalHeight),
Paint()..color = Colors.white,
);
// Draw left text if provided
if (leftText.isNotEmpty) {
// Use normal font weight for softer appearance
final textPainter = TextPainter(
text: TextSpan(
text: leftText,
style: TextStyle(
color: Colors.black,
fontSize: textSize.toDouble(),
fontWeight: FontWeight.w300, // Softer appearance
fontFamily: 'Siemreap', // Use the standard font
)),
textDirection: TextDirection.ltr,
textAlign: TextAlign.left,
);
textPainter.layout(maxWidth: leftTextWidth);
textPainter.paint(canvas, Offset(leftTextPosition, 0));
}
// Generate barcode pattern
final List<int> encodedData = BarcodeGenerator.encodeCode128(barcodeData);
final List<String> patterns =
BarcodeGenerator.generateCode128Patterns(encodedData);
final String fullPattern = patterns.join();
// Use minimum bar width for barcode
final double barMinWidth = 2;
// Calculate optimal barcode dimensions
final double barcodeWidth = fullPattern.length * barMinWidth;
final startX = (printerWidth - barcodeWidth) / 2; // Center the barcode
double currentX = startX;
// Draw barcode bars with fixed width
for (int i = 0; i < fullPattern.length; i++) {
if (fullPattern[i] == '1') {
canvas.drawRect(
Rect.fromLTWH(currentX, 2, barMinWidth, height - 2),
Paint()..color = Colors.black,
);
}
currentX += barMinWidth;
}
// Draw text under barcode if requested
if (displayValue) {
// Use normal font weight for softer appearance
final textPainter = TextPainter(
text: TextSpan(
text: displayText.isEmpty ? barcodeData : displayText,
style: TextStyle(
color: Colors.black,
fontSize: textSize.toDouble(),
fontWeight: FontWeight.normal,
letterSpacing: 0.4, // Reduced spacing
),
),
textAlign: TextAlign.center,
textDirection: TextDirection.ltr,
);
textPainter.layout(maxWidth: printerWidth.toDouble());
textPainter.paint(
canvas,
Offset((printerWidth - textPainter.width) / 2, height + 2),
);
}
final picture = recorder.endRecording();
final img = await picture.toImage(
printerWidth,
totalHeight.ceil(),
);
final byteData = await img.toByteData(format: ImageByteFormat.png);
final buffer = byteData!.buffer.asUint8List();
// Save to temp file
await File(tempPath).writeAsBytes(buffer);
// Print the image
return await PrinterCore.printImage(
tempPath,
width: printerWidth,
alignment: TextFormatter.convertAlignment(Alignment.center),
);
} catch (e) {
print("Error creating barcode: $e");
return false;
}
}