| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- import 'package:flutter/material.dart';
- import 'package:flutter/services.dart';
- import 'package:lszlgl/base/base_lifecycle_state.dart';
- import 'package:lszlgl/main.dart';
- import 'package:lszlgl/service/print_service.dart';
- import 'package:lszlgl/widget/loading_widget.dart';
- import 'package:lszlgl/widget/page_widget.dart';
- import 'package:signature/signature.dart';
- import 'package:lszlgl/widget/button.dart';
- import '../../plugin/bluetooth_plugin.dart';
- import '../home/home_page.dart';
- class ConnectPrintPageArgs {
- int count;
- ConnectPrintPageArgs({this.count = 1});
- @override
- String toString() {
- return {'count': count}.toString();
- }
- }
- /// 电子签名
- class ConnectPrintPage extends StatefulWidget {
- final ConnectPrintPageArgs args;
- const ConnectPrintPage({
- super.key,
- required this.args,
- });
- @override
- State<ConnectPrintPage> createState() => _ConnectPrintPageState();
- }
- class _ConnectPrintPageState extends BaseLifecycleState<ConnectPrintPage> {
- late List<ServiceModel> serviceList;
- late List<BlueDeviceInfo> deviceList;
- late List<String> deviceMacList;
- late List<BlueDeviceInfo> connectedDeviceList;
- late List<String> connectedDeviceMacList;
- String scanDeviceState = '';
- @override
- void initState() {
- super.initState();
- serviceList = [
- ServiceModel(name: '搜索', icon: imgHomeListPzjc, onTap: () => startScan()),
- ];
- deviceList = [];
- deviceMacList = [];
- connectedDeviceList = [];
- connectedDeviceMacList = [];
- }
- /// 去搜索
- Future<void> startScan() async {
- setState(() {
- deviceList = [];
- deviceMacList = [];
- });
- await PrintService.startBluetoothDiscovery();
- }
- /// 去连接
- Future<void> startConnect(BlueDeviceInfo deviceInfo) async {
- if(deviceInfo.connectStateStr == '未配对') {
- MyNavigator.showLoading(msg: '配对中...');
- await PrintService.startBluetoothPair(deviceInfo);
- } else if(deviceInfo.connectStateStr == '已配对') {
- MyNavigator.showLoading(msg: '连接中...');
- int connectResult = await PrintService.startBluetoothConnect(deviceInfo);
- if(connectResult == 0) { // 连接成功
- MyNavigator.dismiss();
- MyNavigator.showToast('连接成功');
- deviceInfo.connectSuccess();
- if(deviceMacList.contains(deviceInfo.deviceMac)) {
- setState(() {
- deviceMacList.removeWhere((element) => element == deviceInfo.deviceMac);
- deviceList.removeWhere((element) => element.deviceMac == deviceInfo.deviceMac);
- });
- }
- setState(() {
- connectedDeviceMacList.add(deviceInfo.deviceMac);
- connectedDeviceList.add(deviceInfo);
- });
- } else {
- MyNavigator.dismiss();
- MyNavigator.showToast('连接失败');
- }
- } else if(deviceInfo.connectStateStr == '断开') {
- int connectResult = await PrintService.endBluetoothConnect(deviceInfo);
- if(connectResult == 0) { // 断开成功
- deviceInfo.disConnectSuccess();
- if(connectedDeviceMacList.contains(deviceInfo.deviceMac)) {
- setState(() {
- connectedDeviceMacList.removeWhere((element) => element == deviceInfo.deviceMac);
- connectedDeviceList.removeWhere((element) => element.deviceMac == deviceInfo.deviceMac);
- });
- }
- setState(() {
- deviceMacList.add(deviceInfo.deviceMac);
- deviceList.add(deviceInfo);
- });
- }
- }
- }
- @override
- void onInit() {
- BluetoothPlugin.instance.onReceiveDataStream.listen((deviceInfo) {
- if(deviceInfo != null) {
- if(deviceMacList.contains(deviceInfo.deviceMac)) {
- setState(() {
- deviceMacList.removeWhere((element) => element == deviceInfo.deviceMac);
- deviceList.removeWhere((element) => element.deviceMac == deviceInfo.deviceMac);
- });
- }
- setState(() {
- deviceMacList.add(deviceInfo.deviceMac);
- deviceList.add(deviceInfo);
- });
- }
- });
- BluetoothPlugin.instance.onDeviceStateStream.listen((deviceState) {
- if(deviceState == DeviceState.pairEnd) {
- MyNavigator.dismiss();
- }
- setState(() {
- scanDeviceState = deviceState ?? "";
- });
- });
- }
- @override
- void onDestroy() {
- }
- @override
- Widget build(BuildContext context) {
- return myScaffold(
- child: Column(
- children: [
- myAppBar(title: '连接打印机'),
- const SizedBox(height: 18),
- ...List.generate(
- serviceList.length,
- (index) => buildServiceItem(serviceList[index]),
- ).toList(),
- const Text('已连接打印机'),
- ...List.generate(
- connectedDeviceList.length,
- (index) => buildDeviceItem(connectedDeviceList[index]),
- ).toList(),
- const Text('可用打印机'),
- ...List.generate(
- deviceList.length,
- (index) => buildDeviceItem(deviceList[index]),
- ).toList(),
- ],
- ),
- );
- }
- Widget buildServiceItem(ServiceModel service) {
- return GestureDetector(
- onTap: service.onTap,
- child: Container(
- margin: const EdgeInsets.only(left: 12, right: 12, bottom: 22),
- padding: const EdgeInsets.symmetric(vertical: 20, horizontal: 40),
- clipBehavior: Clip.hardEdge,
- decoration: BoxDecoration(
- borderRadius: const BorderRadius.all(Radius.circular(12)),
- boxShadow: [BoxShadow(color: Colors.black.withOpacity(0.1), offset: const Offset(0, 5), blurRadius: 4)],
- image: const DecorationImage(image: AssetImage(imgHomeListBg), fit: BoxFit.fill),
- ),
- child: Row(
- children: [
- Image.asset(service.icon, height: 64),
- const SizedBox(width: 12),
- Expanded(
- child: Column(
- mainAxisSize: MainAxisSize.min,
- children: [
- Text(
- "${service.name}${scanDeviceState == DeviceState.scanStart ? "中..." : ""}",
- textAlign: TextAlign.center,
- style: const TextStyle(color: Color(0xFF333333), fontSize: 20, fontWeight: FontWeight.w500),
- ),
- ],
- ),
- ),
- ],
- ),
- ),
- );
- }
- Widget buildDeviceItem(BlueDeviceInfo deviceInfo) {
- return GestureDetector(
- onTap: () {
- // 连接
- startConnect(deviceInfo);
- },
- child: Container(
- margin: const EdgeInsets.only(left: 12, right: 12, bottom: 22),
- padding: const EdgeInsets.symmetric(vertical: 10),
- clipBehavior: Clip.hardEdge,
- decoration: BoxDecoration(
- borderRadius: const BorderRadius.all(Radius.circular(12)),
- boxShadow: [BoxShadow(color: Colors.black.withOpacity(0.1), offset: const Offset(0, 5), blurRadius: 4)],
- image: const DecorationImage(image: AssetImage(imgHomeListBg), fit: BoxFit.fill),
- ),
- child: Row(
- mainAxisAlignment: MainAxisAlignment.spaceAround,
- children: [
- Text(
- deviceInfo.deviceName,
- textAlign: TextAlign.center,
- style: const TextStyle(color: Color(0xFF333333), fontSize: 15, fontWeight: FontWeight.w500),
- ),
- Text(
- deviceInfo.deviceMac,
- textAlign: TextAlign.center,
- style: const TextStyle(color: Color(0xFF333333), fontSize: 15, fontWeight: FontWeight.w500),
- ),
- Text(
- deviceInfo.connectStateStr,
- textAlign: TextAlign.center,
- style: const TextStyle(color: Color(0xFF333333), fontSize: 15, fontWeight: FontWeight.w500),
- ),
- ],
- ),
- ),
- );
- }
- }
|