Open balaji101010 opened 1 year ago
Do you find any solution?
Hi, here's the solution:
String totalRow(Map<String, dynamic> order) { String total = "First text to the left"); String grandTotal = "Second text to the right"); int totalLength = total.length; int grandTotalLength = grandTotal.length; int lineLength = 46; // Adjust this value as needed int padding = lineLength - totalLength - grandTotalLength; String paddedTotal = total.padRight(totalLength + padding); String paddedGrandTotal = grandTotal.padLeft(grandTotalLength); return paddedTotal + paddedGrandTotal; }
After just add the print part: printContent.add( LineText( type: LineText.TYPE_TEXT, content: totalRow(order), align: LineText.ALIGN_LEFT, weight: 1, linefeed: 1, ) );
and done, took me long time to find this workaround but it works perfectly
We need to handle this manually. You can try following template and make any necessary changes.
Map<String, dynamic> config = {};
List<LineText> list = [];
List<Map<String, dynamic>> items = [
{"name": "Item 1 has a very long text to print. Let's see it can print into 3 lines.", "price": 10.00},
{"name": "Item 2", "price": 20.00},
{"name": "Item 3", "price": 30.00},
];
// Add store header
list.add(LineText(
type: LineText.TYPE_TEXT,
content: 'Sample Store',
weight: 2,
align: LineText.ALIGN_CENTER,
linefeed: 1,
));
list.add(LineText(
type: LineText.TYPE_TEXT,
content: 'Address Line 1',
align: LineText.ALIGN_CENTER,
linefeed: 1,
));
list.add(LineText(
type: LineText.TYPE_TEXT,
content: 'Phone: 123-456-7890',
align: LineText.ALIGN_CENTER,
linefeed: 1,
));
list.add(LineText(
type: LineText.TYPE_TEXT,
content: '--------------------------------',
align: LineText.ALIGN_CENTER,
linefeed: 1,
));
// Add customer and bill details
list.add(LineText(
type: LineText.TYPE_TEXT,
content: 'Customer: John Doe',
align: LineText.ALIGN_LEFT,
linefeed: 1,
));
list.add(LineText(
type: LineText.TYPE_TEXT,
content: 'Bill No: 123456',
align: LineText.ALIGN_LEFT,
linefeed: 1,
));
list.add(LineText(
type: LineText.TYPE_TEXT,
content: '--------------------------------',
align: LineText.ALIGN_CENTER,
linefeed: 1,
));
// Add items with right-aligned prices and wrapping names
int maxLineWidth = 32; // Adjust based on your printer's character limit per line
for (var item in items) {
String name = item['name'];
String price = item['price'].toStringAsFixed(2);
int availableWidthForName = maxLineWidth - price.length - 2;
List<String> words = name.split(' ');
List<String> nameLines = [];
String currentLine = '';
for (String word in words) {
if ((currentLine + word).length <= availableWidthForName) {
currentLine += (currentLine.isEmpty ? '' : ' ') + word;
} else {
nameLines.add(currentLine);
currentLine = word;
}
}
if (currentLine.isNotEmpty) {
nameLines.add(currentLine);
}
String firstLine = nameLines.first;
int spacesCount = availableWidthForName - firstLine.length + 2;
String spaces = ' ' * spacesCount;
list.add(LineText(
type: LineText.TYPE_TEXT,
content: '$firstLine$spaces$price',
align: LineText.ALIGN_LEFT,
linefeed: 1,
));
for (int i = 1; i < nameLines.length; i++) {
list.add(LineText(
type: LineText.TYPE_TEXT,
content: nameLines[i],
align: LineText.ALIGN_LEFT,
linefeed: 1,
));
}
}
list.add(LineText(
type: LineText.TYPE_TEXT,
content: '--------------------------------',
align: LineText.ALIGN_CENTER,
linefeed: 1,
));
// Add totals
double subTotal = items.fold(0, (sum, item) => sum + item['price']);
double discount = 5.00; // example discount
double total = subTotal - discount;
double paid = 50.00; // example paid amount
double balance = total - paid;
list.add(LineText(
type: LineText.TYPE_TEXT,
content: 'Subtotal: '.padRight(maxLineWidth - total.toStringAsFixed(2).length) + total.toStringAsFixed(2),
align: LineText.ALIGN_LEFT,
linefeed: 1,
));
list.add(LineText(
type: LineText.TYPE_TEXT,
content: 'Discount: '.padRight(maxLineWidth - discount.toStringAsFixed(2).length) + discount.toStringAsFixed(2),
align: LineText.ALIGN_LEFT,
linefeed: 1,
));
list.add(LineText(
type: LineText.TYPE_TEXT,
content: 'Total: '.padRight(maxLineWidth - total.toStringAsFixed(2).length) + total.toStringAsFixed(2),
align: LineText.ALIGN_LEFT,
weight: 2, // make total bold
linefeed: 1,
height: 50
));
list.add(LineText(
type: LineText.TYPE_TEXT,
content: 'Paid: '.padRight(maxLineWidth - paid.toStringAsFixed(2).length) + paid.toStringAsFixed(2),
align: LineText.ALIGN_LEFT,
linefeed: 1,
));
list.add(LineText(
type: LineText.TYPE_TEXT,
content: 'Balance: '.padRight(maxLineWidth - balance.toStringAsFixed(2).length) + balance.toStringAsFixed(2),
align: LineText.ALIGN_LEFT,
weight: 2, // make balance bold
linefeed: 1,
size: 20,
));
list.add(LineText(
type: LineText.TYPE_TEXT,
content: '--------------------------------',
align: LineText.ALIGN_CENTER,
linefeed: 1,
));
list.add(LineText(
type: LineText.TYPE_TEXT,
content: 'Thank you for your purchase!',
align: LineText.ALIGN_CENTER,
linefeed: 1,
));
await bluetoothPrint.printReceipt(config, list);
This prints
this is conent leftthis is conent right
like this in iOSBut I want
this is conent left this is conent right
like thisAny help is appreciated!