batch_culling_page.dart 7.2 KB

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