batch_culling_page.dart 8.5 KB

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