sunmi_printer_v2 1.0.1-null-safety.2 sunmi_printer_v2: ^1.0.1-null-safety.2 copied to clipboard
Printer Plugin for Sunmi V2.
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:sunmi_printer_v2/sunmi_printer_v2.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String? _serialNo = 'Unknown';
@override
void initState() {
super.initState();
initPlatformState();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
String? serialNo;
// Platform messages may fail, so we use a try/catch PlatformException.
try {
serialNo = await SunmiPrinterV2.getPrinterSerialNo;
} on PlatformException {
serialNo = 'Failed to get serial no.';
}
// 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(() {
_serialNo = serialNo;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.orange,
),
home: Scaffold(
appBar: AppBar(
title: const Text('Sunmi Test app'),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Center(
child: Text('Printer SN: $_serialNo\n'),
),
ElevatedButton(
onPressed: () async {
await SunmiPrinterV2.printSample();
},
child: Text("Print"),
),
ElevatedButton(
onPressed: () async {
await SunmiPrinterV2.feedPaper();
},
child: Text("Feed Paper"),
),
ElevatedButton(
onPressed: () async {
await SunmiPrinterV2.printBarcode(
barcode: "123456789012",
type: BarcodeType.ean_13,
textPosition: BarcodeTextPosition.noText,
);
await SunmiPrinterV2.feedPaper();
},
child: Text("Print Barcode"),
),
ElevatedButton(
onPressed: () async {
await SunmiPrinterV2.initPrinter();
await SunmiPrinterV2.printLineDivider();
await SunmiPrinterV2.setBold(true);
await SunmiPrinterV2.printTable(
texts: ["Item", "Price"],
);
await SunmiPrinterV2.setBold(false);
await SunmiPrinterV2.printLineDivider();
await SunmiPrinterV2.printText("Coca Cola 300ml",
isBold: true, size: 28);
await SunmiPrinterV2.lineWrap();
await SunmiPrinterV2.setBold(false);
await SunmiPrinterV2.printTable(
texts: ["10.000 250.00", "2500.00"], widths: [2, 1]);
await SunmiPrinterV2.printLineDivider();
await SunmiPrinterV2.feedPaper();
},
child: Text("Print Table"),
),
ElevatedButton(
onPressed: () async {
await SunmiPrinterV2.initPrinter();
await SunmiPrinterV2.setAntiWhite(true);
await SunmiPrinterV2.setAlign(PrinterTextAlignment.center);
await SunmiPrinterV2.printText("Genius Soft", size: 30);
await SunmiPrinterV2.setAntiWhite(false);
await SunmiPrinterV2.initPrinter();
await SunmiPrinterV2.feedPaper();
},
child: Text("Anti White"),
),
],
),
),
);
}
}