cage_change_page.dart 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. import 'package:chicken_farm/apis/index.dart';
  2. import 'package:chicken_farm/core/utils/datetime_util.dart';
  3. import 'package:chicken_farm/core/utils/logger.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:chicken_farm/components/vb_app_bar.dart';
  6. import 'package:chicken_farm/core/utils/toast.dart';
  7. import 'package:chicken_farm/core/services/pda/scan_channel.dart';
  8. import 'package:chicken_farm/components/vb_electronic_id_field.dart';
  9. import 'package:go_router/go_router.dart';
  10. class CageChangePage extends StatefulWidget {
  11. const CageChangePage({super.key});
  12. @override
  13. State<CageChangePage> createState() => _CageChangePageState();
  14. }
  15. class _CageChangePageState extends State<CageChangePage> {
  16. final List<String> _electronicIds = [];
  17. // 添加TextEditingControllers用于控制输入框
  18. // final TextEditingController _sourceCageController = TextEditingController();
  19. // final TextEditingController _targetCageController = TextEditingController();
  20. final TextEditingController _cageController = TextEditingController();
  21. // 添加FocusNode用于控制焦点
  22. // final FocusNode _sourceCageFocusNode = FocusNode();
  23. // final FocusNode _targetCageFocusNode = FocusNode();
  24. // 扫描状态
  25. // bool _isScanningSource = false;
  26. // bool _isScanningTarget = false;
  27. // int _scanTag = 1;
  28. bool _isScanningCode = false;
  29. @override
  30. void initState() {
  31. super.initState();
  32. // _sourceCageController.clear();
  33. // _targetCageController.clear();
  34. // 监听焦点变化以更新_scanTag值
  35. // _sourceCageFocusNode.addListener(() {
  36. // if (_sourceCageFocusNode.hasFocus) {
  37. // _scanTag = 1;
  38. // }
  39. // });
  40. // _targetCageFocusNode.addListener(() {
  41. // if (_targetCageFocusNode.hasFocus) {
  42. // _scanTag = 2;
  43. // }
  44. // });
  45. _isScanningCode = false;
  46. _cageController.clear();
  47. // 延迟到下一帧执行,确保页面构建完成后再打开扫描头
  48. WidgetsBinding.instance.addPostFrameCallback((_) {
  49. ScanChannel.openScanHead().then((success) {
  50. if (success) {
  51. logger.d("扫描头已打开");
  52. } else {
  53. ToastUtil.errorAlert("扫描头打开失败");
  54. if (mounted) {
  55. context.pop();
  56. }
  57. }
  58. });
  59. // 初始化监听
  60. ScanChannel.initScanListener(
  61. onScanResult: (result) {
  62. // if (_scanTag == 1) {
  63. // // 源笼号
  64. // setState(() {
  65. // _sourceCageController.text = result;
  66. // _isScanningSource = false;
  67. // });
  68. // if (_targetCageController.text.isEmpty) {
  69. // // 源笼号扫描成功后自动延时扫描目标笼号
  70. // ScanChannel.stopSingleScan();
  71. // logger.d("自动扫描目标笼号");
  72. // _targetCageFocusNode.requestFocus();
  73. // _dealyScanCode(2);
  74. // }
  75. // } else if (_scanTag == 2) {
  76. // // 目标笼号
  77. // setState(() {
  78. // _targetCageController.text = result;
  79. // _isScanningTarget = false;
  80. // });
  81. // }
  82. // // 目标笼号
  83. // setState(() {
  84. // logger.d("扫描目标笼号:$result");
  85. // _targetCageController.text = result;
  86. // _isScanningTarget = false;
  87. // });
  88. setState(() {
  89. logger.d("扫描目标笼号:$result");
  90. _cageController.text = result;
  91. _isScanningCode = false;
  92. });
  93. // 扫码成功后停止解码(保留扫描头)
  94. ScanChannel.stopSingleScan();
  95. },
  96. onScanError: (error) {
  97. logger.d("扫码错误:$error");
  98. setState(() {
  99. _isScanningCode = false;
  100. });
  101. },
  102. onKeyPress: (bool isDouble, String keyCode) {
  103. if (isDouble && keyCode == "KEY_619") {
  104. // _sourceCageController.clear();
  105. // _targetCageController.clear();
  106. _cageController.clear();
  107. // _scanTag = 1;
  108. ScanChannel.startScan();
  109. } else {
  110. logger.d("按键:$isDouble $keyCode");
  111. }
  112. },
  113. );
  114. });
  115. // _scanTag = 1;
  116. }
  117. @override
  118. void dispose() {
  119. // 释放TextEditingControllers
  120. // _sourceCageController.dispose();
  121. // _targetCageController.dispose();
  122. _cageController.dispose();
  123. // 释放FocusNode
  124. // _sourceCageFocusNode.dispose();
  125. // _targetCageFocusNode.dispose();
  126. // 手动关闭扫描头,立即释放资源
  127. ScanChannel.closeScanHead();
  128. // 释放通道
  129. ScanChannel.dispose();
  130. super.dispose();
  131. }
  132. @override
  133. Widget build(BuildContext context) {
  134. return Scaffold(
  135. appBar: const VberAppBar(title: '换笼管理', showLeftButton: true),
  136. body: SingleChildScrollView(
  137. child: Padding(
  138. padding: const EdgeInsets.all(16.0),
  139. child: Column(
  140. crossAxisAlignment: CrossAxisAlignment.start,
  141. children: [
  142. // // 源笼号区域
  143. // _buildCageSection(1),
  144. // const SizedBox(height: 15),
  145. // // 目标笼号区域
  146. // _buildCageSection(2),
  147. // const SizedBox(height: 15),
  148. // 目标笼号区域
  149. _buildCageSection(),
  150. const SizedBox(height: 15),
  151. // 电子编号区域
  152. _buildElectronicIdSection(),
  153. const SizedBox(height: 15),
  154. // 提交按钮
  155. // SizedBox(
  156. // width: double.infinity,
  157. // child: ElevatedButton(
  158. // onPressed:
  159. // _sourceCageController.text.isNotEmpty &&
  160. // _targetCageController.text.isNotEmpty &&
  161. // _rfids.isNotEmpty
  162. // ? _handleSubmit
  163. // : null,
  164. // style: ElevatedButton.styleFrom(
  165. // backgroundColor:
  166. // (_sourceCageController.text.isNotEmpty &&
  167. // _targetCageController.text.isNotEmpty &&
  168. // _rfids.isNotEmpty)
  169. // ? Colors.blue
  170. // : Colors.grey,
  171. // foregroundColor: Colors.white,
  172. // ),
  173. // child: const Text('提交'),
  174. // ),
  175. // ),
  176. SizedBox(
  177. width: double.infinity,
  178. child: ElevatedButton(
  179. onPressed:
  180. _cageController.text.isNotEmpty &&
  181. _electronicIds.isNotEmpty
  182. ? _handleSubmit
  183. : null,
  184. style: ElevatedButton.styleFrom(
  185. backgroundColor:
  186. (_cageController.text.isNotEmpty &&
  187. _electronicIds.isNotEmpty)
  188. ? Colors.blue
  189. : Colors.grey,
  190. foregroundColor: Colors.white,
  191. ),
  192. child: const Text('提交'),
  193. ),
  194. ),
  195. const SizedBox(height: 20),
  196. // 已识别的电子编号列表
  197. if (_electronicIds.isNotEmpty) ...[
  198. Row(
  199. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  200. children: [
  201. const Text(
  202. '已识别的电子编号',
  203. style: TextStyle(fontWeight: FontWeight.bold),
  204. ),
  205. IconButton(
  206. icon: const Icon(Icons.clear, size: 18),
  207. onPressed: _clearElectronicIds,
  208. tooltip: '清空编号',
  209. ),
  210. ],
  211. ),
  212. const SizedBox(height: 10),
  213. Container(
  214. padding: const EdgeInsets.all(10),
  215. decoration: BoxDecoration(
  216. border: Border.all(color: Colors.grey),
  217. borderRadius: BorderRadius.circular(8),
  218. ),
  219. child: ListView.builder(
  220. shrinkWrap: true,
  221. physics: const NeverScrollableScrollPhysics(),
  222. padding: EdgeInsets.zero,
  223. itemCount: _electronicIds.length,
  224. itemBuilder: (context, index) {
  225. return ListTile(
  226. visualDensity: VisualDensity.compact,
  227. contentPadding: const EdgeInsets.symmetric(
  228. horizontal: 2,
  229. vertical: 0,
  230. ),
  231. title: Text(_electronicIds[index]),
  232. trailing: IconButton(
  233. icon: const Icon(
  234. Icons.delete,
  235. size: 18,
  236. color: Colors.red,
  237. ),
  238. onPressed: () => _removeElectronicId(index),
  239. ),
  240. );
  241. },
  242. ),
  243. ),
  244. ],
  245. const SizedBox(height: 20),
  246. ],
  247. ),
  248. ),
  249. ),
  250. );
  251. }
  252. // Widget _buildCageSection(int tag) {
  253. // String title = '源笼号';
  254. // TextEditingController controller = _sourceCageController;
  255. // FocusNode focusNode = _sourceCageFocusNode;
  256. // bool isScanning = _isScanningSource;
  257. // if (tag != 1) {
  258. // tag = 2;
  259. // title = '目标笼号';
  260. // controller = _targetCageController;
  261. // focusNode = _targetCageFocusNode;
  262. // isScanning = _isScanningTarget;
  263. // }
  264. // // 确保光标在文本末尾
  265. // if (controller.text.isNotEmpty) {
  266. // controller.selection = TextSelection.fromPosition(
  267. // TextPosition(offset: controller.text.length),
  268. // );
  269. // }
  270. // return Column(
  271. // crossAxisAlignment: CrossAxisAlignment.start,
  272. // children: [
  273. // Text(title, style: const TextStyle(fontWeight: FontWeight.bold)),
  274. // const SizedBox(height: 10),
  275. // Container(
  276. // padding: const EdgeInsets.fromLTRB(16, 2, 16, 2),
  277. // decoration: BoxDecoration(
  278. // border: Border.all(color: Colors.grey),
  279. // borderRadius: BorderRadius.circular(8),
  280. // ),
  281. // child: Row(
  282. // children: [
  283. // Expanded(
  284. // // 将Text替换为TextField
  285. // child: TextField(
  286. // focusNode: focusNode,
  287. // controller: controller,
  288. // enabled: controller.text.isEmpty,
  289. // decoration: InputDecoration(
  290. // border: InputBorder.none,
  291. // hintText: controller.text.isNotEmpty ? null : '未扫描',
  292. // hintStyle: TextStyle(
  293. // color: controller.text.isNotEmpty
  294. // ? Colors.black
  295. // : Colors.grey,
  296. // fontSize: 16,
  297. // ),
  298. // ),
  299. // style: TextStyle(
  300. // color: controller.text.isNotEmpty
  301. // ? Colors.black
  302. // : Colors.grey,
  303. // fontSize: 16,
  304. // ),
  305. // onChanged: (text) {
  306. // setState(() {
  307. // // 文本更改由controller自动处理
  308. // });
  309. // },
  310. // ),
  311. // ),
  312. // if (controller.text.isNotEmpty) ...[
  313. // IconButton(
  314. // icon: const Icon(Icons.refresh, size: 20),
  315. // onPressed: () {
  316. // _handleChangeCageCode(tag);
  317. // },
  318. // ),
  319. // ] else ...[
  320. // IconButton(
  321. // icon: isScanning
  322. // ? const SizedBox(
  323. // width: 20,
  324. // height: 20,
  325. // child: CircularProgressIndicator(strokeWidth: 2),
  326. // )
  327. // : const Icon(Icons.qr_code_scanner, size: 20),
  328. // onPressed: () {
  329. // _handleScanCageCode(tag);
  330. // },
  331. // ),
  332. // ],
  333. // ],
  334. // ),
  335. // ),
  336. // ],
  337. // );
  338. // }
  339. Widget _buildCageSection() {
  340. // 确保光标在文本末尾
  341. if (_cageController.text.isNotEmpty) {
  342. _cageController.selection = TextSelection.fromPosition(
  343. TextPosition(offset: _cageController.text.length),
  344. );
  345. }
  346. return Column(
  347. crossAxisAlignment: CrossAxisAlignment.start,
  348. children: [
  349. Text('目标笼号', style: const TextStyle(fontWeight: FontWeight.bold)),
  350. const SizedBox(height: 10),
  351. Container(
  352. padding: const EdgeInsets.fromLTRB(16, 2, 16, 2),
  353. decoration: BoxDecoration(
  354. border: Border.all(color: Colors.grey),
  355. borderRadius: BorderRadius.circular(8),
  356. ),
  357. child: Row(
  358. children: [
  359. Expanded(
  360. // 将Text替换为TextField
  361. child: TextField(
  362. controller: _cageController,
  363. enabled: _cageController.text.isEmpty,
  364. decoration: InputDecoration(
  365. border: InputBorder.none,
  366. hintText: _cageController.text.isNotEmpty ? null : '未扫描',
  367. hintStyle: TextStyle(
  368. color: _cageController.text.isNotEmpty
  369. ? Colors.black
  370. : Colors.grey,
  371. fontSize: 16,
  372. ),
  373. ),
  374. style: TextStyle(
  375. color: _cageController.text.isNotEmpty
  376. ? Colors.black
  377. : Colors.grey,
  378. fontSize: 16,
  379. ),
  380. onChanged: (text) {
  381. setState(() {
  382. // 文本更改由controller自动处理
  383. });
  384. },
  385. ),
  386. ),
  387. if (_cageController.text.isNotEmpty) ...[
  388. IconButton(
  389. icon: const Icon(Icons.refresh, size: 20),
  390. onPressed: () {
  391. _handleChangeCageCode();
  392. },
  393. ),
  394. ] else ...[
  395. IconButton(
  396. icon: _isScanningCode
  397. ? const SizedBox(
  398. width: 20,
  399. height: 20,
  400. child: CircularProgressIndicator(strokeWidth: 2),
  401. )
  402. : const Icon(Icons.qr_code_scanner, size: 20),
  403. onPressed: () {
  404. _handleScanCageCode();
  405. },
  406. ),
  407. ],
  408. ],
  409. ),
  410. ),
  411. ],
  412. );
  413. }
  414. Widget _buildElectronicIdSection() {
  415. return VberElectronicIdsField(
  416. electronicIds: _electronicIds,
  417. onIdScanned: (id) {
  418. // 检查是否已存在相同的ID标签
  419. bool isDuplicate = _electronicIds.any((v) => v == id);
  420. if (isDuplicate) {
  421. ScaffoldMessenger.of(context).showSnackBar(
  422. const SnackBar(
  423. content: Text('电子编号已存在'),
  424. backgroundColor: Colors.orange,
  425. ),
  426. );
  427. } else {
  428. // 如果不重复,添加到列表中
  429. setState(() {
  430. _electronicIds.insert(0, id);
  431. });
  432. ScaffoldMessenger.of(context).showSnackBar(
  433. const SnackBar(
  434. content: Text('成功添加新的电子编号'),
  435. backgroundColor: Colors.green,
  436. ),
  437. );
  438. }
  439. },
  440. multiple: true,
  441. multipleScan: false,
  442. label: '电子编号',
  443. multiplePlaceholder: '未识别',
  444. multipleFormat: '已识别 %d 枚电子编号',
  445. );
  446. }
  447. // void _dealyScanCode(int scanTag, {int? dealy}) async {
  448. // dealy ??= 800;
  449. // _scanTag = scanTag;
  450. // logger.d("开始扫码: $_scanTag");
  451. // await Future.delayed(Duration(milliseconds: dealy));
  452. // bool success = await ScanChannel.startScan();
  453. // if (!success) {
  454. // ToastUtil.error("扫码失败");
  455. // }
  456. // }
  457. // void _handleScanCageCode(int tag) async {
  458. // if (_isScanningTarget || _isScanningSource) return;
  459. // setState(() {
  460. // if (tag == 1) {
  461. // _isScanningSource = true;
  462. // } else if (tag == 2) {
  463. // _isScanningTarget = true;
  464. // }
  465. // });
  466. // _scanTag = tag;
  467. // logger.d("开始扫码: $_scanTag");
  468. // bool success = await ScanChannel.startScan();
  469. // if (!success) {
  470. // ToastUtil.error("扫码失败");
  471. // }
  472. // }
  473. // void _handleChangeCageCode(int tag) {
  474. // setState(() {
  475. // if (tag == 1) {
  476. // _sourceCageController.clear();
  477. // } else {
  478. // _targetCageController.clear();
  479. // }
  480. // });
  481. // _dealyScanCode(tag);
  482. // }
  483. void _handleScanCageCode() async {
  484. if (_isScanningCode) return;
  485. setState(() {
  486. _isScanningCode = true;
  487. });
  488. bool success = await ScanChannel.startScan();
  489. if (!success) {
  490. ToastUtil.error("扫码失败");
  491. }
  492. }
  493. void _handleChangeCageCode() async {
  494. if (!await ScanChannel.isScanHeadOpened()) {
  495. logger.w("扫描头未打开,正在重新打开");
  496. await ScanChannel.openScanHead().then((success) async {
  497. if (success) {
  498. success = await ScanChannel.startScan();
  499. if (!success) {
  500. ToastUtil.error("扫码失败");
  501. }
  502. } else {
  503. ToastUtil.errorAlert("扫描头打开失败");
  504. if (mounted) {
  505. context.pop();
  506. }
  507. }
  508. });
  509. } else {
  510. bool success = await ScanChannel.startScan();
  511. if (!success) {
  512. ToastUtil.error("扫码失败");
  513. }
  514. }
  515. }
  516. // 移除指定索引的电子编号
  517. void _removeElectronicId(int index) {
  518. setState(() {
  519. _electronicIds.removeAt(index);
  520. });
  521. }
  522. // 清空所有已识别的电子编号
  523. void _clearElectronicIds() {
  524. setState(() {
  525. _electronicIds.clear();
  526. });
  527. ToastUtil.info('已清空所有电子编号');
  528. }
  529. // 提交数据
  530. void _handleSubmit() {
  531. final data = {
  532. 'targetCage': _cageController.text,
  533. 'electronicIds': _electronicIds,
  534. 'date': DateTimeUtil.format(DateTime.now()),
  535. };
  536. apis.breeding.submitApi.cageChange(data).then((res) {
  537. if (res.success) {
  538. if (res.message.isNotEmpty) {
  539. ToastUtil.success(res.message);
  540. }
  541. if (mounted) {
  542. ScaffoldMessenger.of(context).showSnackBar(
  543. const SnackBar(
  544. content: Text('换笼提交成功'),
  545. backgroundColor: Colors.green,
  546. ),
  547. );
  548. // 提交后重置表单
  549. setState(() {
  550. // _sourceCageController.clear();
  551. // _targetCageController.clear();
  552. _cageController.clear();
  553. _electronicIds.clear();
  554. });
  555. }
  556. } else {
  557. ToastUtil.error('换笼提交失败');
  558. if (mounted && res.message.isNotEmpty) {
  559. logger.e(res.message);
  560. ScaffoldMessenger.of(context).showSnackBar(
  561. SnackBar(content: Text(res.message), backgroundColor: Colors.red),
  562. );
  563. }
  564. }
  565. });
  566. }
  567. }