208 lines
7.2 KiB
Dart
208 lines
7.2 KiB
Dart
import 'dart:async';
|
|
import 'dart:convert';
|
|
import 'dart:typed_data';
|
|
|
|
import 'package:bluetooth_print/bluetooth_print.dart';
|
|
import 'package:bluetooth_print/bluetooth_print_model.dart';
|
|
import 'package:charset_converter/charset_converter.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
|
|
import 'package:logger/logger.dart';
|
|
import 'package:satu/core/utils/logger.dart';
|
|
import 'package:satu/core/utils/pos_printer.dart';
|
|
|
|
class PrinterView extends StatefulWidget {
|
|
const PrinterView({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
_PrinterViewState createState() => _PrinterViewState();
|
|
}
|
|
|
|
class _PrinterViewState extends State<PrinterView> {
|
|
final Logger log = getLogger('_PrinterViewState');
|
|
BluetoothPrint? bluetoothPrint;
|
|
|
|
bool _connected = false;
|
|
BluetoothDevice? _device;
|
|
String tips = 'no device connect';
|
|
|
|
@override
|
|
void initState() {
|
|
bluetoothPrint = BluetoothPrint.instance;
|
|
if (WidgetsBinding.instance != null) {
|
|
WidgetsBinding.instance!.addPostFrameCallback((_) => initBluetooth());
|
|
}
|
|
|
|
super.initState();
|
|
}
|
|
|
|
// Platform messages are asynchronous, so we initialize in an async method.
|
|
Future<void> initBluetooth() async {
|
|
bluetoothPrint!.startScan(timeout: Duration(seconds: 4));
|
|
|
|
final bool isConnected = await bluetoothPrint!.isConnected ?? false;
|
|
|
|
bluetoothPrint!.state.listen((state) {
|
|
print('cur device status: $state');
|
|
|
|
switch (state) {
|
|
case BluetoothPrint.CONNECTED:
|
|
setState(() {
|
|
_connected = true;
|
|
tips = 'connect success';
|
|
});
|
|
break;
|
|
case BluetoothPrint.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: Duration(seconds: 4)),
|
|
child: SingleChildScrollView(
|
|
child: Column(
|
|
children: <Widget>[
|
|
Row(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: <Widget>[
|
|
Padding(
|
|
padding:
|
|
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 != null &&
|
|
_device!.address == d.address
|
|
? const Icon(
|
|
Icons.check,
|
|
color: Colors.green,
|
|
)
|
|
: null,
|
|
))
|
|
.toList(),
|
|
),
|
|
),
|
|
const Divider(),
|
|
Container(
|
|
padding: EdgeInsets.fromLTRB(20, 5, 20, 10),
|
|
child: Column(
|
|
children: <Widget>[
|
|
Row(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: <Widget>[
|
|
OutlinedButton(
|
|
onPressed: _connected
|
|
? null
|
|
: () async {
|
|
if (_device != null &&
|
|
_device!.address != null) {
|
|
await bluetoothPrint!.connect(_device!);
|
|
} else {
|
|
setState(() {
|
|
tips = 'please select device';
|
|
});
|
|
print('please select device');
|
|
}
|
|
},
|
|
child: const Text('connect'),
|
|
),
|
|
SizedBox(width: 10.0),
|
|
OutlinedButton(
|
|
onPressed: _connected
|
|
? () async {
|
|
await bluetoothPrint!.disconnect();
|
|
}
|
|
: null,
|
|
child: const Text('disconnect'),
|
|
),
|
|
],
|
|
),
|
|
OutlinedButton(
|
|
onPressed: _connected
|
|
? () async {
|
|
Map<String, dynamic> config = Map();
|
|
await bluetoothPrint!
|
|
.rawBytes(config, await getReceipt());
|
|
}
|
|
: null,
|
|
child: const Text('print receipt(esc) - text'),
|
|
),
|
|
OutlinedButton(
|
|
onPressed: _connected
|
|
? () async {
|
|
Map<String, dynamic> config = Map();
|
|
await bluetoothPrint!
|
|
.rawBytes(config, await getReceiptImg());
|
|
}
|
|
: null,
|
|
child: const Text('print label(esc) img'),
|
|
),
|
|
],
|
|
),
|
|
)
|
|
],
|
|
),
|
|
),
|
|
),
|
|
floatingActionButton: StreamBuilder<bool>(
|
|
stream: bluetoothPrint!.isScanning,
|
|
initialData: false,
|
|
builder: (c, snapshot) {
|
|
if (snapshot.data != null) {
|
|
return FloatingActionButton(
|
|
onPressed: () => bluetoothPrint!.stopScan(),
|
|
backgroundColor: Colors.red,
|
|
child: const Icon(Icons.stop),
|
|
);
|
|
} else {
|
|
return FloatingActionButton(
|
|
child: const Icon(Icons.search),
|
|
onPressed: () => bluetoothPrint!
|
|
.startScan(timeout: const Duration(seconds: 4)));
|
|
}
|
|
},
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|