cage_change_page.dart 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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_rfid_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> _rfids = [];
  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. _buildRfidSection(),
  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 && _rfids.isNotEmpty
  181. ? _handleSubmit
  182. : null,
  183. style: ElevatedButton.styleFrom(
  184. backgroundColor:
  185. (_cageController.text.isNotEmpty && _rfids.isNotEmpty)
  186. ? Colors.blue
  187. : Colors.grey,
  188. foregroundColor: Colors.white,
  189. ),
  190. child: const Text('提交'),
  191. ),
  192. ),
  193. const SizedBox(height: 20),
  194. // 已识别的电子编号列表
  195. if (_rfids.isNotEmpty) ...[
  196. Row(
  197. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  198. children: [
  199. const Text(
  200. '已识别的电子编号',
  201. style: TextStyle(fontWeight: FontWeight.bold),
  202. ),
  203. IconButton(
  204. icon: const Icon(Icons.clear, size: 18),
  205. onPressed: _clearRfids,
  206. tooltip: '清空编号',
  207. ),
  208. ],
  209. ),
  210. const SizedBox(height: 10),
  211. Container(
  212. padding: const EdgeInsets.all(10),
  213. decoration: BoxDecoration(
  214. border: Border.all(color: Colors.grey),
  215. borderRadius: BorderRadius.circular(8),
  216. ),
  217. child: ListView.builder(
  218. shrinkWrap: true,
  219. physics: const NeverScrollableScrollPhysics(),
  220. padding: EdgeInsets.zero,
  221. itemCount: _rfids.length,
  222. itemBuilder: (context, index) {
  223. return ListTile(
  224. visualDensity: VisualDensity.compact,
  225. contentPadding: const EdgeInsets.symmetric(
  226. horizontal: 2,
  227. vertical: 0,
  228. ),
  229. title: Text(_rfids[index]),
  230. trailing: IconButton(
  231. icon: const Icon(
  232. Icons.delete,
  233. size: 18,
  234. color: Colors.red,
  235. ),
  236. onPressed: () => _removeRfid(index),
  237. ),
  238. );
  239. },
  240. ),
  241. ),
  242. ],
  243. const SizedBox(height: 20),
  244. ],
  245. ),
  246. ),
  247. ),
  248. );
  249. }
  250. // Widget _buildCageSection(int tag) {
  251. // String title = '源笼号';
  252. // TextEditingController controller = _sourceCageController;
  253. // FocusNode focusNode = _sourceCageFocusNode;
  254. // bool isScanning = _isScanningSource;
  255. // if (tag != 1) {
  256. // tag = 2;
  257. // title = '目标笼号';
  258. // controller = _targetCageController;
  259. // focusNode = _targetCageFocusNode;
  260. // isScanning = _isScanningTarget;
  261. // }
  262. // // 确保光标在文本末尾
  263. // if (controller.text.isNotEmpty) {
  264. // controller.selection = TextSelection.fromPosition(
  265. // TextPosition(offset: controller.text.length),
  266. // );
  267. // }
  268. // return Column(
  269. // crossAxisAlignment: CrossAxisAlignment.start,
  270. // children: [
  271. // Text(title, style: const TextStyle(fontWeight: FontWeight.bold)),
  272. // const SizedBox(height: 10),
  273. // Container(
  274. // padding: const EdgeInsets.fromLTRB(16, 2, 16, 2),
  275. // decoration: BoxDecoration(
  276. // border: Border.all(color: Colors.grey),
  277. // borderRadius: BorderRadius.circular(8),
  278. // ),
  279. // child: Row(
  280. // children: [
  281. // Expanded(
  282. // // 将Text替换为TextField
  283. // child: TextField(
  284. // focusNode: focusNode,
  285. // controller: controller,
  286. // enabled: controller.text.isEmpty,
  287. // decoration: InputDecoration(
  288. // border: InputBorder.none,
  289. // hintText: controller.text.isNotEmpty ? null : '未扫描',
  290. // hintStyle: TextStyle(
  291. // color: controller.text.isNotEmpty
  292. // ? Colors.black
  293. // : Colors.grey,
  294. // fontSize: 16,
  295. // ),
  296. // ),
  297. // style: TextStyle(
  298. // color: controller.text.isNotEmpty
  299. // ? Colors.black
  300. // : Colors.grey,
  301. // fontSize: 16,
  302. // ),
  303. // onChanged: (text) {
  304. // setState(() {
  305. // // 文本更改由controller自动处理
  306. // });
  307. // },
  308. // ),
  309. // ),
  310. // if (controller.text.isNotEmpty) ...[
  311. // IconButton(
  312. // icon: const Icon(Icons.refresh, size: 20),
  313. // onPressed: () {
  314. // _handleChangeCageCode(tag);
  315. // },
  316. // ),
  317. // ] else ...[
  318. // IconButton(
  319. // icon: isScanning
  320. // ? const SizedBox(
  321. // width: 20,
  322. // height: 20,
  323. // child: CircularProgressIndicator(strokeWidth: 2),
  324. // )
  325. // : const Icon(Icons.qr_code_scanner, size: 20),
  326. // onPressed: () {
  327. // _handleScanCageCode(tag);
  328. // },
  329. // ),
  330. // ],
  331. // ],
  332. // ),
  333. // ),
  334. // ],
  335. // );
  336. // }
  337. Widget _buildCageSection() {
  338. // 确保光标在文本末尾
  339. if (_cageController.text.isNotEmpty) {
  340. _cageController.selection = TextSelection.fromPosition(
  341. TextPosition(offset: _cageController.text.length),
  342. );
  343. }
  344. return Column(
  345. crossAxisAlignment: CrossAxisAlignment.start,
  346. children: [
  347. Text('目标笼号', style: const TextStyle(fontWeight: FontWeight.bold)),
  348. const SizedBox(height: 10),
  349. Container(
  350. padding: const EdgeInsets.fromLTRB(16, 2, 16, 2),
  351. decoration: BoxDecoration(
  352. border: Border.all(color: Colors.grey),
  353. borderRadius: BorderRadius.circular(8),
  354. ),
  355. child: Row(
  356. children: [
  357. Expanded(
  358. // 将Text替换为TextField
  359. child: TextField(
  360. controller: _cageController,
  361. enabled: _cageController.text.isEmpty,
  362. decoration: InputDecoration(
  363. border: InputBorder.none,
  364. hintText: _cageController.text.isNotEmpty ? null : '未扫描',
  365. hintStyle: TextStyle(
  366. color: _cageController.text.isNotEmpty
  367. ? Colors.black
  368. : Colors.grey,
  369. fontSize: 16,
  370. ),
  371. ),
  372. style: TextStyle(
  373. color: _cageController.text.isNotEmpty
  374. ? Colors.black
  375. : Colors.grey,
  376. fontSize: 16,
  377. ),
  378. onChanged: (text) {
  379. setState(() {
  380. // 文本更改由controller自动处理
  381. });
  382. },
  383. ),
  384. ),
  385. if (_cageController.text.isNotEmpty) ...[
  386. IconButton(
  387. icon: const Icon(Icons.refresh, size: 20),
  388. onPressed: () {
  389. _handleChangeCageCode();
  390. },
  391. ),
  392. ] else ...[
  393. IconButton(
  394. icon: _isScanningCode
  395. ? const SizedBox(
  396. width: 20,
  397. height: 20,
  398. child: CircularProgressIndicator(strokeWidth: 2),
  399. )
  400. : const Icon(Icons.qr_code_scanner, size: 20),
  401. onPressed: () {
  402. _handleScanCageCode();
  403. },
  404. ),
  405. ],
  406. ],
  407. ),
  408. ),
  409. ],
  410. );
  411. }
  412. Widget _buildRfidSection() {
  413. return VberRfidField(
  414. rfids: _rfids,
  415. onRfidScanned: (rfid) {
  416. // 检查是否已存在相同的RFID标签
  417. bool isDuplicate = _rfids.any((existingRfid) => existingRfid == rfid);
  418. if (isDuplicate) {
  419. ScaffoldMessenger.of(context).showSnackBar(
  420. const SnackBar(
  421. content: Text('电子编号已存在'),
  422. backgroundColor: Colors.orange,
  423. ),
  424. );
  425. } else {
  426. // 如果不重复,添加到列表中
  427. setState(() {
  428. _rfids.insert(0, rfid);
  429. });
  430. ScaffoldMessenger.of(context).showSnackBar(
  431. const SnackBar(
  432. content: Text('成功添加新的电子编号'),
  433. backgroundColor: Colors.green,
  434. ),
  435. );
  436. }
  437. },
  438. multiple: true,
  439. multipleScan: false,
  440. label: '电子编号',
  441. multiplePlaceholder: '未识别',
  442. multipleFormat: '已识别 %d 枚电子编号',
  443. );
  444. }
  445. // void _dealyScanCode(int scanTag, {int? dealy}) async {
  446. // dealy ??= 800;
  447. // _scanTag = scanTag;
  448. // logger.d("开始扫码: $_scanTag");
  449. // await Future.delayed(Duration(milliseconds: dealy));
  450. // bool success = await ScanChannel.startScan();
  451. // if (!success) {
  452. // ToastUtil.error("扫码失败");
  453. // }
  454. // }
  455. // void _handleScanCageCode(int tag) async {
  456. // if (_isScanningTarget || _isScanningSource) return;
  457. // setState(() {
  458. // if (tag == 1) {
  459. // _isScanningSource = true;
  460. // } else if (tag == 2) {
  461. // _isScanningTarget = true;
  462. // }
  463. // });
  464. // _scanTag = tag;
  465. // logger.d("开始扫码: $_scanTag");
  466. // bool success = await ScanChannel.startScan();
  467. // if (!success) {
  468. // ToastUtil.error("扫码失败");
  469. // }
  470. // }
  471. // void _handleChangeCageCode(int tag) {
  472. // setState(() {
  473. // if (tag == 1) {
  474. // _sourceCageController.clear();
  475. // } else {
  476. // _targetCageController.clear();
  477. // }
  478. // });
  479. // _dealyScanCode(tag);
  480. // }
  481. void _dealyScanCode({int? dealy}) async {
  482. dealy ??= 600;
  483. await Future.delayed(Duration(milliseconds: dealy));
  484. bool success = await ScanChannel.startScan();
  485. if (!success) {
  486. ToastUtil.error("扫码失败");
  487. }
  488. }
  489. void _handleScanCageCode() async {
  490. if (_isScanningCode) return;
  491. setState(() {
  492. _isScanningCode = true;
  493. });
  494. bool success = await ScanChannel.startScan();
  495. if (!success) {
  496. ToastUtil.error("扫码失败");
  497. }
  498. }
  499. void _handleChangeCageCode() {
  500. _dealyScanCode();
  501. }
  502. // 移除指定索引的电子编号
  503. void _removeRfid(int index) {
  504. setState(() {
  505. _rfids.removeAt(index);
  506. });
  507. }
  508. // 清空所有已识别的电子编号
  509. void _clearRfids() {
  510. setState(() {
  511. _rfids.clear();
  512. });
  513. ToastUtil.info('已清空所有电子编号');
  514. }
  515. // 提交数据
  516. void _handleSubmit() {
  517. // final data = {
  518. // 'sourceCage': _sourceCageController.text,
  519. // 'targetCage': _targetCageController.text,
  520. // 'rfids': _rfids,
  521. // 'date': DateTimeUtil.format(DateTime.now()),
  522. // };
  523. final data = {
  524. 'targetCage': _cageController.text,
  525. 'rfids': _rfids,
  526. 'date': DateTimeUtil.format(DateTime.now()),
  527. };
  528. apis.breeding.submitApi.cageChange(data).then((res) {
  529. if (res.success) {
  530. if (res.message.isNotEmpty) {
  531. ToastUtil.success(res.message);
  532. }
  533. if (mounted) {
  534. ScaffoldMessenger.of(context).showSnackBar(
  535. const SnackBar(
  536. content: Text('换笼提交成功'),
  537. backgroundColor: Colors.green,
  538. ),
  539. );
  540. // 提交后重置表单
  541. setState(() {
  542. // _sourceCageController.clear();
  543. // _targetCageController.clear();
  544. _cageController.clear();
  545. _rfids.clear();
  546. });
  547. }
  548. } else {
  549. ToastUtil.error('换笼提交失败');
  550. if (mounted && res.message.isNotEmpty) {
  551. logger.e(res.message);
  552. ScaffoldMessenger.of(context).showSnackBar(
  553. SnackBar(content: Text(res.message), backgroundColor: Colors.red),
  554. );
  555. }
  556. }
  557. });
  558. }
  559. }