connect_print_page.dart 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/services.dart';
  3. import 'package:lszlgl/base/base_lifecycle_state.dart';
  4. import 'package:lszlgl/main.dart';
  5. import 'package:lszlgl/service/print_service.dart';
  6. import 'package:lszlgl/widget/loading_widget.dart';
  7. import 'package:lszlgl/widget/page_widget.dart';
  8. import 'package:signature/signature.dart';
  9. import 'package:lszlgl/widget/button.dart';
  10. import '../../plugin/bluetooth_plugin.dart';
  11. import '../home/home_page.dart';
  12. class ConnectPrintPageArgs {
  13. int count;
  14. ConnectPrintPageArgs({this.count = 1});
  15. @override
  16. String toString() {
  17. return {'count': count}.toString();
  18. }
  19. }
  20. /// 电子签名
  21. class ConnectPrintPage extends StatefulWidget {
  22. final ConnectPrintPageArgs args;
  23. const ConnectPrintPage({
  24. super.key,
  25. required this.args,
  26. });
  27. @override
  28. State<ConnectPrintPage> createState() => _ConnectPrintPageState();
  29. }
  30. class _ConnectPrintPageState extends BaseLifecycleState<ConnectPrintPage> {
  31. late List<ServiceModel> serviceList;
  32. late List<BlueDeviceInfo> deviceList;
  33. late List<String> deviceMacList;
  34. late List<BlueDeviceInfo> connectedDeviceList;
  35. late List<String> connectedDeviceMacList;
  36. String scanDeviceState = '';
  37. @override
  38. void initState() {
  39. super.initState();
  40. serviceList = [
  41. ServiceModel(name: '搜索', icon: imgHomeListPzjc, onTap: () => startScan()),
  42. ];
  43. deviceList = [];
  44. deviceMacList = [];
  45. connectedDeviceList = [];
  46. connectedDeviceMacList = [];
  47. }
  48. /// 去搜索
  49. Future<void> startScan() async {
  50. setState(() {
  51. deviceList = [];
  52. deviceMacList = [];
  53. });
  54. await PrintService.startBluetoothDiscovery();
  55. }
  56. /// 去连接
  57. Future<void> startConnect(BlueDeviceInfo deviceInfo) async {
  58. if(deviceInfo.connectStateStr == '未配对') {
  59. MyNavigator.showLoading(msg: '配对中...');
  60. await PrintService.startBluetoothPair(deviceInfo);
  61. } else if(deviceInfo.connectStateStr == '已配对') {
  62. MyNavigator.showLoading(msg: '连接中...');
  63. int connectResult = await PrintService.startBluetoothConnect(deviceInfo);
  64. if(connectResult == 0) { // 连接成功
  65. MyNavigator.dismiss();
  66. MyNavigator.showToast('连接成功');
  67. deviceInfo.connectSuccess();
  68. if(deviceMacList.contains(deviceInfo.deviceMac)) {
  69. setState(() {
  70. deviceMacList.removeWhere((element) => element == deviceInfo.deviceMac);
  71. deviceList.removeWhere((element) => element.deviceMac == deviceInfo.deviceMac);
  72. });
  73. }
  74. setState(() {
  75. connectedDeviceMacList.add(deviceInfo.deviceMac);
  76. connectedDeviceList.add(deviceInfo);
  77. });
  78. } else {
  79. MyNavigator.dismiss();
  80. MyNavigator.showToast('连接失败');
  81. }
  82. } else if(deviceInfo.connectStateStr == '断开') {
  83. int connectResult = await PrintService.endBluetoothConnect(deviceInfo);
  84. if(connectResult == 0) { // 断开成功
  85. deviceInfo.disConnectSuccess();
  86. if(connectedDeviceMacList.contains(deviceInfo.deviceMac)) {
  87. setState(() {
  88. connectedDeviceMacList.removeWhere((element) => element == deviceInfo.deviceMac);
  89. connectedDeviceList.removeWhere((element) => element.deviceMac == deviceInfo.deviceMac);
  90. });
  91. }
  92. setState(() {
  93. deviceMacList.add(deviceInfo.deviceMac);
  94. deviceList.add(deviceInfo);
  95. });
  96. }
  97. }
  98. }
  99. @override
  100. void onInit() {
  101. BluetoothPlugin.instance.onReceiveDataStream.listen((deviceInfo) {
  102. if(deviceInfo != null) {
  103. if(deviceMacList.contains(deviceInfo.deviceMac)) {
  104. setState(() {
  105. deviceMacList.removeWhere((element) => element == deviceInfo.deviceMac);
  106. deviceList.removeWhere((element) => element.deviceMac == deviceInfo.deviceMac);
  107. });
  108. }
  109. setState(() {
  110. deviceMacList.add(deviceInfo.deviceMac);
  111. deviceList.add(deviceInfo);
  112. });
  113. }
  114. });
  115. BluetoothPlugin.instance.onDeviceStateStream.listen((deviceState) {
  116. if(deviceState == DeviceState.pairEnd) {
  117. MyNavigator.dismiss();
  118. }
  119. setState(() {
  120. scanDeviceState = deviceState ?? "";
  121. });
  122. });
  123. }
  124. @override
  125. void onDestroy() {
  126. }
  127. @override
  128. Widget build(BuildContext context) {
  129. return myScaffold(
  130. child: Column(
  131. children: [
  132. myAppBar(title: '连接打印机'),
  133. const SizedBox(height: 18),
  134. ...List.generate(
  135. serviceList.length,
  136. (index) => buildServiceItem(serviceList[index]),
  137. ).toList(),
  138. const Text('已连接打印机'),
  139. ...List.generate(
  140. connectedDeviceList.length,
  141. (index) => buildDeviceItem(connectedDeviceList[index]),
  142. ).toList(),
  143. const Text('可用打印机'),
  144. ...List.generate(
  145. deviceList.length,
  146. (index) => buildDeviceItem(deviceList[index]),
  147. ).toList(),
  148. ],
  149. ),
  150. );
  151. }
  152. Widget buildServiceItem(ServiceModel service) {
  153. return GestureDetector(
  154. onTap: service.onTap,
  155. child: Container(
  156. margin: const EdgeInsets.only(left: 12, right: 12, bottom: 22),
  157. padding: const EdgeInsets.symmetric(vertical: 20, horizontal: 40),
  158. clipBehavior: Clip.hardEdge,
  159. decoration: BoxDecoration(
  160. borderRadius: const BorderRadius.all(Radius.circular(12)),
  161. boxShadow: [BoxShadow(color: Colors.black.withOpacity(0.1), offset: const Offset(0, 5), blurRadius: 4)],
  162. image: const DecorationImage(image: AssetImage(imgHomeListBg), fit: BoxFit.fill),
  163. ),
  164. child: Row(
  165. children: [
  166. Image.asset(service.icon, height: 64),
  167. const SizedBox(width: 12),
  168. Expanded(
  169. child: Column(
  170. mainAxisSize: MainAxisSize.min,
  171. children: [
  172. Text(
  173. "${service.name}${scanDeviceState == DeviceState.scanStart ? "中..." : ""}",
  174. textAlign: TextAlign.center,
  175. style: const TextStyle(color: Color(0xFF333333), fontSize: 20, fontWeight: FontWeight.w500),
  176. ),
  177. ],
  178. ),
  179. ),
  180. ],
  181. ),
  182. ),
  183. );
  184. }
  185. Widget buildDeviceItem(BlueDeviceInfo deviceInfo) {
  186. return GestureDetector(
  187. onTap: () {
  188. // 连接
  189. startConnect(deviceInfo);
  190. },
  191. child: Container(
  192. margin: const EdgeInsets.only(left: 12, right: 12, bottom: 22),
  193. padding: const EdgeInsets.symmetric(vertical: 10),
  194. clipBehavior: Clip.hardEdge,
  195. decoration: BoxDecoration(
  196. borderRadius: const BorderRadius.all(Radius.circular(12)),
  197. boxShadow: [BoxShadow(color: Colors.black.withOpacity(0.1), offset: const Offset(0, 5), blurRadius: 4)],
  198. image: const DecorationImage(image: AssetImage(imgHomeListBg), fit: BoxFit.fill),
  199. ),
  200. child: Row(
  201. mainAxisAlignment: MainAxisAlignment.spaceAround,
  202. children: [
  203. Text(
  204. deviceInfo.deviceName,
  205. textAlign: TextAlign.center,
  206. style: const TextStyle(color: Color(0xFF333333), fontSize: 15, fontWeight: FontWeight.w500),
  207. ),
  208. Text(
  209. deviceInfo.deviceMac,
  210. textAlign: TextAlign.center,
  211. style: const TextStyle(color: Color(0xFF333333), fontSize: 15, fontWeight: FontWeight.w500),
  212. ),
  213. Text(
  214. deviceInfo.connectStateStr,
  215. textAlign: TextAlign.center,
  216. style: const TextStyle(color: Color(0xFF333333), fontSize: 15, fontWeight: FontWeight.w500),
  217. ),
  218. ],
  219. ),
  220. ),
  221. );
  222. }
  223. }