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; bool _isScanningRfid = false; @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 && _disposalMethod != null ? _handleSubmit : null, style: ElevatedButton.styleFrom( backgroundColor: _rfids.isNotEmpty && _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 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: Row( children: [ Expanded( child: Text( _rfids.isEmpty ? '未扫描' : '已扫描 ${_rfids.length} 只鸡', style: TextStyle( color: _rfids.isNotEmpty ? Colors.black : Colors.grey, fontSize: 16, ), ), ), // 根据是否有已扫描的RFID决定显示什么按钮 if (_rfids.isEmpty) ...[ IconButton( icon: _isScanningRfid ? const SizedBox( width: 20, height: 20, child: CircularProgressIndicator(strokeWidth: 2), ) : const Icon(Icons.qr_code_scanner, size: 20), onPressed: _simulateScanMultipleRfids, ), ] else ...[ TextButton(onPressed: _clearRfids, child: const Text('清空编号')), ], ], ), ), ], ); } 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 _simulateScanMultipleRfids() { if (_isScanningRfid) return; setState(() { _isScanningRfid = true; }); // 模拟扫描过程 Future.delayed(const Duration(seconds: 1), () { if (mounted) { setState(() { // 一次性添加多个电子编号(模拟实际情况) _rfids.clear(); // 确保是新的扫描结果 final now = DateTime.now().millisecondsSinceEpoch; for (int i = 0; i < 5; i++) { _rfids.add('RFID-${now + i}'); } _isScanningRfid = false; }); ToastUtil.success('成功扫描${_rfids.length}个电子编号'); } }); } // 清空所有已扫描的电子编号 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; }); } }