batch_culling_page.dart 8.1 KB

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