batch_create_page.dart 12 KB

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