refactor: unify error feedback system with FeedbackService and StatusBar widget
- Create FeedbackService as centralized sound/vibration dispatcher - Create reusable StatusBar widget with StatusDotColor enum (blue/orange/green/red/yellow/amber) - Extend SoundService with beep/error/alert sound types and loop playback - Remove top feedback banners from both registration and boxing pages - Route all feedback through bottom status bar per PRD requirements - Add sound and vibration feedback to boxing module (was completely missing) - Handle network errors with yellow status, general errors with red - Enhance duplicate dialog with alert loop sound that stops on close - Add beep/error/alert sound file selectors in settings page Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -4,7 +4,9 @@ import 'package:pad_scanner/services/app_config_service.dart';
|
||||
import 'package:pad_scanner/services/scanner_service.dart';
|
||||
import 'package:pad_scanner/services/code_parser.dart';
|
||||
import 'package:pad_scanner/services/api_service.dart';
|
||||
import 'package:pad_scanner/services/feedback_service.dart';
|
||||
import 'package:pad_scanner/pages/boxing_detail_page.dart';
|
||||
import 'package:pad_scanner/widgets/status_bar.dart';
|
||||
|
||||
// === 装箱模式 ===
|
||||
|
||||
@@ -34,6 +36,7 @@ class BoxingPage extends StatefulWidget {
|
||||
class _BoxingPageState extends State<BoxingPage> {
|
||||
final _scannerService = ScannerService();
|
||||
final _apiService = ApiService();
|
||||
final _feedbackService = FeedbackService();
|
||||
|
||||
// 模式
|
||||
BoxingMode _mode = BoxingMode.one2one;
|
||||
@@ -72,9 +75,11 @@ class _BoxingPageState extends State<BoxingPage> {
|
||||
// 重复箱号
|
||||
bool _isDuplicateBoxNo = false;
|
||||
|
||||
// 反馈
|
||||
String? _feedbackMessage;
|
||||
Color? _feedbackColor;
|
||||
// Status bar state
|
||||
StatusDotColor _statusDot = StatusDotColor.blue;
|
||||
String _statusText = '等待扫码';
|
||||
String? _statusOverrideText;
|
||||
StatusDotColor? _statusOverrideDot;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
@@ -88,6 +93,7 @@ class _BoxingPageState extends State<BoxingPage> {
|
||||
_quantityController.dispose();
|
||||
_boxNoFocusNode.dispose();
|
||||
_quantityFocusNode.dispose();
|
||||
_feedbackService.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@@ -105,6 +111,7 @@ class _BoxingPageState extends State<BoxingPage> {
|
||||
}
|
||||
|
||||
void _cycleMode() {
|
||||
_feedbackService.trigger(FeedbackEvent.modeSwitch);
|
||||
setState(() {
|
||||
switch (_mode) {
|
||||
case BoxingMode.one2one:
|
||||
@@ -133,8 +140,8 @@ class _BoxingPageState extends State<BoxingPage> {
|
||||
_lastBoxNo = null;
|
||||
_lastQuantity = null;
|
||||
_isDuplicateBoxNo = false;
|
||||
_feedbackMessage = null;
|
||||
_feedbackColor = null;
|
||||
_statusOverrideText = null;
|
||||
_statusOverrideDot = null;
|
||||
}
|
||||
|
||||
// === 扫码处理 ===
|
||||
@@ -143,7 +150,12 @@ class _BoxingPageState extends State<BoxingPage> {
|
||||
final parsed = CodeParser.parse(result.barcode);
|
||||
|
||||
if (parsed.type != CodeType.zongpaiNo) {
|
||||
_showFeedback('无效码,请重新扫描', isError: true);
|
||||
_feedbackService.trigger(FeedbackEvent.scanInvalid);
|
||||
_showStatusOverride(
|
||||
'无效码,请重新扫描',
|
||||
StatusDotColor.red,
|
||||
const Duration(seconds: 2),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -167,7 +179,7 @@ class _BoxingPageState extends State<BoxingPage> {
|
||||
setState(() {
|
||||
_phase = _Phase.waiting;
|
||||
_zongpaiNo = null;
|
||||
_feedbackMessage = null;
|
||||
_statusOverrideText = null;
|
||||
});
|
||||
_queryBoxInfo(zongpai);
|
||||
}
|
||||
@@ -176,7 +188,12 @@ class _BoxingPageState extends State<BoxingPage> {
|
||||
final configService = AppConfigService();
|
||||
final baseUrl = await configService.getString('api_url') ?? '';
|
||||
if (baseUrl.isEmpty) {
|
||||
_showFeedback('未配置 API 地址,请前往设置', isError: true);
|
||||
_feedbackService.trigger(FeedbackEvent.submitFailure);
|
||||
_showStatusOverride(
|
||||
'未配置 API 地址,请前往设置',
|
||||
StatusDotColor.red,
|
||||
const Duration(seconds: 2),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -188,10 +205,20 @@ class _BoxingPageState extends State<BoxingPage> {
|
||||
if (!mounted) return;
|
||||
|
||||
if (!result.success) {
|
||||
_showFeedback(result.errorMessage ?? '查询失败', isError: true);
|
||||
final isNetwork = result.errorMessage == '网络异常,请检查网络连接';
|
||||
_feedbackService.trigger(
|
||||
isNetwork ? FeedbackEvent.networkError : FeedbackEvent.scanInvalid,
|
||||
);
|
||||
_showStatusOverride(
|
||||
result.errorMessage ?? '查询失败',
|
||||
isNetwork ? StatusDotColor.yellow : StatusDotColor.red,
|
||||
const Duration(seconds: 2),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
_feedbackService.trigger(FeedbackEvent.scanValid);
|
||||
|
||||
setState(() {
|
||||
_zongpaiNo = zongpai;
|
||||
_paichanNo = result.paichanNo;
|
||||
@@ -200,7 +227,7 @@ class _BoxingPageState extends State<BoxingPage> {
|
||||
_maxBoxNo = result.maxBoxNo;
|
||||
_phase = _Phase.scanned;
|
||||
_isDuplicateBoxNo = false;
|
||||
_feedbackMessage = null;
|
||||
_statusOverrideText = null;
|
||||
|
||||
// 多码一箱:记录已扫描列表
|
||||
if (_mode == BoxingMode.many2one && !_scannedZongpais.contains(zongpai)) {
|
||||
@@ -215,21 +242,17 @@ class _BoxingPageState extends State<BoxingPage> {
|
||||
void _applyAutoFill() {
|
||||
switch (_mode) {
|
||||
case BoxingMode.one2one:
|
||||
// 箱号 = max+1, 数量 = ERP 数量
|
||||
_boxNoController.text = (_maxBoxNo + 1).toString();
|
||||
_quantityController.text = (_erpQuantity ?? 0).toString();
|
||||
case BoxingMode.one2many:
|
||||
if (_lastBoxNo != null) {
|
||||
// 继续添加:箱号 = 上次+1, 数量 = 上次值
|
||||
_boxNoController.text = (_lastBoxNo! + 1).toString();
|
||||
_quantityController.text = _lastQuantity?.toString() ?? '';
|
||||
} else {
|
||||
// 首次:箱号 = max+1, 数量不填
|
||||
_boxNoController.text = (_maxBoxNo + 1).toString();
|
||||
_quantityController.clear();
|
||||
}
|
||||
case BoxingMode.many2one:
|
||||
// 箱号 = max+1 (首次) 或锁定, 数量 = ERP 数量
|
||||
if (!_boxNoLocked) {
|
||||
_boxNoController.text = (_maxBoxNo + 1).toString();
|
||||
_boxNoLocked = true;
|
||||
@@ -269,7 +292,12 @@ class _BoxingPageState extends State<BoxingPage> {
|
||||
final configService = AppConfigService();
|
||||
final baseUrl = await configService.getString('api_url') ?? '';
|
||||
if (baseUrl.isEmpty) {
|
||||
_showFeedback('未配置 API 地址,请前往设置', isError: true);
|
||||
_feedbackService.trigger(FeedbackEvent.submitFailure);
|
||||
_showStatusOverride(
|
||||
'未配置 API 地址,请前往设置',
|
||||
StatusDotColor.red,
|
||||
const Duration(seconds: 2),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -298,24 +326,39 @@ class _BoxingPageState extends State<BoxingPage> {
|
||||
if (result.success) {
|
||||
_onSubmitSuccess(boxNo, quantity);
|
||||
} else if (result.isDuplicate) {
|
||||
_feedbackService.trigger(FeedbackEvent.duplicateBoxNo);
|
||||
if (_mode == BoxingMode.one2one) {
|
||||
_showFeedback('该总排号已绑定箱号 ${result.boxNo ?? "?"},请勿重复装箱', isError: true);
|
||||
_showStatusOverride(
|
||||
'该总排号已绑定箱号 ${result.boxNo ?? "?"},请勿重复装箱',
|
||||
StatusDotColor.red,
|
||||
const Duration(seconds: 2),
|
||||
);
|
||||
} else {
|
||||
_showFeedback(
|
||||
_showStatusOverride(
|
||||
'排产号 ${result.paichanNo ?? ""} 下箱号 ${result.boxNo ?? ""} 已存在',
|
||||
isError: true,
|
||||
StatusDotColor.amber,
|
||||
const Duration(seconds: 2),
|
||||
);
|
||||
}
|
||||
} else {
|
||||
_showFeedback(result.errorMessage ?? '提交失败', isError: true);
|
||||
final isNetwork = result.errorMessage == '网络异常,请检查网络连接';
|
||||
_feedbackService.trigger(
|
||||
isNetwork ? FeedbackEvent.networkError : FeedbackEvent.submitFailure,
|
||||
);
|
||||
_showStatusOverride(
|
||||
result.errorMessage ?? '提交失败',
|
||||
isNetwork ? StatusDotColor.yellow : StatusDotColor.red,
|
||||
const Duration(seconds: 2),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void _onSubmitSuccess(int boxNo, int quantity) {
|
||||
_feedbackService.trigger(FeedbackEvent.submitSuccess);
|
||||
|
||||
setState(() {
|
||||
_lastBoxNo = boxNo;
|
||||
_lastQuantity = quantity;
|
||||
// 刷新已有箱号列表(将新记录加入本地列表)
|
||||
_existingBoxes = List.from(_existingBoxes)
|
||||
..add(
|
||||
BoxDetailData(
|
||||
@@ -328,56 +371,54 @@ class _BoxingPageState extends State<BoxingPage> {
|
||||
|
||||
switch (_mode) {
|
||||
case BoxingMode.one2one:
|
||||
// 显示成功 1.5s → 重置
|
||||
_showFeedback('装箱成功', isError: false);
|
||||
_showStatusOverride('装箱成功', StatusDotColor.green, const Duration(milliseconds: 1500));
|
||||
Future.delayed(const Duration(milliseconds: 1500), () {
|
||||
if (mounted) setState(() => _resetState());
|
||||
});
|
||||
|
||||
case BoxingMode.one2many:
|
||||
// 进入已提交状态,等待"继续添加"或"返回"
|
||||
setState(() {
|
||||
_phase = _Phase.submitted;
|
||||
_zongpaiNo = null;
|
||||
});
|
||||
_showFeedback('装箱成功', isError: false);
|
||||
_showStatusOverride('装箱成功,可继续添加或返回', StatusDotColor.green, const Duration(milliseconds: 1500));
|
||||
|
||||
case BoxingMode.many2one:
|
||||
// 进入已提交状态,自动等待下一个扫码
|
||||
setState(() {
|
||||
_phase = _Phase.submitted;
|
||||
_zongpaiNo = null;
|
||||
});
|
||||
_showFeedback('装箱成功,请扫描下一个总排号', isError: false);
|
||||
_showStatusOverride('装箱成功,请扫描下一个总排号', StatusDotColor.green, const Duration(milliseconds: 1500));
|
||||
}
|
||||
}
|
||||
|
||||
// === 操作按钮 ===
|
||||
|
||||
void _onContinueAdding() {
|
||||
// 一码多箱的继续添加
|
||||
setState(() {
|
||||
_phase = _Phase.waiting;
|
||||
_feedbackMessage = null;
|
||||
_statusOverrideText = null;
|
||||
});
|
||||
// 预填值会在下次扫码后的 _applyAutoFill 中处理
|
||||
// 但这里需要手动触发,因为不重新扫码
|
||||
// 用户需要扫描同一个总排号(或其他总排号)
|
||||
}
|
||||
|
||||
void _onGoBack() {
|
||||
setState(() => _resetState());
|
||||
}
|
||||
|
||||
// === 反馈 ===
|
||||
// === Status bar management ===
|
||||
|
||||
void _showFeedback(String message, {bool isError = false}) {
|
||||
void _showStatusOverride(String text, StatusDotColor dot, Duration duration) {
|
||||
setState(() {
|
||||
_feedbackMessage = message;
|
||||
_feedbackColor = isError ? Colors.red.shade700 : Colors.green.shade700;
|
||||
_statusOverrideText = text;
|
||||
_statusOverrideDot = dot;
|
||||
});
|
||||
Future.delayed(const Duration(seconds: 2), () {
|
||||
if (mounted) setState(() => _feedbackMessage = null);
|
||||
Future.delayed(duration, () {
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_statusOverrideText = null;
|
||||
_statusOverrideDot = null;
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -397,44 +438,41 @@ class _BoxingPageState extends State<BoxingPage> {
|
||||
|
||||
// === 状态文字 ===
|
||||
|
||||
String get _statusText {
|
||||
if (_isSubmitting) return '正在提交…';
|
||||
if (_feedbackMessage != null) return _feedbackMessage!;
|
||||
void _updateBaseStatus() {
|
||||
if (_isSubmitting) {
|
||||
_statusDot = StatusDotColor.orange;
|
||||
_statusText = '正在提交…';
|
||||
return;
|
||||
}
|
||||
if (_isDuplicateBoxNo && _phase == _Phase.scanned) {
|
||||
_statusDot = StatusDotColor.amber;
|
||||
_statusText = '箱号 ${_boxNoController.text} 已存在,请重新输入';
|
||||
return;
|
||||
}
|
||||
switch (_phase) {
|
||||
case _Phase.waiting:
|
||||
_statusDot = StatusDotColor.blue;
|
||||
if (_mode == BoxingMode.many2one && _boxNoLocked) {
|
||||
return '请扫描下一个总排号';
|
||||
_statusText = '请扫描下一个总排号';
|
||||
} else {
|
||||
_statusText = '等待扫码';
|
||||
}
|
||||
return '等待扫码';
|
||||
case _Phase.scanned:
|
||||
return '请确认信息并提交';
|
||||
_statusDot = StatusDotColor.blue;
|
||||
_statusText = '请确认信息并提交';
|
||||
case _Phase.submitted:
|
||||
_statusDot = StatusDotColor.green;
|
||||
switch (_mode) {
|
||||
case BoxingMode.one2many:
|
||||
return '装箱成功,可继续添加或返回';
|
||||
_statusText = '装箱成功,可继续添加或返回';
|
||||
case BoxingMode.many2one:
|
||||
return '请扫描下一个总排号';
|
||||
_statusText = '请扫描下一个总排号';
|
||||
case BoxingMode.one2one:
|
||||
return '';
|
||||
_statusText = '';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Color get _statusDotColor {
|
||||
if (_isSubmitting) return Colors.orange;
|
||||
if (_feedbackMessage != null) {
|
||||
return _feedbackColor == Colors.red.shade700 ? Colors.red : Colors.green;
|
||||
}
|
||||
switch (_phase) {
|
||||
case _Phase.waiting:
|
||||
return Colors.blue;
|
||||
case _Phase.scanned:
|
||||
return Colors.green;
|
||||
case _Phase.submitted:
|
||||
return Colors.green;
|
||||
}
|
||||
}
|
||||
|
||||
// === Build ===
|
||||
|
||||
@override
|
||||
@@ -444,11 +482,14 @@ class _BoxingPageState extends State<BoxingPage> {
|
||||
final showActionButtons =
|
||||
_phase == _Phase.submitted && _mode != BoxingMode.one2one;
|
||||
|
||||
_updateBaseStatus();
|
||||
final effectiveDot = _statusOverrideDot ?? _statusDot;
|
||||
final effectiveText = _statusOverrideText ?? _statusText;
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('装箱编号'),
|
||||
actions: [
|
||||
// 模式切换按钮
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(right: 4),
|
||||
child: TextButton.icon(
|
||||
@@ -466,20 +507,7 @@ class _BoxingPageState extends State<BoxingPage> {
|
||||
),
|
||||
body: Column(
|
||||
children: [
|
||||
// 反馈提示条
|
||||
if (_feedbackMessage != null)
|
||||
Container(
|
||||
width: double.infinity,
|
||||
padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 16),
|
||||
color: _feedbackColor,
|
||||
child: Text(
|
||||
_feedbackMessage!,
|
||||
style: const TextStyle(color: Colors.white, fontSize: 14),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
),
|
||||
|
||||
// 重复箱号警告条
|
||||
// 重复箱号警告条(保留,因为这是输入区关联的即时反馈)
|
||||
if (_isDuplicateBoxNo && _phase == _Phase.scanned)
|
||||
Container(
|
||||
width: double.infinity,
|
||||
@@ -547,31 +575,7 @@ class _BoxingPageState extends State<BoxingPage> {
|
||||
),
|
||||
|
||||
// 底部状态栏
|
||||
Container(
|
||||
width: double.infinity,
|
||||
padding: const EdgeInsets.symmetric(vertical: 12, horizontal: 16),
|
||||
color: colorScheme.surfaceContainerHighest,
|
||||
child: Row(
|
||||
children: [
|
||||
Container(
|
||||
width: 8,
|
||||
height: 8,
|
||||
decoration: BoxDecoration(
|
||||
color: _statusDotColor,
|
||||
shape: BoxShape.circle,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Expanded(
|
||||
child: Text(
|
||||
_statusText,
|
||||
style: const TextStyle(fontSize: 14),
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
StatusBar(dotColor: effectiveDot, text: effectiveText),
|
||||
],
|
||||
),
|
||||
);
|
||||
@@ -581,7 +585,6 @@ class _BoxingPageState extends State<BoxingPage> {
|
||||
|
||||
Widget _buildScanArea(bool isWaiting) {
|
||||
if (isWaiting) {
|
||||
// 等待扫码:置灰提示
|
||||
return Container(
|
||||
width: double.infinity,
|
||||
padding: const EdgeInsets.all(16),
|
||||
@@ -603,9 +606,7 @@ class _BoxingPageState extends State<BoxingPage> {
|
||||
);
|
||||
}
|
||||
|
||||
// 已扫码:显示总排号
|
||||
if (_mode == BoxingMode.many2one && _scannedZongpais.isNotEmpty) {
|
||||
// 多码一箱:显示已扫描列表
|
||||
return Wrap(
|
||||
spacing: 6,
|
||||
runSpacing: 4,
|
||||
@@ -624,7 +625,6 @@ class _BoxingPageState extends State<BoxingPage> {
|
||||
);
|
||||
}
|
||||
|
||||
// 单个总排号
|
||||
return Container(
|
||||
width: double.infinity,
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 12),
|
||||
@@ -662,7 +662,6 @@ class _BoxingPageState extends State<BoxingPage> {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
// 排产号
|
||||
Text(
|
||||
'排产号:',
|
||||
style: TextStyle(
|
||||
@@ -681,7 +680,6 @@ class _BoxingPageState extends State<BoxingPage> {
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
|
||||
// 已有箱数 + 最大箱号 + 详情按钮
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
|
||||
Reference in New Issue
Block a user