| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359 |
- import 'package:chicken_farm/apis/index.dart';
- import 'package:chicken_farm/core/services/pda/scan_manager.dart';
- import 'package:chicken_farm/core/utils/datetime_util.dart';
- import 'package:chicken_farm/core/utils/logger.dart';
- 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_electronic_id_field.dart';
- class CageChangePage extends StatefulWidget {
- const CageChangePage({super.key});
- @override
- State<CageChangePage> createState() => _CageChangePageState();
- }
- class _CageChangePageState extends State<CageChangePage> {
- final List<String> _electronicIds = [];
- final TextEditingController _cageController = TextEditingController();
- bool _isScanningCode = false;
- @override
- void initState() {
- super.initState();
- _isScanningCode = false;
- _cageController.clear();
- logger.d("页面初始化");
- ScanManager.instance.registerCallbacks(
- onScanResult: (result) {
- setState(() {
- logger.d("扫描目标笼号:$result");
- _cageController.text = result;
- _isScanningCode = false;
- });
- ScanManager.instance.stopScan();
- },
- onScanError: (error) {
- logger.d("扫码错误:$error");
- setState(() {
- _isScanningCode = false;
- });
- },
- onKeyPress: (bool isDouble, String keyCode) {
- if (isDouble && keyCode == "KEY_619") {
- _cageController.clear();
- ScanManager.instance.startScan();
- } else {
- logger.d("按键:$isDouble $keyCode");
- }
- },
- onScanHeadStateChanged: (bool isOpen) {
- if (isOpen) {
- ToastUtil.show("扫描组件已打开", duration: 1, bgColor: Colors.green);
- } else {
- ToastUtil.show("扫描组件已关闭", duration: 0.5);
- }
- setState(() {});
- },
- );
- }
- @override
- void dispose() {
- _cageController.dispose();
- // 释放通道
- ScanManager.instance.disposeScan();
- super.dispose();
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: const VberAppBar(title: '换笼管理', showLeftButton: true),
- body: SingleChildScrollView(
- child: Padding(
- padding: const EdgeInsets.all(16.0),
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- // // 源笼号区域
- // _buildCageSection(1),
- // const SizedBox(height: 15),
- // // 目标笼号区域
- // _buildCageSection(2),
- // const SizedBox(height: 15),
- // 目标笼号区域
- _buildCageSection(),
- const SizedBox(height: 15),
- // 电子编号区域
- _buildElectronicIdSection(),
- const SizedBox(height: 15),
- // 提交按钮
- // SizedBox(
- // width: double.infinity,
- // child: ElevatedButton(
- // onPressed:
- // _sourceCageController.text.isNotEmpty &&
- // _targetCageController.text.isNotEmpty &&
- // _rfids.isNotEmpty
- // ? _handleSubmit
- // : null,
- // style: ElevatedButton.styleFrom(
- // backgroundColor:
- // (_sourceCageController.text.isNotEmpty &&
- // _targetCageController.text.isNotEmpty &&
- // _rfids.isNotEmpty)
- // ? Colors.blue
- // : Colors.grey,
- // foregroundColor: Colors.white,
- // ),
- // child: const Text('提交'),
- // ),
- // ),
- SizedBox(
- width: double.infinity,
- child: ElevatedButton(
- onPressed:
- _cageController.text.isNotEmpty &&
- _electronicIds.isNotEmpty
- ? _handleSubmit
- : null,
- style: ElevatedButton.styleFrom(
- backgroundColor:
- (_cageController.text.isNotEmpty &&
- _electronicIds.isNotEmpty)
- ? Colors.blue
- : Colors.grey,
- foregroundColor: Colors.white,
- ),
- child: const Text('提交'),
- ),
- ),
- const SizedBox(height: 20),
- // 已识别的电子编号列表
- if (_electronicIds.isNotEmpty) ...[
- Row(
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- children: [
- const Text(
- '已识别的电子编号',
- style: TextStyle(fontWeight: FontWeight.bold),
- ),
- IconButton(
- icon: const Icon(Icons.clear, size: 18),
- onPressed: _clearElectronicIds,
- tooltip: '清空编号',
- ),
- ],
- ),
- const SizedBox(height: 10),
- Container(
- padding: const EdgeInsets.all(10),
- decoration: BoxDecoration(
- border: Border.all(color: Colors.grey),
- borderRadius: BorderRadius.circular(8),
- ),
- child: ListView.builder(
- shrinkWrap: true,
- physics: const NeverScrollableScrollPhysics(),
- padding: EdgeInsets.zero,
- itemCount: _electronicIds.length,
- itemBuilder: (context, index) {
- return ListTile(
- visualDensity: VisualDensity.compact,
- contentPadding: const EdgeInsets.symmetric(
- horizontal: 2,
- vertical: 0,
- ),
- title: Text(_electronicIds[index]),
- trailing: IconButton(
- icon: const Icon(
- Icons.delete,
- size: 18,
- color: Colors.red,
- ),
- onPressed: () => _removeElectronicId(index),
- ),
- );
- },
- ),
- ),
- ],
- const SizedBox(height: 20),
- ],
- ),
- ),
- ),
- );
- }
- Widget _buildCageSection() {
- // 确保光标在文本末尾
- if (_cageController.text.isNotEmpty) {
- _cageController.selection = TextSelection.fromPosition(
- TextPosition(offset: _cageController.text.length),
- );
- }
- return Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- Text('目标笼号', 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(
- // 将Text替换为TextField
- child: TextField(
- controller: _cageController,
- enabled: _cageController.text.isEmpty,
- decoration: InputDecoration(
- border: InputBorder.none,
- hintText: _cageController.text.isNotEmpty ? null : '未扫描',
- hintStyle: TextStyle(
- color: _cageController.text.isNotEmpty
- ? Colors.black
- : Colors.grey,
- fontSize: 16,
- ),
- ),
- style: TextStyle(
- color: _cageController.text.isNotEmpty
- ? Colors.black
- : Colors.grey,
- fontSize: 16,
- ),
- onChanged: (text) {
- setState(() {
- // 文本更改由controller自动处理
- });
- },
- ),
- ),
- if (_cageController.text.isNotEmpty) ...[
- IconButton(
- icon: const Icon(Icons.refresh, size: 20),
- onPressed: () {
- _handleScanCageCode();
- },
- ),
- ] else ...[
- IconButton(
- icon: _isScanningCode
- ? const SizedBox(
- width: 20,
- height: 20,
- child: CircularProgressIndicator(strokeWidth: 2),
- )
- : const Icon(Icons.qr_code_scanner, size: 20),
- onPressed: () {
- _handleScanCageCode();
- },
- ),
- ],
- ],
- ),
- ),
- ],
- );
- }
- Widget _buildElectronicIdSection() {
- return VberElectronicIdsField(
- electronicIds: _electronicIds,
- onIdScanned: (id) {
- // 检查是否已存在相同的ID标签
- bool isDuplicate = _electronicIds.any((v) => v == id);
- if (isDuplicate) {
- ScaffoldMessenger.of(context).showSnackBar(
- const SnackBar(
- content: Text('电子编号已存在'),
- backgroundColor: Colors.orange,
- ),
- );
- } else {
- // 如果不重复,添加到列表中
- setState(() {
- _electronicIds.insert(0, id);
- });
- ScaffoldMessenger.of(context).showSnackBar(
- const SnackBar(
- content: Text('成功添加新的电子编号'),
- backgroundColor: Colors.green,
- ),
- );
- }
- },
- multiple: true,
- multipleScan: false,
- label: '电子编号',
- multiplePlaceholder: '未识别',
- multipleFormat: '已识别 %d 枚电子编号',
- );
- }
- void _handleScanCageCode() async {
- if (_isScanningCode) return;
- setState(() {
- _isScanningCode = true;
- });
- bool success = await ScanManager.instance.startScan();
- if (!success) {
- ToastUtil.error("扫码失败");
- }
- }
- // 移除指定索引的电子编号
- void _removeElectronicId(int index) {
- setState(() {
- _electronicIds.removeAt(index);
- });
- }
- // 清空所有已识别的电子编号
- void _clearElectronicIds() {
- setState(() {
- _electronicIds.clear();
- });
- ToastUtil.info('已清空所有电子编号');
- }
- // 提交数据
- void _handleSubmit() {
- final data = {
- 'targetCage': _cageController.text,
- 'electronicIds': _electronicIds,
- 'date': DateTimeUtil.format(DateTime.now()),
- };
- apis.breeding.submitApi.cageChange(data).then((res) {
- if (res.success) {
- if (res.message.isNotEmpty) {
- ToastUtil.success(res.message.isEmpty ? '换笼成功' : res.message);
- }
- if (mounted) {
- // 提交后重置表单
- setState(() {
- _electronicIds.clear();
- });
- }
- } else {
- ToastUtil.errorAlert(res.message.isEmpty ? '换笼提交失败' : res.message);
- }
- });
- }
- }
|