|
|
@@ -1,4 +1,3 @@
|
|
|
-import 'package:chicken_farm/core/services/pda/rfid_manager.dart';
|
|
|
import 'package:chicken_farm/core/utils/logger.dart';
|
|
|
import 'package:flutter/material.dart';
|
|
|
import 'package:chicken_farm/components/vb_app_bar.dart';
|
|
|
@@ -14,10 +13,12 @@ class CageChangePage extends StatefulWidget {
|
|
|
}
|
|
|
|
|
|
class _CageChangePageState extends State<CageChangePage> {
|
|
|
- String? _sourceCageId;
|
|
|
- String? _targetCageId;
|
|
|
final List<String> _rfids = [];
|
|
|
|
|
|
+ // 添加TextEditingControllers用于控制输入框
|
|
|
+ final TextEditingController _sourceCageController = TextEditingController();
|
|
|
+ final TextEditingController _targetCageController = TextEditingController();
|
|
|
+
|
|
|
// 添加FocusNode用于控制焦点
|
|
|
final FocusNode _sourceCageFocusNode = FocusNode();
|
|
|
final FocusNode _targetCageFocusNode = FocusNode();
|
|
|
@@ -26,12 +27,13 @@ class _CageChangePageState extends State<CageChangePage> {
|
|
|
bool _isScanningSource = false;
|
|
|
bool _isScanningTarget = false;
|
|
|
int _scanTag = 1;
|
|
|
- late final RfidManager _rfidManager;
|
|
|
|
|
|
@override
|
|
|
void initState() {
|
|
|
super.initState();
|
|
|
|
|
|
+ _sourceCageController.clear();
|
|
|
+ _targetCageController.clear();
|
|
|
// 监听焦点变化以更新_scanTag值
|
|
|
_sourceCageFocusNode.addListener(() {
|
|
|
if (_sourceCageFocusNode.hasFocus) {
|
|
|
@@ -55,26 +57,27 @@ class _CageChangePageState extends State<CageChangePage> {
|
|
|
// 初始化监听
|
|
|
ScanChannel.initScanListener(
|
|
|
onScanResult: (result) {
|
|
|
- logger.d("扫码结果:$result");
|
|
|
+ logger.d("扫码结果:$result tag: $_scanTag");
|
|
|
if (_scanTag == 1) {
|
|
|
// 源笼号
|
|
|
setState(() {
|
|
|
- _sourceCageId = result;
|
|
|
+ _sourceCageController.text = result;
|
|
|
_isScanningSource = false;
|
|
|
});
|
|
|
- // 源笼号扫描成功后自动延时扫描目标笼号
|
|
|
- WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
|
- _sourceCageFocusNode.requestFocus();
|
|
|
- logger.d("开始自动扫描目标笼号");
|
|
|
+ if (_targetCageController.text.isEmpty) {
|
|
|
+ // 源笼号扫描成功后自动延时扫描目标笼号
|
|
|
+ ScanChannel.stopSingleScan();
|
|
|
+ logger.d("自动扫描目标笼号");
|
|
|
+ _targetCageFocusNode.requestFocus();
|
|
|
_dealyScanCode(2);
|
|
|
- });
|
|
|
+ }
|
|
|
} else if (_scanTag == 2) {
|
|
|
// 目标笼号
|
|
|
setState(() {
|
|
|
- _targetCageId = result;
|
|
|
+ _targetCageController.text = result;
|
|
|
_isScanningTarget = false;
|
|
|
});
|
|
|
- } else if (_scanTag == 3) {}
|
|
|
+ }
|
|
|
// 扫码成功后停止解码(保留扫描头)
|
|
|
ScanChannel.stopSingleScan();
|
|
|
},
|
|
|
@@ -85,21 +88,24 @@ class _CageChangePageState extends State<CageChangePage> {
|
|
|
});
|
|
|
},
|
|
|
);
|
|
|
+ _scanTag = 1;
|
|
|
|
|
|
// 进入页面默认使原笼号获取到焦点
|
|
|
- WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
|
- _sourceCageFocusNode.requestFocus();
|
|
|
- });
|
|
|
+ // WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
|
+ // _sourceCageFocusNode.requestFocus();
|
|
|
+ // });
|
|
|
}
|
|
|
|
|
|
@override
|
|
|
void dispose() {
|
|
|
+ // 释放TextEditingControllers
|
|
|
+ _sourceCageController.dispose();
|
|
|
+ _targetCageController.dispose();
|
|
|
+
|
|
|
// 释放FocusNode
|
|
|
_sourceCageFocusNode.dispose();
|
|
|
_targetCageFocusNode.dispose();
|
|
|
|
|
|
- // 释放RFID
|
|
|
- _rfidManager.disposeRfid();
|
|
|
// 手动关闭扫描头,立即释放资源
|
|
|
ScanChannel.closeScanHead();
|
|
|
// 释放通道
|
|
|
@@ -133,15 +139,15 @@ class _CageChangePageState extends State<CageChangePage> {
|
|
|
width: double.infinity,
|
|
|
child: ElevatedButton(
|
|
|
onPressed:
|
|
|
- _sourceCageId != null &&
|
|
|
- _targetCageId != null &&
|
|
|
+ _sourceCageController.text.isNotEmpty &&
|
|
|
+ _targetCageController.text.isNotEmpty &&
|
|
|
_rfids.isNotEmpty
|
|
|
? _handleSubmit
|
|
|
: null,
|
|
|
style: ElevatedButton.styleFrom(
|
|
|
backgroundColor:
|
|
|
- (_sourceCageId != null &&
|
|
|
- _targetCageId != null &&
|
|
|
+ (_sourceCageController.text.isNotEmpty &&
|
|
|
+ _targetCageController.text.isNotEmpty &&
|
|
|
_rfids.isNotEmpty)
|
|
|
? Colors.blue
|
|
|
: Colors.grey,
|
|
|
@@ -200,16 +206,25 @@ class _CageChangePageState extends State<CageChangePage> {
|
|
|
|
|
|
Widget _buildCageSection(int tag) {
|
|
|
String title = '源笼号';
|
|
|
- String? value = _sourceCageId;
|
|
|
- bool isScanning = _isScanningSource;
|
|
|
+ TextEditingController controller = _sourceCageController;
|
|
|
FocusNode focusNode = _sourceCageFocusNode;
|
|
|
+ bool isScanning = _isScanningSource;
|
|
|
+
|
|
|
if (tag != 1) {
|
|
|
tag = 2;
|
|
|
title = '目标笼号';
|
|
|
- value = _targetCageId;
|
|
|
- isScanning = _isScanningTarget;
|
|
|
+ controller = _targetCageController;
|
|
|
focusNode = _targetCageFocusNode;
|
|
|
+ isScanning = _isScanningTarget;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 确保光标在文本末尾
|
|
|
+ if (controller.text.isNotEmpty) {
|
|
|
+ controller.selection = TextSelection.fromPosition(
|
|
|
+ TextPosition(offset: controller.text.length),
|
|
|
+ );
|
|
|
}
|
|
|
+
|
|
|
return Column(
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
children: [
|
|
|
@@ -227,34 +242,31 @@ class _CageChangePageState extends State<CageChangePage> {
|
|
|
// 将Text替换为TextField
|
|
|
child: TextField(
|
|
|
focusNode: focusNode,
|
|
|
- controller: TextEditingController(text: value),
|
|
|
+ controller: controller,
|
|
|
decoration: InputDecoration(
|
|
|
border: InputBorder.none,
|
|
|
- hintText: value ?? '未扫描',
|
|
|
+ hintText: controller.text.isNotEmpty ? null : '未扫描',
|
|
|
hintStyle: TextStyle(
|
|
|
- color: value != null ? Colors.black : Colors.grey,
|
|
|
+ color: controller.text.isNotEmpty
|
|
|
+ ? Colors.black
|
|
|
+ : Colors.grey,
|
|
|
fontSize: 16,
|
|
|
),
|
|
|
),
|
|
|
style: TextStyle(
|
|
|
- color: value != null ? Colors.black : Colors.grey,
|
|
|
+ color: controller.text.isNotEmpty
|
|
|
+ ? Colors.black
|
|
|
+ : Colors.grey,
|
|
|
fontSize: 16,
|
|
|
),
|
|
|
onChanged: (text) {
|
|
|
- // 根据标题判断是哪个输入框
|
|
|
- if (title == '源笼号') {
|
|
|
- setState(() {
|
|
|
- _sourceCageId = text.isEmpty ? null : text;
|
|
|
- });
|
|
|
- } else if (title == '目标笼号') {
|
|
|
- setState(() {
|
|
|
- _targetCageId = text.isEmpty ? null : text;
|
|
|
- });
|
|
|
- }
|
|
|
+ setState(() {
|
|
|
+ // 文本更改由controller自动处理
|
|
|
+ });
|
|
|
},
|
|
|
),
|
|
|
),
|
|
|
- if (value != null) ...[
|
|
|
+ if (controller.text.isNotEmpty) ...[
|
|
|
IconButton(
|
|
|
icon: const Icon(Icons.refresh, size: 20),
|
|
|
onPressed: () {
|
|
|
@@ -283,36 +295,25 @@ class _CageChangePageState extends State<CageChangePage> {
|
|
|
}
|
|
|
|
|
|
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(),
|
|
|
- ),
|
|
|
- ],
|
|
|
+ return VberRfidField(
|
|
|
+ rfids: _rfids,
|
|
|
+ onRfidScanned: (rfid) {
|
|
|
+ setState(() {
|
|
|
+ _rfids.insert(0, rfid);
|
|
|
+ });
|
|
|
+ ToastUtil.success("标签已添加");
|
|
|
+ },
|
|
|
+ multiple: true,
|
|
|
+ label: '鸡数量',
|
|
|
+ multiplePlaceholder: '未扫描',
|
|
|
+ multipleFormat: '已扫描 %d 只鸡',
|
|
|
);
|
|
|
- // return VberRfidField(
|
|
|
- // rfids: _rfids,
|
|
|
- // onRfidScanned: (rfid) {
|
|
|
- // setState(() {
|
|
|
- // _rfids.insert(0, rfid);
|
|
|
- // });
|
|
|
- // ToastUtil.success("标签已添加");
|
|
|
- // },
|
|
|
- // multiple: true,
|
|
|
- // label: '鸡数量',
|
|
|
- // multiplePlaceholder: '未扫描',
|
|
|
- // multipleFormat: '已扫描 %d 只鸡',
|
|
|
- // );
|
|
|
}
|
|
|
|
|
|
void _dealyScanCode(int scanTag, {int? dealy}) async {
|
|
|
dealy ??= 800;
|
|
|
_scanTag = scanTag;
|
|
|
-
|
|
|
+ logger.d("开始扫码: $_scanTag");
|
|
|
await Future.delayed(Duration(milliseconds: dealy));
|
|
|
bool success = await ScanChannel.startScan();
|
|
|
if (!success) {
|
|
|
@@ -330,6 +331,7 @@ class _CageChangePageState extends State<CageChangePage> {
|
|
|
}
|
|
|
});
|
|
|
_scanTag = tag;
|
|
|
+ logger.d("开始扫码: $_scanTag");
|
|
|
bool success = await ScanChannel.startScan();
|
|
|
if (!success) {
|
|
|
ToastUtil.error("扫码失败");
|
|
|
@@ -338,7 +340,11 @@ class _CageChangePageState extends State<CageChangePage> {
|
|
|
|
|
|
void _handleChangeCageCode(int tag) {
|
|
|
setState(() {
|
|
|
- _sourceCageId = null;
|
|
|
+ if (tag == 1) {
|
|
|
+ _sourceCageController.clear();
|
|
|
+ } else {
|
|
|
+ _targetCageController.clear();
|
|
|
+ }
|
|
|
});
|
|
|
_dealyScanCode(tag);
|
|
|
}
|
|
|
@@ -359,8 +365,8 @@ class _CageChangePageState extends State<CageChangePage> {
|
|
|
|
|
|
// 提交后重置表单
|
|
|
setState(() {
|
|
|
- _sourceCageId = null;
|
|
|
- _targetCageId = null;
|
|
|
+ _sourceCageController.clear();
|
|
|
+ _targetCageController.clear();
|
|
|
_rfids.clear();
|
|
|
});
|
|
|
}
|