cage_change_page.dart 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. import 'package:flutter/material.dart';
  2. import 'package:chicken_farm/components/vb_app_bar.dart';
  3. import 'package:chicken_farm/core/utils/toast.dart';
  4. class CageChangePage extends StatefulWidget {
  5. const CageChangePage({super.key});
  6. @override
  7. State<CageChangePage> createState() => _CageChangePageState();
  8. }
  9. class _CageChangePageState extends State<CageChangePage> {
  10. String? _sourceCageId;
  11. String? _targetCageId;
  12. final List<String> _rfids = [];
  13. // 模拟扫描状态
  14. bool _isScanningSource = false;
  15. bool _isScanningTarget = false;
  16. bool _isScanningRfid = false;
  17. @override
  18. Widget build(BuildContext context) {
  19. return Scaffold(
  20. appBar: const VberAppBar(title: '换笼管理', showLeftButton: true),
  21. body: Padding(
  22. padding: const EdgeInsets.all(16.0),
  23. child: Column(
  24. crossAxisAlignment: CrossAxisAlignment.start,
  25. children: [
  26. // 源笼号区域
  27. _buildCageSection(
  28. title: '源笼号',
  29. value: _sourceCageId,
  30. isScanning: _isScanningSource,
  31. onScanPressed: _simulateScanSourceCage,
  32. onChangePressed: _handleChangeSourceCage,
  33. ),
  34. const SizedBox(height: 15),
  35. // 目标笼号区域
  36. _buildCageSection(
  37. title: '目标笼号',
  38. value: _targetCageId,
  39. isScanning: _isScanningTarget,
  40. onScanPressed: _simulateScanTargetCage,
  41. onChangePressed: _handleChangeTargetCage,
  42. ),
  43. const SizedBox(height: 15),
  44. // 电子编号区域
  45. _buildRfidSection(),
  46. const SizedBox(height: 15),
  47. // 提交按钮
  48. SizedBox(
  49. width: double.infinity,
  50. child: ElevatedButton(
  51. onPressed:
  52. _sourceCageId != null &&
  53. _targetCageId != null &&
  54. _rfids.isNotEmpty
  55. ? _handleSubmit
  56. : null,
  57. style: ElevatedButton.styleFrom(
  58. backgroundColor:
  59. (_sourceCageId != null &&
  60. _targetCageId != null &&
  61. _rfids.isNotEmpty)
  62. ? Colors.blue
  63. : Colors.grey,
  64. foregroundColor: Colors.white,
  65. ),
  66. child: const Text('提交'),
  67. ),
  68. ),
  69. const SizedBox(height: 20),
  70. // 已扫描的电子编号列表
  71. if (_rfids.isNotEmpty) ...[
  72. const Text(
  73. '已扫描的电子编号',
  74. style: TextStyle(fontWeight: FontWeight.bold),
  75. ),
  76. const SizedBox(height: 10),
  77. Expanded(
  78. child: Container(
  79. padding: const EdgeInsets.all(10),
  80. decoration: BoxDecoration(
  81. border: Border.all(color: Colors.grey),
  82. borderRadius: BorderRadius.circular(8),
  83. ),
  84. child: ListView.builder(
  85. padding: EdgeInsets.zero,
  86. itemCount: _rfids.length,
  87. itemBuilder: (context, index) {
  88. return ListTile(
  89. visualDensity: VisualDensity.compact,
  90. contentPadding: const EdgeInsets.symmetric(
  91. horizontal: 2,
  92. vertical: 0,
  93. ),
  94. title: Text(_rfids[index]),
  95. trailing: IconButton(
  96. icon: const Icon(
  97. Icons.delete,
  98. size: 18,
  99. color: Colors.red,
  100. ),
  101. onPressed: () => _removeRfid(index),
  102. ),
  103. );
  104. },
  105. ),
  106. ),
  107. ),
  108. ],
  109. const SizedBox(height: 20),
  110. ],
  111. ),
  112. ),
  113. );
  114. }
  115. Widget _buildCageSection({
  116. required String title,
  117. String? value,
  118. required bool isScanning,
  119. required VoidCallback onScanPressed,
  120. required VoidCallback onChangePressed,
  121. }) {
  122. return Column(
  123. crossAxisAlignment: CrossAxisAlignment.start,
  124. children: [
  125. Text(title, style: const TextStyle(fontWeight: FontWeight.bold)),
  126. const SizedBox(height: 10),
  127. Container(
  128. padding: const EdgeInsets.fromLTRB(16, 2, 16, 2),
  129. decoration: BoxDecoration(
  130. border: Border.all(color: Colors.grey),
  131. borderRadius: BorderRadius.circular(8),
  132. ),
  133. child: Row(
  134. children: [
  135. Expanded(
  136. child: Text(
  137. value ?? '未扫描',
  138. style: TextStyle(
  139. color: value != null ? Colors.black : Colors.grey,
  140. fontSize: 16,
  141. ),
  142. ),
  143. ),
  144. if (value != null) ...[
  145. IconButton(
  146. icon: const Icon(Icons.refresh, size: 20),
  147. onPressed: onChangePressed,
  148. ),
  149. ] else ...[
  150. IconButton(
  151. icon: isScanning
  152. ? const SizedBox(
  153. width: 20,
  154. height: 20,
  155. child: CircularProgressIndicator(strokeWidth: 2),
  156. )
  157. : const Icon(Icons.qr_code_scanner, size: 20),
  158. onPressed: onScanPressed,
  159. ),
  160. ],
  161. ],
  162. ),
  163. ),
  164. ],
  165. );
  166. }
  167. Widget _buildRfidSection() {
  168. return Column(
  169. crossAxisAlignment: CrossAxisAlignment.start,
  170. children: [
  171. const Text('鸡数量', style: TextStyle(fontWeight: FontWeight.bold)),
  172. const SizedBox(height: 10),
  173. Container(
  174. padding: const EdgeInsets.fromLTRB(16, 2, 16, 2),
  175. decoration: BoxDecoration(
  176. border: Border.all(color: Colors.grey),
  177. borderRadius: BorderRadius.circular(8),
  178. ),
  179. child: Row(
  180. children: [
  181. Expanded(
  182. child: Text(
  183. _rfids.isEmpty ? '未扫描' : '已扫描 ${_rfids.length} 只鸡',
  184. style: TextStyle(
  185. color: _rfids.isNotEmpty ? Colors.black : Colors.grey,
  186. fontSize: 16,
  187. ),
  188. ),
  189. ),
  190. IconButton(
  191. icon: _isScanningRfid
  192. ? const SizedBox(
  193. width: 20,
  194. height: 20,
  195. child: CircularProgressIndicator(strokeWidth: 2),
  196. )
  197. : const Icon(Icons.add, size: 20),
  198. onPressed: _simulateScanRfid,
  199. ),
  200. ],
  201. ),
  202. ),
  203. ],
  204. );
  205. }
  206. // 模拟扫描源笼号
  207. void _simulateScanSourceCage() {
  208. if (_isScanningSource) return;
  209. setState(() {
  210. _isScanningSource = true;
  211. });
  212. // 模拟扫描过程
  213. Future.delayed(const Duration(seconds: 1), () {
  214. if (mounted) {
  215. setState(() {
  216. _sourceCageId = 'SC-2025-001'; // 模拟扫描结果
  217. _isScanningSource = false;
  218. });
  219. ToastUtil.success('源笼号扫描成功');
  220. }
  221. });
  222. }
  223. // 模拟扫描目标笼号
  224. void _simulateScanTargetCage() {
  225. if (_isScanningTarget) return;
  226. setState(() {
  227. _isScanningTarget = true;
  228. });
  229. // 模拟扫描过程
  230. Future.delayed(const Duration(seconds: 1), () {
  231. if (mounted) {
  232. setState(() {
  233. _targetCageId = 'TC-2025-008'; // 模拟扫描结果
  234. _isScanningTarget = false;
  235. });
  236. ToastUtil.success('目标笼号扫描成功');
  237. }
  238. });
  239. }
  240. // 模拟扫描电子编号
  241. void _simulateScanRfid() {
  242. if (_isScanningRfid) return;
  243. setState(() {
  244. _isScanningRfid = true;
  245. });
  246. // 模拟扫描过程
  247. Future.delayed(const Duration(seconds: 1), () {
  248. if (mounted) {
  249. setState(() {
  250. // 每次添加一个新的电子编号到数组开头(模拟按一次实体按钮扫描一个电子编号)
  251. _rfids.insert(0, 'RFID-${DateTime.now().millisecondsSinceEpoch}');
  252. _isScanningRfid = false;
  253. });
  254. ToastUtil.success('鸡扫描成功');
  255. }
  256. });
  257. }
  258. // 重新扫描源笼号
  259. void _handleChangeSourceCage() {
  260. setState(() {
  261. _sourceCageId = null;
  262. });
  263. }
  264. // 重新扫描目标笼号
  265. void _handleChangeTargetCage() {
  266. setState(() {
  267. _targetCageId = null;
  268. });
  269. }
  270. // 移除指定索引的电子编号
  271. void _removeRfid(int index) {
  272. setState(() {
  273. _rfids.removeAt(index);
  274. });
  275. }
  276. // 提交数据
  277. void _handleSubmit() {
  278. // 在实际应用中,这里会发送数据到服务器
  279. ScaffoldMessenger.of(context).showSnackBar(
  280. const SnackBar(content: Text('换笼操作提交成功'), backgroundColor: Colors.green),
  281. );
  282. // 提交后重置表单
  283. setState(() {
  284. _sourceCageId = null;
  285. _targetCageId = null;
  286. _rfids.clear();
  287. });
  288. }
  289. }