batch_culling_page.dart 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. import 'package:chicken_farm/apis/index.dart';
  2. import 'package:chicken_farm/components/vb_electronic_id_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> _electronicIds = [];
  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: SingleChildScrollView(
  24. padding: const EdgeInsets.all(16.0),
  25. child: Column(
  26. crossAxisAlignment: CrossAxisAlignment.start,
  27. children: [
  28. // 电子编号区域
  29. _buildElectronicIdSection(),
  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. _electronicIds.isNotEmpty &&
  43. _cullReason != null &&
  44. _disposalMethod != null
  45. ? _handleSubmit
  46. : null,
  47. style: ElevatedButton.styleFrom(
  48. backgroundColor:
  49. _electronicIds.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 (_electronicIds.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. Container(
  78. height: 200, // 固定高度以支持滚动
  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: _electronicIds.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(_electronicIds[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. const SizedBox(height: 20),
  109. ],
  110. ),
  111. ),
  112. );
  113. }
  114. Widget _buildElectronicIdSection() {
  115. return VberElectronicIdsField(
  116. electronicIds: _electronicIds,
  117. onIdsScanned: (List<RfidModel> idList) {
  118. // 过滤出未存在的ID
  119. final ids = idList
  120. .where((id) => !_electronicIds.contains(id.uid))
  121. .toList();
  122. if (ids.isNotEmpty) {
  123. setState(() {
  124. // 将新的ID添加到列表中
  125. for (var id in ids) {
  126. _electronicIds.insert(0, id.uid);
  127. }
  128. });
  129. // ToastUtil.success("新增 ${newRfids.length} 枚电子编号");
  130. ScaffoldMessenger.of(context).showSnackBar(
  131. SnackBar(
  132. content: Text("新增 ${ids.length} 枚电子编号"),
  133. backgroundColor: Colors.green,
  134. ),
  135. );
  136. } else {
  137. // 所有RFID都已存在
  138. // ToastUtil.info("电子编号已存在");
  139. ScaffoldMessenger.of(context).showSnackBar(
  140. const SnackBar(
  141. content: Text('电子编号已存在'),
  142. backgroundColor: Colors.orange,
  143. ),
  144. );
  145. }
  146. },
  147. multiple: true,
  148. label: '电子编号',
  149. multiplePlaceholder: '未识别',
  150. multipleFormat: '已识别 %d 枚电子编号',
  151. );
  152. }
  153. Widget _buildCullReasonSection() {
  154. return Column(
  155. crossAxisAlignment: CrossAxisAlignment.start,
  156. children: [
  157. const Text('淘汰原因', style: TextStyle(fontWeight: FontWeight.bold)),
  158. const SizedBox(height: 10),
  159. Container(
  160. padding: const EdgeInsets.fromLTRB(16, 2, 16, 2),
  161. decoration: BoxDecoration(
  162. border: Border.all(color: Colors.grey),
  163. borderRadius: BorderRadius.circular(8),
  164. ),
  165. child: VberDictSelect(
  166. dictType: 'chicken_cull_reason',
  167. value: _cullReason,
  168. onChanged: (value) {
  169. setState(() {
  170. _cullReason = value;
  171. });
  172. },
  173. hint: '请选择淘汰原因',
  174. hideUnderline: true,
  175. ),
  176. ),
  177. ],
  178. );
  179. }
  180. Widget _buildDisposalMethodSection() {
  181. return Column(
  182. crossAxisAlignment: CrossAxisAlignment.start,
  183. children: [
  184. const Text('处置方式', style: TextStyle(fontWeight: FontWeight.bold)),
  185. const SizedBox(height: 10),
  186. Container(
  187. padding: const EdgeInsets.fromLTRB(16, 2, 16, 2),
  188. decoration: BoxDecoration(
  189. border: Border.all(color: Colors.grey),
  190. borderRadius: BorderRadius.circular(8),
  191. ),
  192. child: VberDictSelect(
  193. dictType: 'chicken_disposal_method',
  194. value: _disposalMethod,
  195. onChanged: (value) {
  196. setState(() {
  197. _disposalMethod = value;
  198. });
  199. },
  200. hint: '请选择处置方式',
  201. hideUnderline: true,
  202. ),
  203. ),
  204. ],
  205. );
  206. }
  207. // 移除指定索引的电子编号
  208. void _removeRfid(int index) {
  209. setState(() {
  210. _electronicIds.removeAt(index);
  211. });
  212. }
  213. // 清空所有已识别的电子编号
  214. void _clearRfids() {
  215. setState(() {
  216. _electronicIds.clear();
  217. });
  218. ToastUtil.info('已清空所有电子编号');
  219. }
  220. // 提交数据
  221. void _handleSubmit() {
  222. final data = {
  223. 'electronicIds': _electronicIds,
  224. 'disposalMethod': _disposalMethod,
  225. 'cullReason': _cullReason,
  226. 'date': DateTimeUtil.format(DateTime.now()),
  227. };
  228. apis.breeding.submitApi.weight(data).then((res) {
  229. if (res.success) {
  230. ToastUtil.success(res.message.isNotEmpty ? res.message : '批量淘汰提交成功');
  231. if (mounted) {
  232. // 提交后重置表单
  233. setState(() {
  234. _electronicIds.clear();
  235. // _cullReason = null;
  236. // _disposalMethod = null;
  237. });
  238. }
  239. } else {
  240. ToastUtil.errorAlert(res.message.isNotEmpty ? res.message : '批量淘汰提交失败');
  241. }
  242. });
  243. }
  244. }