batch_create_page.dart 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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}',
  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. hideUnderline: true,
  220. ),
  221. ),
  222. ],
  223. );
  224. }
  225. Widget _buildFamilySearchSelect() {
  226. final bool isFamilySelectEnabled = _batchNum != null;
  227. return Column(
  228. crossAxisAlignment: CrossAxisAlignment.start,
  229. children: [
  230. const Text('选择家系号', style: TextStyle(fontWeight: FontWeight.bold)),
  231. const SizedBox(height: 10),
  232. Container(
  233. padding: const EdgeInsets.fromLTRB(16, 2, 16, 2),
  234. decoration: BoxDecoration(
  235. border: Border.all(color: Colors.grey),
  236. borderRadius: BorderRadius.circular(8),
  237. ),
  238. child: VberSearchSelect<FamilyModel>(
  239. searchApi: ({required dynamic queryParams}) async {
  240. queryParams['batchNum'] = _batchNum;
  241. final result = await apis.breeding.queryApi.queryPageFamilys(
  242. queryParams,
  243. );
  244. return {'rows': result.rows, 'total': result.total};
  245. },
  246. converter: (FamilyModel data) {
  247. return SelectOption(
  248. label: '家系号:${data.familyNum}',
  249. value: data.familyNum,
  250. extra: data,
  251. );
  252. },
  253. value: _familyNum,
  254. hint: '家系号',
  255. enabled: isFamilySelectEnabled,
  256. onChanged: (String? value) {
  257. setState(() {
  258. _familyNum = value;
  259. });
  260. },
  261. hideUnderline: true,
  262. ),
  263. ),
  264. ],
  265. );
  266. }
  267. Widget _buildElectronicIdsSection() {
  268. return VberElectronicIdsField(
  269. electronicIds: _electronicIds,
  270. onIdsScanned: (List<RfidModel> idList) {
  271. // 过滤出未存在的RFID
  272. final newIds = idList
  273. .where((id) => !_electronicIds.contains(id.uid))
  274. .toList();
  275. if (newIds.isNotEmpty) {
  276. setState(() {
  277. // 将新的ElectronicId添加到列表中
  278. for (var id in newIds) {
  279. _electronicIds.insert(0, id.uid);
  280. }
  281. });
  282. ScaffoldMessenger.of(context).showSnackBar(
  283. SnackBar(
  284. content: Text("新增 ${newIds.length} 枚电子编号"),
  285. backgroundColor: Colors.green,
  286. ),
  287. );
  288. // ToastUtil.success("新增 ${newIds.length} 枚电子编号");
  289. // if (newIds.length == idList.length) {
  290. // // 全都是新添加的
  291. // ToastUtil.success("成功添加 ${newIds.length} 枚新的电子编号");
  292. // } else {
  293. // // 部分是重复的
  294. // ToastUtil.info("新增 ${newIds.length} 枚电子编号");
  295. // // ,${idList.length - newIds.length} 枚已存在
  296. // }
  297. } else {
  298. // 所有RFID都已存在
  299. // ToastUtil.info("电子编号已存在");
  300. ScaffoldMessenger.of(context).showSnackBar(
  301. const SnackBar(
  302. content: Text('电子编号已存在'),
  303. backgroundColor: Colors.orange,
  304. ),
  305. );
  306. }
  307. },
  308. multiple: true,
  309. label: '电子编号',
  310. multiplePlaceholder: '未识别',
  311. multipleFormat: '已识别 %d 枚电子编号',
  312. );
  313. }
  314. // 移除指定索引的电子编号
  315. void _removeRfid(int index) {
  316. setState(() {
  317. _electronicIds.removeAt(index);
  318. });
  319. }
  320. // 清空所有已识别的电子编号
  321. void _clearRfids() {
  322. setState(() {
  323. _electronicIds.clear();
  324. });
  325. ToastUtil.info('已清空所有电子编号');
  326. }
  327. // 提交数据
  328. void _handleSubmit() {
  329. final data = {
  330. 'electronicIds': _electronicIds,
  331. 'batchNum': _batchNum,
  332. 'familyNum': _familyNum,
  333. 'date': DateTimeUtil.format(DateTime.now()),
  334. };
  335. apis.breeding.submitApi.bindChicken(data).then((res) {
  336. if (res.success) {
  337. ToastUtil.success(res.message.isEmpty ? '批量绑定个体成功' : res.message);
  338. if (mounted) {
  339. // 提交后重置表单
  340. setState(() {
  341. _electronicIds.clear();
  342. });
  343. }
  344. } else {
  345. ToastUtil.errorAlert(res.message.isNotEmpty ? res.message : '批量绑定个体失败');
  346. }
  347. });
  348. }
  349. }