flutter_sunmi_printer_old 0.0.1
flutter_sunmi_printer_old: ^0.0.1 copied to clipboard
A comprehensive Flutter plugin for Sunmi printer integration. Supports text printing with custom styles, image printing, table/row printing, and provides both original API compatibility and new stream [...]
import 'package:flutter/material.dart';
import 'package:flutter_sunmi_printer_old/flutter_sunmi_printer_old.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final FlutterSunmiPrinterOld _printer = FlutterSunmiPrinterOld();
String _platformVersion = 'Unknown';
final GlobalKey<ScaffoldMessengerState> _scaffoldMessengerKey =
GlobalKey<ScaffoldMessengerState>();
@override
void initState() {
super.initState();
initPlatformState();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
String platformVersion;
// Platform messages may fail, so we use a try/catch PlatformException.
try {
platformVersion =
await _printer.getPlatformVersion() ?? 'Unknown platform version';
await _printer.isPrinting();
} catch (e) {
platformVersion = 'Failed to get platform version.';
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(() {
_platformVersion = platformVersion;
});
}
void _showSnackBar(String message) {
_scaffoldMessengerKey.currentState?.showSnackBar(
SnackBar(content: Text(message)),
);
}
Future<void> _printTestReceipt() async {
try {
// Reset printer
await _printer.reset();
// Start printing
await _printer.startPrint();
// Print header using original API style
await _printer.text(
'SUNMI PRINTER TEST',
styles: SunmiStyles(
align: SunmiAlign.center,
bold: true,
size: SunmiSize.lg,
),
linesAfter: 1,
);
// Print separator using hr method
await _printer.hr(linesAfter: 1);
// Print some text using original API
await _printer.text(
'This is a test receipt',
styles: SunmiStyles(align: SunmiAlign.left),
linesAfter: 1,
);
// Print a table row using original API
await _printer.row(
cols: [
SunmiCol(text: 'Item', width: 8, align: SunmiAlign.left),
SunmiCol(text: 'Price', width: 2, align: SunmiAlign.right),
SunmiCol(text: 'Qty', width: 2, align: SunmiAlign.right),
],
bold: true,
linesAfter: 1,
);
// Print some items using original API
final items = [
['Coffee', '\$3.50', '2'],
['Tea', '\$2.00', '1'],
['Cake', '\$5.00', '1'],
];
for (final item in items) {
await _printer.row(
cols: [
SunmiCol(text: item[0], width: 8, align: SunmiAlign.left),
SunmiCol(text: item[1], width: 2, align: SunmiAlign.right),
SunmiCol(text: item[2], width: 2, align: SunmiAlign.right),
],
linesAfter: 1,
);
}
// Print total using original API
await _printer.text(
'Total: \$14.00',
styles: SunmiStyles(
align: SunmiAlign.right,
bold: true,
size: SunmiSize.md,
),
linesAfter: 2,
);
// Print footer
await _printer.text(
'Thank you for your purchase!',
styles: SunmiStyles(align: SunmiAlign.center),
linesAfter: 2,
);
// Stop printing
await _printer.stopPrint();
if (!mounted) return;
_showSnackBar('Print job completed!');
} catch (e) {
if (!mounted) return;
_showSnackBar('Print error: $e');
}
}
Future<void> _printTestReceiptNewAPI() async {
try {
// Reset printer
await _printer.reset();
// Start printing
await _printer.startPrint();
// Print header using new API
await _printer.printText(
text: 'SUNMI PRINTER TEST (NEW API)',
align: 1, // Center
bold: true,
size: 36,
linesAfter: 1,
);
// Print separator
await _printer.printText(
text: '========================',
align: 1,
linesAfter: 1,
);
// Print some text
await _printer.printText(
text: 'This is a test receipt using new API',
align: 0, // Left
linesAfter: 1,
);
// Print a table row using new API
final rowData = [
{'text': 'Item', 'width': 16, 'align': 0},
{'text': 'Price', 'width': 8, 'align': 2},
{'text': 'Qty', 'width': 8, 'align': 2},
];
await _printer.printRow(
cols: rowData.toString(),
bold: true,
linesAfter: 1,
);
// Print some items
final items = [
['Coffee', '\$3.50', '2'],
['Tea', '\$2.00', '1'],
['Cake', '\$5.00', '1'],
];
for (final item in items) {
final itemData = [
{'text': item[0], 'width': 16, 'align': 0},
{'text': item[1], 'width': 8, 'align': 2},
{'text': item[2], 'width': 8, 'align': 2},
];
await _printer.printRow(cols: itemData.toString(), linesAfter: 1);
}
// Print total
await _printer.printText(
text: 'Total: \$14.00',
align: 2, // Right
bold: true,
size: 28,
linesAfter: 2,
);
// Print footer
await _printer.printText(
text: 'Thank you for your purchase!',
align: 1,
linesAfter: 2,
);
// Stop printing
await _printer.stopPrint();
if (!mounted) return;
_showSnackBar('Print job completed (New API)!');
} catch (e) {
if (!mounted) return;
_showSnackBar('Print error: $e');
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
scaffoldMessengerKey: _scaffoldMessengerKey,
home: Scaffold(
appBar: AppBar(title: const Text('Sunmi Printer Example')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Running on: $_platformVersion\n'),
const SizedBox(height: 20),
ElevatedButton(
onPressed: _printTestReceipt,
child: const Text('Print Test Receipt (Original API)'),
),
const SizedBox(height: 10),
ElevatedButton(
onPressed: _printTestReceiptNewAPI,
child: const Text('Print Test Receipt (New API)'),
),
const SizedBox(height: 20),
ElevatedButton(
onPressed: () async {
final isPrinting = await _printer.isPrinting();
if (!mounted) return;
_showSnackBar('Is printing: $isPrinting');
},
child: const Text('Check Print Status'),
),
],
),
),
),
);
}
}