batch_culling_page.dart 8.1 KB

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