| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363 |
- import 'package:chicken_farm/core/config/app_config.dart';
- import 'package:chicken_farm/core/utils/datetime_util.dart';
- import 'package:chicken_farm/core/utils/logger.dart';
- import 'package:chicken_farm/apis/index.dart';
- import 'package:chicken_farm/components/vb_electronic_id_field.dart';
- import 'package:chicken_farm/components/vb_search_select.dart';
- import 'package:chicken_farm/components/vb_select.dart';
- import 'package:chicken_farm/modes/breeding/batch.dart';
- import 'package:chicken_farm/modes/breeding/family.dart';
- import 'package:chicken_farm/modes/rfid/rfid_model.dart';
- import 'package:flutter/material.dart';
- import 'package:chicken_farm/components/vb_app_bar.dart';
- import 'package:chicken_farm/core/utils/toast.dart';
- class BatchCreatePage extends StatefulWidget {
- const BatchCreatePage({super.key});
- @override
- State<BatchCreatePage> createState() => _BatchCreatePageState();
- }
- class _BatchCreatePageState extends State<BatchCreatePage> {
- final List<String> _electronicIds = [];
- String? _batchNum;
- String? _familyNum;
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: const VberAppBar(title: '个体绑定', showLeftButton: true),
- body: SingleChildScrollView(
- padding: const EdgeInsets.all(16.0),
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- // 批次选择
- _buildBatchInput(),
- const SizedBox(height: 10),
- // 家系号选择
- _buildFamilyInput(),
- const SizedBox(height: 20),
- // 电子编号区域
- _buildElectronicIdsSection(),
- const SizedBox(height: 20),
- // 提交按钮
- SizedBox(
- width: double.infinity,
- child: ElevatedButton(
- onPressed:
- _electronicIds.isNotEmpty &&
- _batchNum != null &&
- _familyNum != null
- ? _handleSubmit
- : null,
- style: ElevatedButton.styleFrom(
- backgroundColor:
- _electronicIds.isNotEmpty &&
- _batchNum != null &&
- _familyNum != null
- ? Colors.blue
- : Colors.grey,
- foregroundColor: Colors.white,
- ),
- child: const Text('提交'),
- ),
- ),
- const SizedBox(height: 20),
- // 已识别的电子编号列表
- if (_electronicIds.isNotEmpty) ...[
- Row(
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- children: [
- const Text(
- '已识别的电子编号',
- style: TextStyle(fontWeight: FontWeight.bold),
- ),
- IconButton(
- icon: const Icon(Icons.clear, size: 18),
- onPressed: _clearRfids,
- tooltip: '清空编号',
- ),
- ],
- ),
- const SizedBox(height: 10),
- Container(
- height: 200, // 固定高度以支持滚动
- padding: const EdgeInsets.all(10),
- decoration: BoxDecoration(
- border: Border.all(color: Colors.grey),
- borderRadius: BorderRadius.circular(8),
- ),
- child: ListView.builder(
- padding: EdgeInsets.zero,
- itemCount: _electronicIds.length,
- itemBuilder: (context, index) {
- return ListTile(
- visualDensity: VisualDensity.compact,
- contentPadding: const EdgeInsets.symmetric(
- horizontal: 2,
- vertical: 0,
- ),
- title: Text(_electronicIds[index]),
- trailing: IconButton(
- icon: const Icon(
- Icons.delete,
- size: 18,
- color: Colors.red,
- ),
- onPressed: () => _removeRfid(index),
- ),
- );
- },
- ),
- ),
- ],
- const SizedBox(height: 20),
- ],
- ),
- ),
- );
- }
- Widget _buildBatchInput() {
- if (AppConfig.isOffline) {
- // 离线模式下使用文本输入框
- return Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- const Text('批次号', style: TextStyle(fontWeight: FontWeight.bold)),
- const SizedBox(height: 10),
- Container(
- padding: const EdgeInsets.fromLTRB(16, 2, 16, 2),
- decoration: BoxDecoration(
- border: Border.all(color: Colors.grey),
- borderRadius: BorderRadius.circular(8),
- ),
- child: TextField(
- decoration: const InputDecoration(
- hintText: '请输入批次号',
- border: InputBorder.none,
- ),
- onChanged: (value) {
- setState(() {
- _batchNum = value.isNotEmpty ? value : null;
- });
- },
- ),
- ),
- ],
- );
- } else {
- // 在线模式下使用原来的搜索选择器
- return _buildBatchSearchSelect();
- }
- }
- Widget _buildFamilyInput() {
- if (AppConfig.isOffline) {
- // 离线模式下使用文本输入框
- return Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- const Text('家系号', style: TextStyle(fontWeight: FontWeight.bold)),
- const SizedBox(height: 10),
- Container(
- padding: const EdgeInsets.fromLTRB(16, 2, 16, 2),
- decoration: BoxDecoration(
- border: Border.all(color: Colors.grey),
- borderRadius: BorderRadius.circular(8),
- ),
- child: TextField(
- decoration: const InputDecoration(
- hintText: '请输入家系号',
- border: InputBorder.none,
- ),
- onChanged: (value) {
- setState(() {
- _familyNum = value.isNotEmpty ? value : null;
- });
- },
- ),
- ),
- ],
- );
- } else {
- // 在线模式下使用原来的搜索选择器
- return _buildFamilySearchSelect();
- }
- }
- Widget _buildBatchSearchSelect() {
- return Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- const Text('选择批次', style: TextStyle(fontWeight: FontWeight.bold)),
- const SizedBox(height: 10),
- Container(
- padding: const EdgeInsets.fromLTRB(16, 2, 16, 2),
- decoration: BoxDecoration(
- border: Border.all(color: Colors.grey),
- borderRadius: BorderRadius.circular(8),
- ),
- child: VberSearchSelect<BatchModel>(
- searchApi: ({required dynamic queryParams}) async {
- final result = await apis.breeding.queryApi.queryPageBatchs(
- queryParams,
- );
- return {'rows': result.rows, 'total': result.total};
- },
- converter: (BatchModel data) {
- return SelectOption(
- label: '批次号:${data.batchNum}',
- value: data.batchNum,
- extra: data,
- );
- },
- value: _batchNum,
- hint: '批次号',
- onChanged: (String? value) {
- setState(() {
- _batchNum = value;
- // 当切换批次时,重置后续选项和数据
- _familyNum = null;
- });
- },
- hideUnderline: true,
- ),
- ),
- ],
- );
- }
- Widget _buildFamilySearchSelect() {
- final bool isFamilySelectEnabled = _batchNum != null;
- return Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- const Text('选择家系号', style: TextStyle(fontWeight: FontWeight.bold)),
- const SizedBox(height: 10),
- Container(
- padding: const EdgeInsets.fromLTRB(16, 2, 16, 2),
- decoration: BoxDecoration(
- border: Border.all(color: Colors.grey),
- borderRadius: BorderRadius.circular(8),
- ),
- child: VberSearchSelect<FamilyModel>(
- searchApi: ({required dynamic queryParams}) async {
- queryParams['batchNum'] = _batchNum;
- final result = await apis.breeding.queryApi.queryPageFamilys(
- queryParams,
- );
- return {'rows': result.rows, 'total': result.total};
- },
- converter: (FamilyModel data) {
- return SelectOption(
- label: '家系号:${data.familyNum}',
- value: data.familyNum,
- extra: data,
- );
- },
- value: _familyNum,
- hint: '家系号',
- enabled: isFamilySelectEnabled,
- onChanged: (String? value) {
- setState(() {
- _familyNum = value;
- });
- },
- hideUnderline: true,
- ),
- ),
- ],
- );
- }
- Widget _buildElectronicIdsSection() {
- return VberElectronicIdsField(
- electronicIds: _electronicIds,
- onIdsScanned: (List<RfidModel> idList) {
- // 过滤出未存在的RFID
- final newIds = idList
- .where((id) => !_electronicIds.contains(id.uid))
- .toList();
- if (newIds.isNotEmpty) {
- setState(() {
- // 将新的ElectronicId添加到列表中
- for (var id in newIds) {
- _electronicIds.insert(0, id.uid);
- }
- });
- ScaffoldMessenger.of(context).showSnackBar(
- SnackBar(
- content: Text("新增 ${newIds.length} 枚电子编号"),
- backgroundColor: Colors.green,
- ),
- );
- // ToastUtil.success("新增 ${newIds.length} 枚电子编号");
- // if (newIds.length == idList.length) {
- // // 全都是新添加的
- // ToastUtil.success("成功添加 ${newIds.length} 枚新的电子编号");
- // } else {
- // // 部分是重复的
- // ToastUtil.info("新增 ${newIds.length} 枚电子编号");
- // // ,${idList.length - newIds.length} 枚已存在
- // }
- } else {
- // 所有RFID都已存在
- // ToastUtil.info("电子编号已存在");
- ScaffoldMessenger.of(context).showSnackBar(
- const SnackBar(
- content: Text('电子编号已存在'),
- backgroundColor: Colors.orange,
- ),
- );
- }
- },
- multiple: true,
- label: '电子编号',
- multiplePlaceholder: '未识别',
- multipleFormat: '已识别 %d 枚电子编号',
- );
- }
- // 移除指定索引的电子编号
- void _removeRfid(int index) {
- setState(() {
- _electronicIds.removeAt(index);
- });
- }
- // 清空所有已识别的电子编号
- void _clearRfids() {
- setState(() {
- _electronicIds.clear();
- });
- ToastUtil.info('已清空所有电子编号');
- }
- // 提交数据
- void _handleSubmit() {
- final data = {
- 'electronicIds': _electronicIds,
- 'batchNum': _batchNum,
- 'familyNum': _familyNum,
- 'date': DateTimeUtil.format(DateTime.now()),
- };
- apis.breeding.submitApi.bindChicken(data).then((res) {
- if (res.success) {
- ToastUtil.success(res.message.isEmpty ? '批量绑定个体成功' : res.message);
- if (mounted) {
- // 提交后重置表单
- setState(() {
- _electronicIds.clear();
- });
- }
- } else {
- ToastUtil.errorAlert(res.message.isNotEmpty ? res.message : '批量绑定个体失败');
- }
- });
- }
- }
|