huaji_bluetooth_print 1.6.4
huaji_bluetooth_print: ^1.6.4 copied to clipboard
Flutter Bluetooth printing plugin for Gprinter-compatible ESC, TSC, and CPCL thermal printers.
import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:huaji_bluetooth_print/bluetooth_print_model.dart';
import 'package:huaji_bluetooth_print/huaji_bluetooth_print.dart';
void main() => runApp(const MyApp());
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
HuajiBluetoothPrint bluetoothPrint = HuajiBluetoothPrint.instance;
bool _connected = false;
BluetoothDevice? _device;
String tips = 'no device connect';
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) => initBluetooth());
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initBluetooth() async {
bluetoothPrint.startScan(timeout: const Duration(seconds: 4));
final isConnected = await bluetoothPrint.isConnected;
bluetoothPrint.state.listen((state) {
if (!mounted) {
return;
}
switch (state) {
case HuajiBluetoothPrint.CONNECTED:
setState(() {
_connected = true;
tips = 'connect success';
});
break;
case HuajiBluetoothPrint.DISCONNECTED:
setState(() {
_connected = false;
tips = 'disconnect success';
});
break;
default:
break;
}
});
if (!mounted) return;
if (isConnected) {
setState(() {
_connected = true;
});
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('BluetoothPrint example app'),
),
body: RefreshIndicator(
onRefresh: () =>
bluetoothPrint.startScan(timeout: const Duration(seconds: 4)),
child: SingleChildScrollView(
child: Column(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(
padding: const EdgeInsets.symmetric(
vertical: 10, horizontal: 10),
child: Text(tips),
),
],
),
const Divider(),
StreamBuilder<List<BluetoothDevice>>(
stream: bluetoothPrint.scanResults,
initialData: const [],
builder: (c, snapshot) => Column(
children: snapshot.data!
.map((d) => ListTile(
title: Text(d.name ?? ''),
subtitle: Text(d.address ?? ''),
onTap: () async {
setState(() {
_device = d;
});
},
trailing: _device?.address == d.address
? const Icon(
Icons.check,
color: Colors.green,
)
: null,
))
.toList(),
),
),
const Divider(),
Container(
padding: const EdgeInsets.fromLTRB(20, 5, 20, 10),
child: Column(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
OutlinedButton(
child: const Text('connect'),
onPressed: _connected
? null
: () async {
final device = _device;
if (device != null) {
await bluetoothPrint.connect(device);
} else {
setState(() {
tips = 'please select device';
});
}
},
),
const SizedBox(width: 10.0),
OutlinedButton(
child: const Text('disconnect'),
onPressed: _connected
? () async {
await bluetoothPrint.disconnect();
}
: null,
),
],
),
OutlinedButton(
child: const Text('print receipt(esc)'),
onPressed: _connected
? () async {
Map<String, dynamic> config = {};
List<LineText> list = [];
list.add(LineText(
type: LineText.TYPE_TEXT,
content: 'A Title',
weight: 1,
align: LineText.ALIGN_CENTER,
linefeed: 1));
list.add(LineText(
type: LineText.TYPE_TEXT,
content: 'this is conent left',
weight: 0,
align: LineText.ALIGN_LEFT,
linefeed: 1));
list.add(LineText(
type: LineText.TYPE_TEXT,
content: 'this is conent right',
align: LineText.ALIGN_RIGHT,
linefeed: 1));
list.add(LineText(linefeed: 1));
ByteData data = await rootBundle
.load("assets/images/bluetooth_print.png");
List<int> imageBytes = data.buffer.asUint8List(
data.offsetInBytes, data.lengthInBytes);
String base64Image = base64Encode(imageBytes);
list.add(LineText(
type: LineText.TYPE_IMAGE,
content: base64Image,
align: LineText.ALIGN_CENTER,
linefeed: 1));
await bluetoothPrint.printReceipt(config, list);
}
: null,
),
OutlinedButton(
child: const Text('print label(tsc)'),
onPressed: _connected
? () async {
Map<String, dynamic> config = {};
config['width'] = 40; // 标签宽度,单位mm
config['height'] = 70; // 标签高度,单位mm
config['gap'] = 2; // 标签间隔,单位mm
// x、y坐标位置,单位dpi,1mm=8dpi
List<LineText> list = [];
list.add(LineText(
type: LineText.TYPE_TEXT,
x: 10,
y: 10,
content: 'A Title'));
list.add(LineText(
type: LineText.TYPE_TEXT,
x: 200,
y: 10,
content: 'A Title'));
list.add(LineText(
type: LineText.TYPE_TEXT,
x: 10,
y: 40,
content: 'this is content'));
list.add(LineText(
type: LineText.TYPE_BARCODE,
x: 10,
y: 70,
content: 'qrcode i\n'));
await bluetoothPrint.printLabel(config, list);
}
: null,
),
OutlinedButton(
child: const Text('print selftest'),
onPressed: _connected
? () async {
await bluetoothPrint.printTest();
}
: null,
)
],
),
)
],
),
),
),
floatingActionButton: StreamBuilder<bool>(
stream: bluetoothPrint.isScanning,
initialData: false,
builder: (c, snapshot) {
if (snapshot.data!) {
return FloatingActionButton(
child: const Icon(Icons.stop),
onPressed: () => bluetoothPrint.stopScan(),
backgroundColor: Colors.red,
);
} else {
return FloatingActionButton(
child: const Icon(Icons.search),
onPressed: () => bluetoothPrint.startScan(
timeout: const Duration(seconds: 4)));
}
},
),
),
);
}
}