batch_create_page.dart 12 KB

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