batch_create_page.dart 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. import 'package:chicken_farm/core/config/app_config.dart';
  2. import 'package:chicken_farm/core/utils/datetime_util.dart';
  3. import 'package:chicken_farm/core/utils/logger.dart';
  4. import 'package:chicken_farm/apis/index.dart';
  5. import 'package:chicken_farm/components/vb_electronic_id_field.dart';
  6. import 'package:chicken_farm/components/vb_search_select.dart';
  7. import 'package:chicken_farm/components/vb_select.dart';
  8. import 'package:chicken_farm/modes/breeding/batch.dart';
  9. import 'package:chicken_farm/modes/breeding/family.dart';
  10. import 'package:chicken_farm/modes/rfid/rfid_model.dart';
  11. import 'package:flutter/material.dart';
  12. import 'package:chicken_farm/components/vb_app_bar.dart';
  13. import 'package:chicken_farm/core/utils/toast.dart';
  14. class BatchCreatePage extends StatefulWidget {
  15. const BatchCreatePage({super.key});
  16. @override
  17. State<BatchCreatePage> createState() => _BatchCreatePageState();
  18. }
  19. class _BatchCreatePageState extends State<BatchCreatePage> {
  20. final List<String> _electronicIds = [];
  21. String? _batchNum;
  22. String? _familyNum;
  23. @override
  24. Widget build(BuildContext context) {
  25. return Scaffold(
  26. appBar: const VberAppBar(title: '个体绑定', showLeftButton: true),
  27. body: SingleChildScrollView(
  28. padding: const EdgeInsets.all(16.0),
  29. child: Column(
  30. crossAxisAlignment: CrossAxisAlignment.start,
  31. children: [
  32. // 批次选择
  33. _buildBatchInput(),
  34. const SizedBox(height: 10),
  35. // 家系号选择
  36. _buildFamilyInput(),
  37. const SizedBox(height: 20),
  38. // 电子编号区域
  39. _buildElectronicIdsSection(),
  40. const SizedBox(height: 20),
  41. // 提交按钮
  42. SizedBox(
  43. width: double.infinity,
  44. child: ElevatedButton(
  45. onPressed:
  46. _electronicIds.isNotEmpty &&
  47. _batchNum != null &&
  48. _familyNum != null
  49. ? _handleSubmit
  50. : null,
  51. style: ElevatedButton.styleFrom(
  52. backgroundColor:
  53. _electronicIds.isNotEmpty &&
  54. _batchNum != null &&
  55. _familyNum != null
  56. ? Colors.blue
  57. : Colors.grey,
  58. foregroundColor: Colors.white,
  59. ),
  60. child: const Text('提交'),
  61. ),
  62. ),
  63. const SizedBox(height: 20),
  64. // 已识别的电子编号列表
  65. if (_electronicIds.isNotEmpty) ...[
  66. Row(
  67. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  68. children: [
  69. const Text(
  70. '已识别的电子编号',
  71. style: TextStyle(fontWeight: FontWeight.bold),
  72. ),
  73. IconButton(
  74. icon: const Icon(Icons.clear, size: 18),
  75. onPressed: _clearRfids,
  76. tooltip: '清空编号',
  77. ),
  78. ],
  79. ),
  80. const SizedBox(height: 10),
  81. Container(
  82. height: 200, // 固定高度以支持滚动
  83. padding: const EdgeInsets.all(10),
  84. decoration: BoxDecoration(
  85. border: Border.all(color: Colors.grey),
  86. borderRadius: BorderRadius.circular(8),
  87. ),
  88. child: ListView.builder(
  89. padding: EdgeInsets.zero,
  90. itemCount: _electronicIds.length,
  91. itemBuilder: (context, index) {
  92. return ListTile(
  93. visualDensity: VisualDensity.compact,
  94. contentPadding: const EdgeInsets.symmetric(
  95. horizontal: 2,
  96. vertical: 0,
  97. ),
  98. title: Text(_electronicIds[index]),
  99. trailing: IconButton(
  100. icon: const Icon(
  101. Icons.delete,
  102. size: 18,
  103. color: Colors.red,
  104. ),
  105. onPressed: () => _removeRfid(index),
  106. ),
  107. );
  108. },
  109. ),
  110. ),
  111. ],
  112. const SizedBox(height: 20),
  113. ],
  114. ),
  115. ),
  116. );
  117. }
  118. Widget _buildBatchInput() {
  119. if (AppConfig.isOffline) {
  120. // 离线模式下使用文本输入框
  121. return Column(
  122. crossAxisAlignment: CrossAxisAlignment.start,
  123. children: [
  124. const Text('批次号', style: TextStyle(fontWeight: FontWeight.bold)),
  125. const SizedBox(height: 10),
  126. Container(
  127. padding: const EdgeInsets.fromLTRB(16, 2, 16, 2),
  128. decoration: BoxDecoration(
  129. border: Border.all(color: Colors.grey),
  130. borderRadius: BorderRadius.circular(8),
  131. ),
  132. child: TextField(
  133. decoration: const InputDecoration(
  134. hintText: '请输入批次号',
  135. border: InputBorder.none,
  136. ),
  137. onChanged: (value) {
  138. setState(() {
  139. _batchNum = value.isNotEmpty ? value : null;
  140. });
  141. },
  142. ),
  143. ),
  144. ],
  145. );
  146. } else {
  147. // 在线模式下使用原来的搜索选择器
  148. return _buildBatchSearchSelect();
  149. }
  150. }
  151. Widget _buildFamilyInput() {
  152. if (AppConfig.isOffline) {
  153. // 离线模式下使用文本输入框
  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: TextField(
  166. decoration: const InputDecoration(
  167. hintText: '请输入家系号',
  168. border: InputBorder.none,
  169. ),
  170. onChanged: (value) {
  171. setState(() {
  172. _familyNum = value.isNotEmpty ? value : null;
  173. });
  174. },
  175. ),
  176. ),
  177. ],
  178. );
  179. } else {
  180. // 在线模式下使用原来的搜索选择器
  181. return _buildFamilySearchSelect();
  182. }
  183. }
  184. Widget _buildBatchSearchSelect() {
  185. return Column(
  186. crossAxisAlignment: CrossAxisAlignment.start,
  187. children: [
  188. const Text('选择批次', style: TextStyle(fontWeight: FontWeight.bold)),
  189. const SizedBox(height: 10),
  190. Container(
  191. padding: const EdgeInsets.fromLTRB(16, 2, 16, 2),
  192. decoration: BoxDecoration(
  193. border: Border.all(color: Colors.grey),
  194. borderRadius: BorderRadius.circular(8),
  195. ),
  196. child: VberSearchSelect<BatchModel>(
  197. searchApi: ({required dynamic queryParams}) async {
  198. final result = await apis.breeding.queryApi.queryPageBatchs(
  199. queryParams,
  200. );
  201. return {'rows': result.rows, 'total': result.total};
  202. },
  203. converter: (BatchModel data) {
  204. return SelectOption(
  205. label: '${data.batchNum}(${data.batchName})',
  206. value: data.batchNum,
  207. extra: data,
  208. );
  209. },
  210. value: _batchNum,
  211. hint: '批次号',
  212. onChanged: (String? value) {
  213. setState(() {
  214. _batchNum = value;
  215. // 当切换批次时,重置后续选项和数据
  216. _familyNum = null;
  217. });
  218. // // 获取翅号数据
  219. // if (value != null) {
  220. // // 使用Future.microtask确保在下一个微任务中加载数据,避免在构建过程中修改状态
  221. // Future.microtask(() => _loadWingTags(value));
  222. // }
  223. },
  224. hideUnderline: true,
  225. ),
  226. ),
  227. ],
  228. );
  229. }
  230. Widget _buildFamilySearchSelect() {
  231. return Column(
  232. crossAxisAlignment: CrossAxisAlignment.start,
  233. children: [
  234. const Text('选择家系号', style: TextStyle(fontWeight: FontWeight.bold)),
  235. const SizedBox(height: 10),
  236. Container(
  237. padding: const EdgeInsets.fromLTRB(16, 2, 16, 2),
  238. decoration: BoxDecoration(
  239. border: Border.all(color: Colors.grey),
  240. borderRadius: BorderRadius.circular(8),
  241. ),
  242. child: VberSearchSelect<FamilyModel>(
  243. searchApi: ({required dynamic queryParams}) async {
  244. final result = await apis.breeding.queryApi.queryPageFamilys(
  245. queryParams,
  246. );
  247. return {'rows': result.rows, 'total': result.total};
  248. },
  249. converter: (FamilyModel data) {
  250. return SelectOption(
  251. label: data.familyNum,
  252. value: data.id.toString(),
  253. extra: data,
  254. );
  255. },
  256. value: _familyNum,
  257. hint: '家系号',
  258. onChanged: (String? value) {
  259. setState(() {
  260. _familyNum = value;
  261. });
  262. },
  263. hideUnderline: true,
  264. ),
  265. ),
  266. ],
  267. );
  268. }
  269. Widget _buildElectronicIdsSection() {
  270. return VberElectronicIdsField(
  271. electronicIds: _electronicIds,
  272. onIdsScanned: (List<RfidModel> idList) {
  273. // 过滤出未存在的RFID
  274. final newIds = idList
  275. .where((id) => !_electronicIds.contains(id.uid))
  276. .toList();
  277. if (newIds.isNotEmpty) {
  278. setState(() {
  279. // 将新的ElectronicId添加到列表中
  280. for (var id in newIds) {
  281. _electronicIds.insert(0, id.uid);
  282. }
  283. });
  284. ScaffoldMessenger.of(context).showSnackBar(
  285. SnackBar(
  286. content: Text("新增 ${newIds.length} 枚电子编号"),
  287. backgroundColor: Colors.green,
  288. ),
  289. );
  290. // ToastUtil.success("新增 ${newIds.length} 枚电子编号");
  291. // if (newIds.length == idList.length) {
  292. // // 全都是新添加的
  293. // ToastUtil.success("成功添加 ${newIds.length} 枚新的电子编号");
  294. // } else {
  295. // // 部分是重复的
  296. // ToastUtil.info("新增 ${newIds.length} 枚电子编号");
  297. // // ,${idList.length - newIds.length} 枚已存在
  298. // }
  299. } else {
  300. // 所有RFID都已存在
  301. // ToastUtil.info("电子编号已存在");
  302. ScaffoldMessenger.of(context).showSnackBar(
  303. const SnackBar(
  304. content: Text('电子编号已存在'),
  305. backgroundColor: Colors.orange,
  306. ),
  307. );
  308. }
  309. },
  310. multiple: true,
  311. label: '电子编号',
  312. multiplePlaceholder: '未识别',
  313. multipleFormat: '已识别 %d 枚电子编号',
  314. );
  315. }
  316. // 移除指定索引的电子编号
  317. void _removeRfid(int index) {
  318. setState(() {
  319. _electronicIds.removeAt(index);
  320. });
  321. }
  322. // 清空所有已识别的电子编号
  323. void _clearRfids() {
  324. setState(() {
  325. _electronicIds.clear();
  326. });
  327. ToastUtil.info('已清空所有电子编号');
  328. }
  329. // 提交数据
  330. void _handleSubmit() {
  331. final data = {
  332. 'electronicIds': _electronicIds,
  333. 'batchNum': _batchNum,
  334. 'familyNum': _familyNum,
  335. 'date': DateTimeUtil.format(DateTime.now()),
  336. };
  337. apis.breeding.submitApi.bindChicken(data).then((res) {
  338. if (res.success) {
  339. if (res.message.isNotEmpty) {
  340. ToastUtil.success(res.message);
  341. }
  342. if (mounted) {
  343. ScaffoldMessenger.of(context).showSnackBar(
  344. const SnackBar(
  345. content: Text('批量绑定个体成功'),
  346. backgroundColor: Colors.green,
  347. ),
  348. );
  349. // 提交后重置表单
  350. setState(() {
  351. _electronicIds.clear();
  352. });
  353. }
  354. } else {
  355. ToastUtil.error('批量绑定个体失败');
  356. if (mounted && res.message.isNotEmpty) {
  357. logger.e(res.message);
  358. ScaffoldMessenger.of(context).showSnackBar(
  359. SnackBar(content: Text(res.message), backgroundColor: Colors.red),
  360. );
  361. }
  362. }
  363. });
  364. }
  365. }