import 'package:chicken_farm/components/vb_rfid_field.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/components/vb_dict_select.dart'; import 'package:chicken_farm/core/utils/toast.dart'; class BatchCullingPage extends StatefulWidget { const BatchCullingPage({super.key}); @override State createState() => _BatchCullingPageState(); } class _BatchCullingPageState extends State { final List _rfids = []; String? _cullReason; String? _disposalMethod; @override Widget build(BuildContext context) { return Scaffold( appBar: const VberAppBar(title: '批量淘汰', showLeftButton: true), body: Padding( padding: const EdgeInsets.all(16.0), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ // 电子编号区域 _buildRfidSection(), const SizedBox(height: 20), // 淘汰原因 _buildCullReasonSection(), const SizedBox(height: 20), // 处置方式 _buildDisposalMethodSection(), const SizedBox(height: 20), // 提交按钮 SizedBox( width: double.infinity, child: ElevatedButton( onPressed: _rfids.isNotEmpty && _cullReason != null && _disposalMethod != null ? _handleSubmit : null, style: ElevatedButton.styleFrom( backgroundColor: _rfids.isNotEmpty && _cullReason != null && _disposalMethod != null ? Colors.blue : Colors.grey, foregroundColor: Colors.white, ), child: const Text('提交'), ), ), const SizedBox(height: 20), // 已识别的电子编号列表 if (_rfids.isNotEmpty) ...[ const Text( '已识别的电子编号', style: TextStyle(fontWeight: FontWeight.bold), ), const SizedBox(height: 10), Expanded( child: Container( padding: const EdgeInsets.all(10), decoration: BoxDecoration( border: Border.all(color: Colors.grey), borderRadius: BorderRadius.circular(8), ), child: ListView.builder( padding: EdgeInsets.zero, itemCount: _rfids.length, itemBuilder: (context, index) { return ListTile( visualDensity: VisualDensity.compact, contentPadding: const EdgeInsets.symmetric( horizontal: 2, vertical: 0, ), title: Text(_rfids[index]), trailing: IconButton( icon: const Icon( Icons.delete, size: 18, color: Colors.red, ), onPressed: () => _removeRfid(index), ), ); }, ), ), ), ], const SizedBox(height: 20), ], ), ), ); } Widget _buildRfidSection() { return VberRfidField( rfids: _rfids, onRfidsScanned: (List scannedRfids) { // 过滤出未存在的RFID final newRfids = scannedRfids .where((rfid) => !_rfids.contains(rfid.uid)) .toList(); if (newRfids.isNotEmpty) { setState(() { // 将新的RFID添加到列表中 for (var rfid in newRfids) { _rfids.add(rfid.uid); } }); if (newRfids.length == scannedRfids.length) { // 全都是新添加的 ToastUtil.success("成功添加 ${newRfids.length} 枚新的电子编号"); } else { // 部分是重复的 ToastUtil.info( "新增 ${newRfids.length} 枚电子编号,${scannedRfids.length - newRfids.length} 枚已存在", ); } } else { // 所有RFID都已存在 ToastUtil.info("所有电子编号均已存在"); } }, multiple: true, label: '电子编号', multiplePlaceholder: '未识别', multipleFormat: '已识别 %d 枚电子编号', ); } Widget _buildCullReasonSection() { 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: VberDictSelect( dictType: 'chicken_cull_reason', value: _cullReason, onChanged: (value) { setState(() { _cullReason = value; }); }, hint: '请选择淘汰原因', hideUnderline: true, ), ), ], ); } Widget _buildDisposalMethodSection() { 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: VberDictSelect( dictType: 'chicken_disposal_method', value: _disposalMethod, onChanged: (value) { setState(() { _disposalMethod = value; }); }, hint: '请选择处置方式', hideUnderline: true, ), ), ], ); } // // 清空所有已识别的电子编号 // void _clearRfids() { // setState(() { // _rfids.clear(); // }); // ToastUtil.info('已清空所有电子编号'); // } // 移除指定索引的电子编号 void _removeRfid(int index) { setState(() { _rfids.removeAt(index); }); } // 提交数据 void _handleSubmit() { // 在实际应用中,这里会发送数据到服务器 ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text('批量淘汰提交成功,共${_rfids.length}只鸡'), backgroundColor: Colors.green, ), ); // 提交后重置表单 setState(() { _rfids.clear(); _disposalMethod = null; }); } }