printDivider static method
Print a divider image.
Creates and prints a visual divider (line, dots, etc.).
style: Style of the divider ('line', 'dotted', 'dashed', etc.)width: Width of the divider (0 = printer default)
Returns true if printing was successful.
Implementation
static Future<bool> printDivider({
String style = 'line',
int width = 0,
}) async {
try {
// Create a temporary file for the divider image
final tempPath = await ImageUtils.createTempFilePath(prefix: 'divider');
// Get printer width (or use default)
final printerWidth = width > 0 ? width : 576;
// Create a canvas to draw the divider
final recorder = ui.PictureRecorder();
final canvas = Canvas(recorder);
// Draw white background
canvas.drawRect(
Rect.fromLTWH(0, 0, printerWidth.toDouble(), 10),
Paint()..color = Colors.white,
);
// Draw the divider based on style
final paint = Paint()
..color = Colors.black
..strokeWidth = 1;
switch (style) {
case 'dotted':
paint.style = PaintingStyle.stroke;
for (double x = 0; x < printerWidth; x += 4) {
canvas.drawCircle(Offset(x, 5), 1, paint);
}
break;
case 'dashed':
paint.style = PaintingStyle.stroke;
double dashWidth = 8;
double dashSpace = 4;
double startX = 0;
while (startX < printerWidth) {
canvas.drawLine(
Offset(startX, 5),
Offset(startX + dashWidth, 5),
paint,
);
startX += dashWidth + dashSpace;
}
break;
case 'double':
canvas.drawLine(
Offset(0, 3),
Offset(printerWidth.toDouble(), 3),
paint,
);
canvas.drawLine(
Offset(0, 7),
Offset(printerWidth.toDouble(), 7),
paint,
);
break;
case 'line':
default:
canvas.drawLine(
Offset(0, 5),
Offset(printerWidth.toDouble(), 5),
paint,
);
break;
}
// Convert to image
final picture = recorder.endRecording();
final img = await picture.toImage(printerWidth, 10);
final byteData = await img.toByteData(format: ui.ImageByteFormat.png);
final buffer = byteData!.buffer.asUint8List();
// Save to temp file
await File(tempPath).writeAsBytes(buffer);
// Print the divider
return await printImage(tempPath, width: printerWidth);
} catch (e) {
print('Error printing divider: $e');
return false;
}
}