import 'package:flutter/material.dart'; import 'package:chicken_farm/components/vb_app_bar.dart'; import 'package:chicken_farm/core/utils/toast.dart'; import 'package:chicken_farm/components/vb_rfid_field.dart'; class IndividualWeighingPage extends StatefulWidget { const IndividualWeighingPage({super.key}); @override State createState() => _IndividualWeighingPageState(); } class _IndividualWeighingPageState extends State { String? _rfid; double? _weight; bool _isReadingWeight = 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), // 重量区域 _buildWeightSection(), const SizedBox(height: 30), // 提交按钮 SizedBox( width: double.infinity, child: ElevatedButton( onPressed: _rfid != null && _weight != null ? _handleSubmit : null, style: ElevatedButton.styleFrom( backgroundColor: _rfid != null && _weight != 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 _buildWeightSection() { return Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ const Text('重量(kg)', 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( _weight != null ? _weight!.toStringAsFixed(2) : '未读取', style: TextStyle( color: _weight != null ? Colors.black : Colors.grey, fontSize: 16, ), ), ), IconButton( icon: _isReadingWeight ? const SizedBox( width: 20, height: 20, child: CircularProgressIndicator(strokeWidth: 2), ) : const Icon(Icons.sync, size: 20), onPressed: _simulateReadWeight, ), ], ), ), ], ); } // 模拟读取重量 void _simulateReadWeight() { if (_isReadingWeight) return; setState(() { _isReadingWeight = true; }); // 模拟读取重量过程 Future.delayed(const Duration(seconds: 1), () { if (mounted) { setState(() { _weight = double.parse( (5.0 + DateTime.now().millisecond % 50 / 10).toStringAsFixed(2), ); _isReadingWeight = false; }); ToastUtil.success('重量读取成功'); } }); } // 提交数据 void _handleSubmit() { // 在实际应用中,这里会发送数据到服务器 ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text('个体称重提交成功'), backgroundColor: Colors.green), ); // 提交后重置表单 setState(() { _rfid = null; _weight = null; }); } }