import 'package:chicken_farm/apis/index.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'; import 'package:chicken_farm/components/vb_rfid_field.dart'; class IndividualCullingPage extends StatefulWidget { const IndividualCullingPage({super.key}); @override State createState() => _IndividualCullingPageState(); } class _IndividualCullingPageState extends State { String? _rfid; 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: 30), // 提交按钮 SizedBox( width: double.infinity, child: ElevatedButton( onPressed: _rfid != null && _cullReason != null && _disposalMethod != null ? _handleSubmit : null, style: ElevatedButton.styleFrom( backgroundColor: _rfid != null && _cullReason != null && _disposalMethod != null ? Colors.blue : Colors.grey, foregroundColor: Colors.white, ), child: const Text('提交'), ), ), ], ), ), ); } Widget _buildRfidSection() { return VberRfidField( rfid: _rfid, onRfidScanned: (rfid) { setState(() { _rfid = rfid; }); ToastUtil.success('电子编号识别成功'); }, label: '电子编号', placeholder: '未识别', ); } 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 _handleSubmit() { final data = { "rfid": _rfid, "cull_reason": _cullReason, "disposal_method": _disposalMethod, }; apis.breeding.submitApi .cull(data) .then((_) { if (mounted) { ScaffoldMessenger.of(context).showSnackBar( const SnackBar( content: Text('淘汰提交成功'), backgroundColor: Colors.green, ), ); // 提交后重置表单 setState(() { _rfid = null; _cullReason = null; _disposalMethod = null; }); } }) .catchError((err) { ToastUtil.error('淘汰提交失败'); if (mounted && err != null) { String errorMessage = err.toString(); if (err is Exception) { errorMessage = err.toString(); } ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text(errorMessage), backgroundColor: Colors.red, ), ); } }); } }