| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335 |
- 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/core/services/pda/pda_scanner_service.dart';
- class CageChangePage extends StatefulWidget {
- const CageChangePage({super.key});
- @override
- State<CageChangePage> createState() => _CageChangePageState();
- }
- class _CageChangePageState extends State<CageChangePage> {
- String? _sourceCageId;
- String? _targetCageId;
- final List<String> _rfids = [];
- // 扫描状态
- bool _isScanningSource = false;
- bool _isScanningTarget = false;
- 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: [
- // 源笼号区域
- _buildCageSection(
- title: '源笼号',
- value: _sourceCageId,
- isScanning: _isScanningSource,
- onScanPressed: _scanSourceCage,
- onChangePressed: _handleChangeSourceCage,
- ),
- const SizedBox(height: 15),
- // 目标笼号区域
- _buildCageSection(
- title: '目标笼号',
- value: _targetCageId,
- isScanning: _isScanningTarget,
- onScanPressed: _scanTargetCage,
- onChangePressed: _handleChangeTargetCage,
- ),
- const SizedBox(height: 15),
- // 电子编号区域
- _buildRfidSection(),
- const SizedBox(height: 15),
- // 提交按钮
- SizedBox(
- width: double.infinity,
- child: ElevatedButton(
- onPressed:
- _sourceCageId != null &&
- _targetCageId != null &&
- _rfids.isNotEmpty
- ? _handleSubmit
- : null,
- style: ElevatedButton.styleFrom(
- backgroundColor:
- (_sourceCageId != null &&
- _targetCageId != null &&
- _rfids.isNotEmpty)
- ? 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 _buildCageSection({
- required String title,
- String? value,
- required bool isScanning,
- required VoidCallback onScanPressed,
- required VoidCallback onChangePressed,
- }) {
- return Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- Text(title, style: const 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(
- value ?? '未扫描',
- style: TextStyle(
- color: value != null ? Colors.black : Colors.grey,
- fontSize: 16,
- ),
- ),
- ),
- if (value != null) ...[
- IconButton(
- icon: const Icon(Icons.refresh, size: 20),
- onPressed: onChangePressed,
- ),
- ] else ...[
- IconButton(
- icon: isScanning
- ? const SizedBox(
- width: 20,
- height: 20,
- child: CircularProgressIndicator(strokeWidth: 2),
- )
- : const Icon(Icons.qr_code_scanner, size: 20),
- onPressed: onScanPressed,
- ),
- ],
- ],
- ),
- ),
- ],
- );
- }
- 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,
- ),
- ),
- ),
- IconButton(
- icon: _isScanningRfid
- ? const SizedBox(
- width: 20,
- height: 20,
- child: CircularProgressIndicator(strokeWidth: 2),
- )
- : const Icon(Icons.add, size: 20),
- onPressed: _scanRfid,
- ),
- ],
- ),
- ),
- ],
- );
- }
- // 扫描源笼号
- void _scanSourceCage() async {
- if (_isScanningSource) return;
- setState(() {
- _isScanningSource = true;
- });
- try {
- final result = await PdaScannerService.scanBarcode();
- if (mounted) {
- setState(() {
- _sourceCageId = result;
- _isScanningSource = false;
- });
- ToastUtil.success('源笼号扫描成功');
- }
- } catch (e) {
- if (mounted) {
- setState(() {
- _isScanningSource = false;
- });
- ToastUtil.error('扫描失败: $e');
- }
- }
- }
- // 扫描目标笼号
- void _scanTargetCage() async {
- if (_isScanningTarget) return;
- setState(() {
- _isScanningTarget = true;
- });
- try {
- final result = await PdaScannerService.scanBarcode();
- if (mounted) {
- setState(() {
- _targetCageId = result;
- _isScanningTarget = false;
- });
- ToastUtil.success('目标笼号扫描成功');
- }
- } catch (e) {
- if (mounted) {
- setState(() {
- _isScanningTarget = false;
- });
- ToastUtil.error('扫描失败: $e');
- }
- }
- }
- // 扫描电子编号
- void _scanRfid() async {
- if (_isScanningRfid) return;
- setState(() {
- _isScanningRfid = true;
- });
- try {
- // 使用实际的RFID读取功能
- final result = await PdaScannerService.readRfid();
- if (mounted) {
- setState(() {
- // 每次添加一个新的电子编号到数组开头(模拟按一次实体按钮扫描一个电子编号)
- _rfids.insert(0, result);
- _isScanningRfid = false;
- });
- ToastUtil.success('鸡扫描成功');
- }
- } catch (e) {
- if (mounted) {
- setState(() {
- _isScanningRfid = false;
- });
- ToastUtil.error('RFID读取失败: $e');
- }
- }
- }
- // 重新扫描源笼号
- void _handleChangeSourceCage() {
- setState(() {
- _sourceCageId = null;
- });
- }
- // 重新扫描目标笼号
- void _handleChangeTargetCage() {
- setState(() {
- _targetCageId = null;
- });
- }
- // 移除指定索引的电子编号
- void _removeRfid(int index) {
- setState(() {
- _rfids.removeAt(index);
- });
- }
- // 提交数据
- void _handleSubmit() {
- // 在实际应用中,这里会发送数据到服务器
- ScaffoldMessenger.of(context).showSnackBar(
- const SnackBar(content: Text('换笼操作提交成功'), backgroundColor: Colors.green),
- );
- // 提交后重置表单
- setState(() {
- _sourceCageId = null;
- _targetCageId = null;
- _rfids.clear();
- });
- }
- }
|