ciontek_sdk_flutter 0.0.2
ciontek_sdk_flutter: ^0.0.2 copied to clipboard
A Flutter plugin for Ciontek SDK, providing functionalities to interact with Ciontek devices.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:ciontek_sdk_flutter/ciontek_sdk_flutter.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';
final _ciontekSdkFlutterPlugin = CiontekSdkFlutter();
@override
void initState() {
super.initState();
initPlatformState();
connectPrinter();
}
// 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.
// We also handle the message potentially returning null.
try {
platformVersion =
await _ciontekSdkFlutterPlugin.getPlatformVersion() ??
'Unknown platform version';
} on PlatformException {
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;
});
}
Future<void> connectPrinter() async {
String? result;
try {
result = await _ciontekSdkFlutterPlugin.connectPrinter();
} on PlatformException catch (e) {
result = "Failed to connect printer: '${e.message}'.";
}
// 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 = result ?? 'Unknown connection result';
});
}
void _printTestReceipt(BuildContext context) async {
// Initialize printer
await _ciontekSdkFlutterPlugin.printInit();
// Set font (example values: fontType=1, fontSize=0, fontStyle=0)
await _ciontekSdkFlutterPlugin.setPrinterFont(16, 16, 0x33);
await _ciontekSdkFlutterPlugin.setPrinterFont(24, 24, 0x00);
// Set mode (example value: 0)
await _ciontekSdkFlutterPlugin.prnSetMode(0);
// Set gray level (example value: 2)
await _ciontekSdkFlutterPlugin.prnSetGray(5);
// Check printer status before printing
int? status = await _ciontekSdkFlutterPlugin.printCheckStatus();
if (!mounted) return; // Ensure widget is still in the tree
if (status == 0) {
// Printer is ready
await _ciontekSdkFlutterPlugin.prnStr("=== TEST RECEIPT ===\n");
await _ciontekSdkFlutterPlugin.prnStr("Item 1 \$10.00\n");
await _ciontekSdkFlutterPlugin.prnStr("Item 2 \$5.00\n");
await _ciontekSdkFlutterPlugin.prnStr("-------------------\n");
await _ciontekSdkFlutterPlugin.prnStr("TOTAL: \$15.00\n");
await _ciontekSdkFlutterPlugin.prnStr("Thank you for your purchase!\n");
await _ciontekSdkFlutterPlugin.prnStr("Visit us again!\n");
await _ciontekSdkFlutterPlugin.prnStr(" \n");
await _ciontekSdkFlutterPlugin.prnStr(" \n");
await _ciontekSdkFlutterPlugin.prnStr(" \n");
await _ciontekSdkFlutterPlugin.prnStr("\n\n\n\n\n\n");
await _ciontekSdkFlutterPlugin.prnStart();
await _ciontekSdkFlutterPlugin.prnFeedPaper(
300,
); // Feed paper after printing
} else {
// Handle printer error
String errorMsg;
switch (status) {
case -1:
errorMsg = "Printer needs paper.";
break;
case -2:
errorMsg = "Printer high temperature.";
break;
case -3:
errorMsg = "Low battery voltage.";
break;
default:
errorMsg = "Unknown printer error.";
}
// Use addPostFrameCallback to avoid async gap issues
WidgetsBinding.instance.addPostFrameCallback((_) {
if (mounted) {
ScaffoldMessenger.of(
context,
).showSnackBar(SnackBar(content: Text(errorMsg)));
}
});
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Plugin example app')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('Running on: $_platformVersion\n'),
ElevatedButton(
onPressed: connectPrinter,
child: const Text('Connect Printer'),
),
ElevatedButton(
onPressed: () {
_printTestReceipt(context);
},
child: const Text('Print Test Receipt'),
),
],
),
),
),
);
}
}