batch_culling_page.dart 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. import 'package:flutter/material.dart';
  2. import 'package:chicken_farm/components/vb_app_bar.dart';
  3. import 'package:chicken_farm/components/vb_dict_select.dart';
  4. import 'package:chicken_farm/core/utils/toast.dart';
  5. class BatchCullingPage extends StatefulWidget {
  6. const BatchCullingPage({super.key});
  7. @override
  8. State<BatchCullingPage> createState() => _BatchCullingPageState();
  9. }
  10. class _BatchCullingPageState extends State<BatchCullingPage> {
  11. final List<String> _rfids = [];
  12. String? _cullReason;
  13. String? _disposalMethod;
  14. bool _isScanningRfid = false;
  15. @override
  16. Widget build(BuildContext context) {
  17. return Scaffold(
  18. appBar: const VberAppBar(title: '批量淘汰', showLeftButton: true),
  19. body: Padding(
  20. padding: const EdgeInsets.all(16.0),
  21. child: Column(
  22. crossAxisAlignment: CrossAxisAlignment.start,
  23. children: [
  24. // 电子编号区域
  25. _buildRfidSection(),
  26. const SizedBox(height: 20),
  27. // 淘汰原因
  28. _buildCullReasonSection(),
  29. const SizedBox(height: 20),
  30. // 处置方式
  31. _buildDisposalMethodSection(),
  32. const SizedBox(height: 20),
  33. // 提交按钮
  34. SizedBox(
  35. width: double.infinity,
  36. child: ElevatedButton(
  37. onPressed: _rfids.isNotEmpty && _disposalMethod != null
  38. ? _handleSubmit
  39. : null,
  40. style: ElevatedButton.styleFrom(
  41. backgroundColor: _rfids.isNotEmpty && _disposalMethod != null
  42. ? Colors.blue
  43. : Colors.grey,
  44. foregroundColor: Colors.white,
  45. ),
  46. child: const Text('提交'),
  47. ),
  48. ),
  49. const SizedBox(height: 20),
  50. // 已扫描的电子编号列表
  51. if (_rfids.isNotEmpty) ...[
  52. const Text(
  53. '已扫描的电子编号',
  54. style: TextStyle(fontWeight: FontWeight.bold),
  55. ),
  56. const SizedBox(height: 10),
  57. Expanded(
  58. child: Container(
  59. padding: const EdgeInsets.all(10),
  60. decoration: BoxDecoration(
  61. border: Border.all(color: Colors.grey),
  62. borderRadius: BorderRadius.circular(8),
  63. ),
  64. child: ListView.builder(
  65. padding: EdgeInsets.zero,
  66. itemCount: _rfids.length,
  67. itemBuilder: (context, index) {
  68. return ListTile(
  69. visualDensity: VisualDensity.compact,
  70. contentPadding: const EdgeInsets.symmetric(
  71. horizontal: 2,
  72. vertical: 0,
  73. ),
  74. title: Text(_rfids[index]),
  75. trailing: IconButton(
  76. icon: const Icon(
  77. Icons.delete,
  78. size: 18,
  79. color: Colors.red,
  80. ),
  81. onPressed: () => _removeRfid(index),
  82. ),
  83. );
  84. },
  85. ),
  86. ),
  87. ),
  88. ],
  89. const SizedBox(height: 20),
  90. ],
  91. ),
  92. ),
  93. );
  94. }
  95. Widget _buildRfidSection() {
  96. return Column(
  97. crossAxisAlignment: CrossAxisAlignment.start,
  98. children: [
  99. const Text('电子编号', style: TextStyle(fontWeight: FontWeight.bold)),
  100. const SizedBox(height: 10),
  101. Container(
  102. padding: const EdgeInsets.fromLTRB(16, 2, 16, 2),
  103. decoration: BoxDecoration(
  104. border: Border.all(color: Colors.grey),
  105. borderRadius: BorderRadius.circular(8),
  106. ),
  107. child: Row(
  108. children: [
  109. Expanded(
  110. child: Text(
  111. _rfids.isEmpty ? '未扫描' : '已扫描 ${_rfids.length} 只鸡',
  112. style: TextStyle(
  113. color: _rfids.isNotEmpty ? Colors.black : Colors.grey,
  114. fontSize: 16,
  115. ),
  116. ),
  117. ),
  118. // 根据是否有已扫描的RFID决定显示什么按钮
  119. if (_rfids.isEmpty) ...[
  120. IconButton(
  121. icon: _isScanningRfid
  122. ? const SizedBox(
  123. width: 20,
  124. height: 20,
  125. child: CircularProgressIndicator(strokeWidth: 2),
  126. )
  127. : const Icon(Icons.qr_code_scanner, size: 20),
  128. onPressed: _simulateScanMultipleRfids,
  129. ),
  130. ] else ...[
  131. TextButton(onPressed: _clearRfids, child: const Text('清空编号')),
  132. ],
  133. ],
  134. ),
  135. ),
  136. ],
  137. );
  138. }
  139. Widget _buildCullReasonSection() {
  140. return Column(
  141. crossAxisAlignment: CrossAxisAlignment.start,
  142. children: [
  143. const Text('淘汰原因', style: TextStyle(fontWeight: FontWeight.bold)),
  144. const SizedBox(height: 10),
  145. Container(
  146. padding: const EdgeInsets.fromLTRB(16, 2, 16, 2),
  147. decoration: BoxDecoration(
  148. border: Border.all(color: Colors.grey),
  149. borderRadius: BorderRadius.circular(8),
  150. ),
  151. child: VberDictSelect(
  152. dictType: 'chicken_cull_reason',
  153. value: _cullReason,
  154. onChanged: (value) {
  155. setState(() {
  156. _cullReason = value;
  157. });
  158. },
  159. hint: '请选择淘汰原因',
  160. hideUnderline: true,
  161. ),
  162. ),
  163. ],
  164. );
  165. }
  166. Widget _buildDisposalMethodSection() {
  167. return Column(
  168. crossAxisAlignment: CrossAxisAlignment.start,
  169. children: [
  170. const Text('处置方式', style: TextStyle(fontWeight: FontWeight.bold)),
  171. const SizedBox(height: 10),
  172. Container(
  173. padding: const EdgeInsets.fromLTRB(16, 2, 16, 2),
  174. decoration: BoxDecoration(
  175. border: Border.all(color: Colors.grey),
  176. borderRadius: BorderRadius.circular(8),
  177. ),
  178. child: VberDictSelect(
  179. dictType: 'chicken_disposal_method',
  180. value: _disposalMethod,
  181. onChanged: (value) {
  182. setState(() {
  183. _disposalMethod = value;
  184. });
  185. },
  186. hint: '请选择处置方式',
  187. hideUnderline: true,
  188. ),
  189. ),
  190. ],
  191. );
  192. }
  193. // 模拟一次性扫描多个电子编号
  194. void _simulateScanMultipleRfids() {
  195. if (_isScanningRfid) return;
  196. setState(() {
  197. _isScanningRfid = true;
  198. });
  199. // 模拟扫描过程
  200. Future.delayed(const Duration(seconds: 1), () {
  201. if (mounted) {
  202. setState(() {
  203. // 一次性添加多个电子编号(模拟实际情况)
  204. _rfids.clear(); // 确保是新的扫描结果
  205. final now = DateTime.now().millisecondsSinceEpoch;
  206. for (int i = 0; i < 5; i++) {
  207. _rfids.add('RFID-${now + i}');
  208. }
  209. _isScanningRfid = false;
  210. });
  211. ToastUtil.success('成功扫描${_rfids.length}个电子编号');
  212. }
  213. });
  214. }
  215. // 清空所有已扫描的电子编号
  216. void _clearRfids() {
  217. setState(() {
  218. _rfids.clear();
  219. });
  220. ToastUtil.info('已清空所有电子编号');
  221. }
  222. // 移除指定索引的电子编号
  223. void _removeRfid(int index) {
  224. setState(() {
  225. _rfids.removeAt(index);
  226. });
  227. }
  228. // 提交数据
  229. void _handleSubmit() {
  230. // 在实际应用中,这里会发送数据到服务器
  231. ScaffoldMessenger.of(context).showSnackBar(
  232. SnackBar(
  233. content: Text('批量淘汰提交成功,共${_rfids.length}只鸡'),
  234. backgroundColor: Colors.green,
  235. ),
  236. );
  237. // 提交后重置表单
  238. setState(() {
  239. _rfids.clear();
  240. _disposalMethod = null;
  241. });
  242. }
  243. }