| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- import 'package:chicken_farm/apis/index.dart';
- 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<BatchCullingPage> createState() => _BatchCullingPageState();
- }
- class _BatchCullingPageState extends State<BatchCullingPage> {
- final List<String> _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) ...[
- Row(
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- children: [
- const Text(
- '已识别的电子编号',
- style: TextStyle(fontWeight: FontWeight.bold),
- ),
- IconButton(
- icon: const Icon(Icons.clear, size: 18),
- onPressed: _clearRfids,
- tooltip: '清空编号',
- ),
- ],
- ),
- 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<RfidModel> scannedRfids) {
- // 过滤出未存在的RFID
- final newRfids = scannedRfids
- .where((rfid) => !_rfids.contains(rfid.uid))
- .toList();
- if (newRfids.isNotEmpty) {
- setState(() {
- // 将新的RFID添加到列表中
- for (var rfid in newRfids) {
- _rfids.insert(0, rfid.uid);
- }
- });
- ToastUtil.success("新增 ${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 _removeRfid(int index) {
- setState(() {
- _rfids.removeAt(index);
- });
- }
- // 清空所有已识别的电子编号
- void _clearRfids() {
- setState(() {
- _rfids.clear();
- });
- ToastUtil.info('已清空所有电子编号');
- }
- // 提交数据
- void _handleSubmit() {
- final data = {
- 'rfids': _rfids,
- 'disposal_method': _disposalMethod,
- 'cull_reason': _cullReason,
- };
- apis.breeding.submitApi
- .weight(data)
- .then((_) {
- if (mounted) {
- ScaffoldMessenger.of(context).showSnackBar(
- const SnackBar(
- content: Text('批量淘汰提交成功'),
- backgroundColor: Colors.green,
- ),
- );
- // 提交后重置表单
- setState(() {
- _rfids.clear();
- _disposalMethod = null;
- _cullReason = 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,
- ),
- );
- }
- });
- }
- }
|