Compare commits

...

2 Commits

Author SHA1 Message Date
Misaka_Company
62583510b2 docs: add CLAUDE.md with app installation rules
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-05-12 17:33:41 +08:00
Misaka_Company
1417c8baf9 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>
2026-05-12 17:15:28 +08:00
8 changed files with 1085 additions and 517 deletions

17
CLAUDE.md Normal file
View File

@@ -0,0 +1,17 @@
# pad_scanner CLAUDE.md
## App Installation Rules
**Always use `adb install -r` to install the app. Never use `flutter install`.**
`flutter install` uninstalls the old version first, which wipes app configuration (API URL, sound file paths, etc.). `adb install -r` performs an in-place upgrade that preserves all app data.
### Installation Steps
```bash
# 1. Build
flutter build apk --release
# 2. Install (in-place upgrade, preserves config)
adb install -r build/app/outputs/flutter-apk/app-release.apk
```

View File

@@ -0,0 +1,261 @@
# 错误反馈机制重构计划
> 版本v1.0
> 日期2026-05-12
> 状态:已完成
---
## 1. 背景与目标
### 1.1 当前问题
对照 PRD上架登记模块 v1.0 + 装箱编号模块 v1.3)的要求,当前 Flutter 应用在错误反馈方面存在以下差距:
| # | 问题 | 影响范围 |
|---|------|---------|
| 1 | **装箱模块完全没有声音和振动反馈**`boxing_page.dart` 未引入 `SoundService``Vibration` | 装箱编号 |
| 2 | **反馈横幅位置错误** — 当前在页面顶部显示反馈横幅PRD 要求统一由底部状态栏承载所有反馈文案 | 上架登记、装箱编号 |
| 3 | **底部状态栏缺少黄色(网络异常)和红色(错误)状态** — 注册页状态栏圆点仅使用蓝/橙/绿三色,错误信息只出现在顶部横幅 | 上架登记 |
| 4 | **反馈信息重复显示** — 顶部横幅和底部状态栏同时显示类似内容,造成冗余 | 上架登记、装箱编号 |
| 5 | **声音类型不足**`SoundService` 只有 success/failure 两种PRD 要求区分"扫描提示音"、"错误音"、"成功音"、"连续错误音" | 全局 |
| 6 | **重复上架弹窗缺少声音差异化** — PRD 要求"连续错误音"+"连续振动",当前只是普通失败音 | 上架登记 |
> LED 反馈暂时不实现,后续单独处理。
### 1.2 重构目标
- **统一反馈入口**:创建集中的 `FeedbackService`,管理声音/振动三通道反馈
- **消除重复**:移除顶部反馈横幅,所有反馈文案统一由底部状态栏显示
- **补全缺失**:为装箱模块补齐声音/振动反馈
- **符合 PRD**:状态栏圆点颜色、文案、持续时间与 PRD 表格一一对应
---
## 2. PRD 反馈要求汇总
### 2.1 上架登记模块PRD 第 7 节)
| 事件 | 屏幕(底部状态栏) | 声音 | 振动 |
|------|-------------------|------|------|
| 扫入有效总排号 | 总排号字段填入,绿色高亮 | 短促提示音 | 短震 |
| 扫入有效货位号 | 货位号字段填入,绿色高亮 | 短促提示音 | 短震 |
| 扫入无效码 | 红色提示 2 秒:"无效码:{值}" | 错误音 | 短震 |
| 提交成功 | 绿色提示 1.5 秒 | 成功音 | 长震 |
| 提交失败 | 红色提示 2 秒 | 失败音 | 短震 |
| 重复上架错误 | 红色错误弹窗(需手动关闭) | 连续错误音 | 连续震 |
| 网络异常 | 黄色提示:"网络异常,请检查网络连接" | 失败音 | 短震 |
### 2.2 装箱编号模块PRD 第 9 节)
| 事件 | 屏幕(底部状态栏) | 声音 | 振动 |
|------|-------------------|------|------|
| 扫入有效总排号 | 显示排产号信息,绿色对勾 | 短促提示音 | 短震 |
| 扫入无效码 | 红色提示:"无效码,请重新扫描" | 错误音 | 短震 |
| 提交成功 | 绿色提示 1.5 秒 | 成功音 | 长震 |
| 重复箱号 | amber 提示 + 输入框 amber 边框 | 失败音 | 短震 |
| 网络异常 | 黄色提示:"网络异常,请检查网络连接" | 失败音 | 短震 |
| 切换装箱模式 | 模式控件高亮切换 | 短促提示音 | — |
### 2.3 底部状态栏圆点颜色规范
| 状态 | 圆点颜色 |
|------|---------|
| 空闲等待 | 蓝色 |
| 提交中 | 橙色 |
| 成功 | 绿色 |
| 错误(无效码/提交失败/重复) | 红色 |
| 网络异常 | 黄色 |
| 警告(重复箱号) | amber |
---
## 3. 架构设计
### 3.1 新增 `FeedbackService`
创建 `lib/services/feedback_service.dart`,作为**唯一的反馈调度中心**。
```
FeedbackService
├── trigger(FeedbackEvent event) ← 统一入口
├── _playSound(SoundType type) ← 声音通道
├── _vibrate(VibrationPattern p) ← 振动通道
└── 底部状态栏状态由各 Page State 管理FeedbackService 不直接操控 Widget
```
**FeedbackEvent 枚举:**
```dart
enum FeedbackEvent {
scanValid, // 扫入有效码 → 短促提示音 + 短震
scanInvalid, // 扫入无效码 → 错误音 + 短震
submitSuccess, // 提交成功 → 成功音 + 长震
submitFailure, // 提交失败 → 失败音 + 短震
duplicateError, // 重复上架 → 连续错误音 + 连续震
networkError, // 网络异常 → 失败音 + 短震
duplicateBoxNo, // 重复箱号 → 失败音 + 短震
modeSwitch, // 模式切换 → 短促提示音
}
```
### 3.2 声音扩展
扩展 `SoundService`,增加声音类型:
| 声音类型 | 方法名 | 用途 |
|---------|--------|------|
| 短促提示音 | `playBeep()` | 有效扫码、模式切换 |
| 错误音 | `playError()` | 无效码 |
| 成功音 | `playSuccess()` | 提交成功(保留现有) |
| 失败音 | `playFailure()` | 提交失败、网络异常(保留现有) |
| 连续错误音 | `playAlertLoop()` | 重复上架弹窗(循环播放直到弹窗关闭) |
**实现策略**
-`SoundService` 中增加 `beep``error``alert` 三个可配置路径
- 设置页面增加对应的文件选择器
- 若路径未配置则静默跳过(不阻塞业务)
### 3.3 底部状态栏组件化
抽取 `lib/widgets/status_bar.dart` 为独立 Widget
```dart
enum StatusDotColor { blue, orange, green, red, yellow, amber }
class StatusBar extends StatelessWidget {
final StatusDotColor dotColor;
final String text;
// ...
}
```
所有页面统一使用此组件,消除重复代码。
---
## 4. 文件变更清单
### 4.1 新增文件
| 文件路径 | 说明 |
|---------|------|
| `lib/services/feedback_service.dart` | 反馈调度中心,管理声音/振动 |
| `lib/widgets/status_bar.dart` | 底部状态栏可复用组件 |
### 4.2 修改文件
| 文件路径 | 变更内容 |
|---------|---------|
| `lib/services/sound_service.dart` | 增加 `beep`/`error`/`alert` 声音类型和配置 |
| `lib/pages/registration_page.dart` | ① 移除顶部反馈横幅 ② 状态栏增加红/黄颜色 ③ 引入 FeedbackService ④ 重复上架弹窗调用连续错误音 |
| `lib/pages/boxing_page.dart` | ① 移除顶部反馈横幅 ② 引入 FeedbackService ③ 增加声音/振动反馈 ④ 状态栏统一使用 StatusBar 组件 |
| `lib/pages/settings_page.dart` | 增加声音文件配置项beep/error/alert |
### 4.3 不变文件
| 文件路径 | 原因 |
|---------|------|
| `lib/services/api_service.dart` | API 层只负责数据传输,不涉及反馈 |
| `lib/services/code_parser.dart` | 解析逻辑不变 |
| `lib/services/scanner_service.dart` | 扫码硬件层不变 |
| `lib/pages/home_page.dart` | 不涉及作业反馈 |
| `lib/pages/boxing_detail_page.dart` | 只读展示页,无反馈交互 |
---
## 5. 实施步骤
### 步骤 1创建 `FeedbackService`(核心)
**文件**`lib/services/feedback_service.dart`
- 定义 `FeedbackEvent` 枚举
- 根据 event 映射声音类型、振动模式
- 调用 `SoundService``Vibration`
**预估改动**~70 行新代码
### 步骤 2扩展 `SoundService`
**文件**`lib/services/sound_service.dart`
- 新增 `playBeep()``playError()``playAlertLoop()``stopAlert()` 方法
- 新增 `beep_path``error_path``alert_path` 配置存取
- 所有播放方法在路径未配置时静默返回
**预估改动**~40 行新增
### 步骤 3创建 `StatusBar` 组件
**文件**`lib/widgets/status_bar.dart`
- 抽取底部状态栏为独立 Widget
- 接收 `dotColor`(枚举)和 `text` 参数
- 统一样式:圆点 + 文字,背景色使用 `surfaceContainerHighest`
**预估改动**~40 行新代码
### 步骤 4重构上架登记页
**文件**`lib/pages/registration_page.dart`
变更清单:
1. **删除**顶部反馈横幅相关代码(`_snackbarMessage``_snackbarColor``_showFeedback()` 方法中的横幅渲染)
2. **引入** `FeedbackService`,在以下时机调用 `trigger()`
- `_onScan` 无效码 → `scanInvalid`
- `_onScan` 有效码 → `scanValid`
- `_submitOne` 成功 → `submitSuccess`
- `_submitOne` 失败 → `submitFailure`
- `_submitOne` 重复 → `duplicateError`
- catch 网络异常 → `networkError`
3. **修改**底部状态栏:
- 状态栏圆点颜色增加红色和黄色
- 错误文案在状态栏中显示(不再在横幅中)
- 移除 `_successMessage` 单独字段,统一由状态栏管理
4. **替换** 内联状态栏 Widget 为 `StatusBar` 组件
5. **修改** `_showDuplicateDialog`:打开时触发 `duplicateError`(连续音/震),关闭时停止
**预估改动**:净减 ~30 行(移除横幅 > 新增 trigger 调用)
### 步骤 5重构装箱编号页
**文件**`lib/pages/boxing_page.dart`
变更清单:
1. **删除**顶部反馈横幅相关代码
2. **删除** 内联底部状态栏代码,替换为 `StatusBar` 组件
3. **引入** `FeedbackService`,在以下时机调用 `trigger()`
- `_onScan` 无效码 → `scanInvalid`
- `_queryBoxInfo` 成功 → `scanValid`
- `_submit` 成功 → `submitSuccess`
- `_submit` 失败 → `submitFailure`
- `_submit` 重复箱号 → `duplicateBoxNo`
- catch 网络异常 → `networkError`
- `_cycleMode``modeSwitch`
4. **修改**状态栏颜色逻辑,增加红色/黄色/amber
**预估改动**:净减 ~20 行
### 步骤 6设置页扩展**文件**`lib/pages/settings_page.dart`- 新增三个声音文件选择器提示音beep、错误音error、警报音alert- 保持与现有 success/failure 选择器一致的 UI 风格**预估改动**~60 行新增
---
## 6. 风险与约束
| 风险 | 缓解措施 |
|------|---------|
| LED 功能依赖设备硬件 | `LedService` 全面包裹 try-catch不支持时静默降级 |
| 声音文件路径未配置 | 所有 `play*()` 方法在路径为空时静默返回 |
| 连续错误音循环播放 | `playAlertLoop()` 使用循环计数或时长上限,`stopAlert()` 确保可停止 |
| 振动权限 | Android 需要 `VIBRATE` 权限,检查 `AndroidManifest.xml` 是否已声明 |
---
## 7. 不在本次范围内
- 后端 API 错误格式调整PRD 已定义,后端自行对齐)
- 操作日志与操作人员记录
- 离线缓存与联网同步
- 新模块开发
- LED 反馈(后续单独处理)

View File

@@ -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/scanner_service.dart';
import 'package:pad_scanner/services/code_parser.dart'; import 'package:pad_scanner/services/code_parser.dart';
import 'package:pad_scanner/services/api_service.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/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> { class _BoxingPageState extends State<BoxingPage> {
final _scannerService = ScannerService(); final _scannerService = ScannerService();
final _apiService = ApiService(); final _apiService = ApiService();
final _feedbackService = FeedbackService();
// 模式 // 模式
BoxingMode _mode = BoxingMode.one2one; BoxingMode _mode = BoxingMode.one2one;
@@ -72,9 +75,11 @@ class _BoxingPageState extends State<BoxingPage> {
// 重复箱号 // 重复箱号
bool _isDuplicateBoxNo = false; bool _isDuplicateBoxNo = false;
// 反馈 // Status bar state
String? _feedbackMessage; StatusDotColor _statusDot = StatusDotColor.blue;
Color? _feedbackColor; String _statusText = '等待扫码';
String? _statusOverrideText;
StatusDotColor? _statusOverrideDot;
@override @override
void initState() { void initState() {
@@ -88,6 +93,7 @@ class _BoxingPageState extends State<BoxingPage> {
_quantityController.dispose(); _quantityController.dispose();
_boxNoFocusNode.dispose(); _boxNoFocusNode.dispose();
_quantityFocusNode.dispose(); _quantityFocusNode.dispose();
_feedbackService.dispose();
super.dispose(); super.dispose();
} }
@@ -105,6 +111,7 @@ class _BoxingPageState extends State<BoxingPage> {
} }
void _cycleMode() { void _cycleMode() {
_feedbackService.trigger(FeedbackEvent.modeSwitch);
setState(() { setState(() {
switch (_mode) { switch (_mode) {
case BoxingMode.one2one: case BoxingMode.one2one:
@@ -133,8 +140,8 @@ class _BoxingPageState extends State<BoxingPage> {
_lastBoxNo = null; _lastBoxNo = null;
_lastQuantity = null; _lastQuantity = null;
_isDuplicateBoxNo = false; _isDuplicateBoxNo = false;
_feedbackMessage = null; _statusOverrideText = null;
_feedbackColor = null; _statusOverrideDot = null;
} }
// === 扫码处理 === // === 扫码处理 ===
@@ -143,7 +150,12 @@ class _BoxingPageState extends State<BoxingPage> {
final parsed = CodeParser.parse(result.barcode); final parsed = CodeParser.parse(result.barcode);
if (parsed.type != CodeType.zongpaiNo) { if (parsed.type != CodeType.zongpaiNo) {
_showFeedback('无效码,请重新扫描', isError: true); _feedbackService.trigger(FeedbackEvent.scanInvalid);
_showStatusOverride(
'无效码,请重新扫描',
StatusDotColor.red,
const Duration(seconds: 2),
);
return; return;
} }
@@ -167,7 +179,7 @@ class _BoxingPageState extends State<BoxingPage> {
setState(() { setState(() {
_phase = _Phase.waiting; _phase = _Phase.waiting;
_zongpaiNo = null; _zongpaiNo = null;
_feedbackMessage = null; _statusOverrideText = null;
}); });
_queryBoxInfo(zongpai); _queryBoxInfo(zongpai);
} }
@@ -176,7 +188,12 @@ class _BoxingPageState extends State<BoxingPage> {
final configService = AppConfigService(); final configService = AppConfigService();
final baseUrl = await configService.getString('api_url') ?? ''; final baseUrl = await configService.getString('api_url') ?? '';
if (baseUrl.isEmpty) { if (baseUrl.isEmpty) {
_showFeedback('未配置 API 地址,请前往设置', isError: true); _feedbackService.trigger(FeedbackEvent.submitFailure);
_showStatusOverride(
'未配置 API 地址,请前往设置',
StatusDotColor.red,
const Duration(seconds: 2),
);
return; return;
} }
@@ -188,10 +205,20 @@ class _BoxingPageState extends State<BoxingPage> {
if (!mounted) return; if (!mounted) return;
if (!result.success) { 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; return;
} }
_feedbackService.trigger(FeedbackEvent.scanValid);
setState(() { setState(() {
_zongpaiNo = zongpai; _zongpaiNo = zongpai;
_paichanNo = result.paichanNo; _paichanNo = result.paichanNo;
@@ -200,7 +227,7 @@ class _BoxingPageState extends State<BoxingPage> {
_maxBoxNo = result.maxBoxNo; _maxBoxNo = result.maxBoxNo;
_phase = _Phase.scanned; _phase = _Phase.scanned;
_isDuplicateBoxNo = false; _isDuplicateBoxNo = false;
_feedbackMessage = null; _statusOverrideText = null;
// 多码一箱:记录已扫描列表 // 多码一箱:记录已扫描列表
if (_mode == BoxingMode.many2one && !_scannedZongpais.contains(zongpai)) { if (_mode == BoxingMode.many2one && !_scannedZongpais.contains(zongpai)) {
@@ -215,21 +242,17 @@ class _BoxingPageState extends State<BoxingPage> {
void _applyAutoFill() { void _applyAutoFill() {
switch (_mode) { switch (_mode) {
case BoxingMode.one2one: case BoxingMode.one2one:
// 箱号 = max+1, 数量 = ERP 数量
_boxNoController.text = (_maxBoxNo + 1).toString(); _boxNoController.text = (_maxBoxNo + 1).toString();
_quantityController.text = (_erpQuantity ?? 0).toString(); _quantityController.text = (_erpQuantity ?? 0).toString();
case BoxingMode.one2many: case BoxingMode.one2many:
if (_lastBoxNo != null) { if (_lastBoxNo != null) {
// 继续添加:箱号 = 上次+1, 数量 = 上次值
_boxNoController.text = (_lastBoxNo! + 1).toString(); _boxNoController.text = (_lastBoxNo! + 1).toString();
_quantityController.text = _lastQuantity?.toString() ?? ''; _quantityController.text = _lastQuantity?.toString() ?? '';
} else { } else {
// 首次:箱号 = max+1, 数量不填
_boxNoController.text = (_maxBoxNo + 1).toString(); _boxNoController.text = (_maxBoxNo + 1).toString();
_quantityController.clear(); _quantityController.clear();
} }
case BoxingMode.many2one: case BoxingMode.many2one:
// 箱号 = max+1 (首次) 或锁定, 数量 = ERP 数量
if (!_boxNoLocked) { if (!_boxNoLocked) {
_boxNoController.text = (_maxBoxNo + 1).toString(); _boxNoController.text = (_maxBoxNo + 1).toString();
_boxNoLocked = true; _boxNoLocked = true;
@@ -269,7 +292,12 @@ class _BoxingPageState extends State<BoxingPage> {
final configService = AppConfigService(); final configService = AppConfigService();
final baseUrl = await configService.getString('api_url') ?? ''; final baseUrl = await configService.getString('api_url') ?? '';
if (baseUrl.isEmpty) { if (baseUrl.isEmpty) {
_showFeedback('未配置 API 地址,请前往设置', isError: true); _feedbackService.trigger(FeedbackEvent.submitFailure);
_showStatusOverride(
'未配置 API 地址,请前往设置',
StatusDotColor.red,
const Duration(seconds: 2),
);
return; return;
} }
@@ -298,24 +326,39 @@ class _BoxingPageState extends State<BoxingPage> {
if (result.success) { if (result.success) {
_onSubmitSuccess(boxNo, quantity); _onSubmitSuccess(boxNo, quantity);
} else if (result.isDuplicate) { } else if (result.isDuplicate) {
_feedbackService.trigger(FeedbackEvent.duplicateBoxNo);
if (_mode == BoxingMode.one2one) { if (_mode == BoxingMode.one2one) {
_showFeedback('该总排号已绑定箱号 ${result.boxNo ?? "?"},请勿重复装箱', isError: true); _showStatusOverride(
'该总排号已绑定箱号 ${result.boxNo ?? "?"},请勿重复装箱',
StatusDotColor.red,
const Duration(seconds: 2),
);
} else { } else {
_showFeedback( _showStatusOverride(
'排产号 ${result.paichanNo ?? ""} 下箱号 ${result.boxNo ?? ""} 已存在', '排产号 ${result.paichanNo ?? ""} 下箱号 ${result.boxNo ?? ""} 已存在',
isError: true, StatusDotColor.amber,
const Duration(seconds: 2),
); );
} }
} else { } 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) { void _onSubmitSuccess(int boxNo, int quantity) {
_feedbackService.trigger(FeedbackEvent.submitSuccess);
setState(() { setState(() {
_lastBoxNo = boxNo; _lastBoxNo = boxNo;
_lastQuantity = quantity; _lastQuantity = quantity;
// 刷新已有箱号列表(将新记录加入本地列表)
_existingBoxes = List.from(_existingBoxes) _existingBoxes = List.from(_existingBoxes)
..add( ..add(
BoxDetailData( BoxDetailData(
@@ -328,56 +371,54 @@ class _BoxingPageState extends State<BoxingPage> {
switch (_mode) { switch (_mode) {
case BoxingMode.one2one: case BoxingMode.one2one:
// 显示成功 1.5s → 重置 _showStatusOverride('装箱成功', StatusDotColor.green, const Duration(milliseconds: 1500));
_showFeedback('装箱成功', isError: false);
Future.delayed(const Duration(milliseconds: 1500), () { Future.delayed(const Duration(milliseconds: 1500), () {
if (mounted) setState(() => _resetState()); if (mounted) setState(() => _resetState());
}); });
case BoxingMode.one2many: case BoxingMode.one2many:
// 进入已提交状态,等待"继续添加"或"返回"
setState(() { setState(() {
_phase = _Phase.submitted; _phase = _Phase.submitted;
_zongpaiNo = null; _zongpaiNo = null;
}); });
_showFeedback('装箱成功', isError: false); _showStatusOverride('装箱成功,可继续添加或返回', StatusDotColor.green, const Duration(milliseconds: 1500));
case BoxingMode.many2one: case BoxingMode.many2one:
// 进入已提交状态,自动等待下一个扫码
setState(() { setState(() {
_phase = _Phase.submitted; _phase = _Phase.submitted;
_zongpaiNo = null; _zongpaiNo = null;
}); });
_showFeedback('装箱成功,请扫描下一个总排号', isError: false); _showStatusOverride('装箱成功,请扫描下一个总排号', StatusDotColor.green, const Duration(milliseconds: 1500));
} }
} }
// === 操作按钮 === // === 操作按钮 ===
void _onContinueAdding() { void _onContinueAdding() {
// 一码多箱的继续添加
setState(() { setState(() {
_phase = _Phase.waiting; _phase = _Phase.waiting;
_feedbackMessage = null; _statusOverrideText = null;
}); });
// 预填值会在下次扫码后的 _applyAutoFill 中处理
// 但这里需要手动触发,因为不重新扫码
// 用户需要扫描同一个总排号(或其他总排号)
} }
void _onGoBack() { void _onGoBack() {
setState(() => _resetState()); setState(() => _resetState());
} }
// === 反馈 === // === Status bar management ===
void _showFeedback(String message, {bool isError = false}) { void _showStatusOverride(String text, StatusDotColor dot, Duration duration) {
setState(() { setState(() {
_feedbackMessage = message; _statusOverrideText = text;
_feedbackColor = isError ? Colors.red.shade700 : Colors.green.shade700; _statusOverrideDot = dot;
}); });
Future.delayed(const Duration(seconds: 2), () { Future.delayed(duration, () {
if (mounted) setState(() => _feedbackMessage = null); if (mounted) {
setState(() {
_statusOverrideText = null;
_statusOverrideDot = null;
});
}
}); });
} }
@@ -397,44 +438,41 @@ class _BoxingPageState extends State<BoxingPage> {
// === 状态文字 === // === 状态文字 ===
String get _statusText { void _updateBaseStatus() {
if (_isSubmitting) return '正在提交…'; if (_isSubmitting) {
if (_feedbackMessage != null) return _feedbackMessage!; _statusDot = StatusDotColor.orange;
_statusText = '正在提交…';
return;
}
if (_isDuplicateBoxNo && _phase == _Phase.scanned) {
_statusDot = StatusDotColor.amber;
_statusText = '箱号 ${_boxNoController.text} 已存在,请重新输入';
return;
}
switch (_phase) { switch (_phase) {
case _Phase.waiting: case _Phase.waiting:
_statusDot = StatusDotColor.blue;
if (_mode == BoxingMode.many2one && _boxNoLocked) { if (_mode == BoxingMode.many2one && _boxNoLocked) {
return '请扫描下一个总排号'; _statusText = '请扫描下一个总排号';
} else {
_statusText = '等待扫码';
} }
return '等待扫码';
case _Phase.scanned: case _Phase.scanned:
return '请确认信息并提交'; _statusDot = StatusDotColor.blue;
_statusText = '请确认信息并提交';
case _Phase.submitted: case _Phase.submitted:
_statusDot = StatusDotColor.green;
switch (_mode) { switch (_mode) {
case BoxingMode.one2many: case BoxingMode.one2many:
return '装箱成功,可继续添加或返回'; _statusText = '装箱成功,可继续添加或返回';
case BoxingMode.many2one: case BoxingMode.many2one:
return '请扫描下一个总排号'; _statusText = '请扫描下一个总排号';
case BoxingMode.one2one: 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 === // === Build ===
@override @override
@@ -444,11 +482,14 @@ class _BoxingPageState extends State<BoxingPage> {
final showActionButtons = final showActionButtons =
_phase == _Phase.submitted && _mode != BoxingMode.one2one; _phase == _Phase.submitted && _mode != BoxingMode.one2one;
_updateBaseStatus();
final effectiveDot = _statusOverrideDot ?? _statusDot;
final effectiveText = _statusOverrideText ?? _statusText;
return Scaffold( return Scaffold(
appBar: AppBar( appBar: AppBar(
title: const Text('装箱编号'), title: const Text('装箱编号'),
actions: [ actions: [
// 模式切换按钮
Padding( Padding(
padding: const EdgeInsets.only(right: 4), padding: const EdgeInsets.only(right: 4),
child: TextButton.icon( child: TextButton.icon(
@@ -466,20 +507,7 @@ class _BoxingPageState extends State<BoxingPage> {
), ),
body: Column( body: Column(
children: [ 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) if (_isDuplicateBoxNo && _phase == _Phase.scanned)
Container( Container(
width: double.infinity, width: double.infinity,
@@ -547,31 +575,7 @@ class _BoxingPageState extends State<BoxingPage> {
), ),
// 底部状态栏 // 底部状态栏
Container( StatusBar(dotColor: effectiveDot, text: effectiveText),
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,
),
),
],
),
),
], ],
), ),
); );
@@ -581,7 +585,6 @@ class _BoxingPageState extends State<BoxingPage> {
Widget _buildScanArea(bool isWaiting) { Widget _buildScanArea(bool isWaiting) {
if (isWaiting) { if (isWaiting) {
// 等待扫码:置灰提示
return Container( return Container(
width: double.infinity, width: double.infinity,
padding: const EdgeInsets.all(16), padding: const EdgeInsets.all(16),
@@ -603,9 +606,7 @@ class _BoxingPageState extends State<BoxingPage> {
); );
} }
// 已扫码:显示总排号
if (_mode == BoxingMode.many2one && _scannedZongpais.isNotEmpty) { if (_mode == BoxingMode.many2one && _scannedZongpais.isNotEmpty) {
// 多码一箱:显示已扫描列表
return Wrap( return Wrap(
spacing: 6, spacing: 6,
runSpacing: 4, runSpacing: 4,
@@ -624,7 +625,6 @@ class _BoxingPageState extends State<BoxingPage> {
); );
} }
// 单个总排号
return Container( return Container(
width: double.infinity, width: double.infinity,
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 12), padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 12),
@@ -662,7 +662,6 @@ class _BoxingPageState extends State<BoxingPage> {
return Column( return Column(
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
children: [ children: [
// 排产号
Text( Text(
'排产号:', '排产号:',
style: TextStyle( style: TextStyle(
@@ -681,7 +680,6 @@ class _BoxingPageState extends State<BoxingPage> {
), ),
const SizedBox(height: 8), const SizedBox(height: 8),
// 已有箱数 + 最大箱号 + 详情按钮
Row( Row(
children: [ children: [
Expanded( Expanded(

View File

@@ -1,11 +1,11 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:vibration/vibration.dart';
import 'package:pad_scanner/services/app_config_service.dart'; import 'package:pad_scanner/services/app_config_service.dart';
import 'package:pad_scanner/services/scanner_service.dart'; import 'package:pad_scanner/services/scanner_service.dart';
import 'package:pad_scanner/services/code_parser.dart'; import 'package:pad_scanner/services/code_parser.dart';
import 'package:pad_scanner/services/api_service.dart'; import 'package:pad_scanner/services/api_service.dart';
import 'package:pad_scanner/services/sound_service.dart'; import 'package:pad_scanner/services/feedback_service.dart';
import 'package:pad_scanner/widgets/status_bar.dart';
class RegistrationPage extends StatefulWidget { class RegistrationPage extends StatefulWidget {
const RegistrationPage({super.key}); const RegistrationPage({super.key});
@@ -17,7 +17,7 @@ class RegistrationPage extends StatefulWidget {
class _RegistrationPageState extends State<RegistrationPage> { class _RegistrationPageState extends State<RegistrationPage> {
final _scannerService = ScannerService(); final _scannerService = ScannerService();
final _apiService = ApiService(); final _apiService = ApiService();
final _soundService = SoundService(); final _feedbackService = FeedbackService();
final _focusNode = FocusNode(); final _focusNode = FocusNode();
final _zongpaiNos = <String>[]; final _zongpaiNos = <String>[];
@@ -26,9 +26,11 @@ class _RegistrationPageState extends State<RegistrationPage> {
bool _isLocked = false; bool _isLocked = false;
bool _isSubmitting = false; bool _isSubmitting = false;
String? _successMessage; // Status bar state
String? _snackbarMessage; StatusDotColor _statusDot = StatusDotColor.blue;
Color? _snackbarColor; String _statusText = '等待扫描总排号或货位号…';
String? _statusOverrideText;
StatusDotColor? _statusOverrideDot;
@override @override
void initState() { void initState() {
@@ -42,7 +44,7 @@ class _RegistrationPageState extends State<RegistrationPage> {
@override @override
void dispose() { void dispose() {
_focusNode.dispose(); _focusNode.dispose();
_soundService.dispose(); _feedbackService.dispose();
super.dispose(); super.dispose();
} }
@@ -57,9 +59,9 @@ class _RegistrationPageState extends State<RegistrationPage> {
final parsed = CodeParser.parse(result.barcode); final parsed = CodeParser.parse(result.barcode);
switch (parsed.type) { switch (parsed.type) {
case CodeType.zongpaiNo: case CodeType.zongpaiNo:
_feedbackService.trigger(FeedbackEvent.scanValid);
setState(() { setState(() {
if (_isLocked) { if (_isLocked) {
// 锁定模式:追加到列表(重复则覆盖)
final idx = _zongpaiNos.indexOf(parsed.value); final idx = _zongpaiNos.indexOf(parsed.value);
if (idx >= 0) { if (idx >= 0) {
_zongpaiNos[idx] = parsed.value; _zongpaiNos[idx] = parsed.value;
@@ -67,28 +69,31 @@ class _RegistrationPageState extends State<RegistrationPage> {
_zongpaiNos.add(parsed.value); _zongpaiNos.add(parsed.value);
} }
} else { } else {
// 单次模式:只有一条,覆盖
if (_zongpaiNos.isNotEmpty) { if (_zongpaiNos.isNotEmpty) {
_zongpaiNos[0] = parsed.value; _zongpaiNos[0] = parsed.value;
} else { } else {
_zongpaiNos.add(parsed.value); _zongpaiNos.add(parsed.value);
} }
} }
_snackbarMessage = null; _clearStatusOverride();
_successMessage = null;
}); });
case CodeType.locationNormal: case CodeType.locationNormal:
case CodeType.locationTransit: case CodeType.locationTransit:
if (!_isLocked) { if (!_isLocked) {
_feedbackService.trigger(FeedbackEvent.scanValid);
setState(() { setState(() {
_locationCode = parsed.value; _locationCode = parsed.value;
_locationType = parsed.type; _locationType = parsed.type;
_snackbarMessage = null; _clearStatusOverride();
_successMessage = null;
}); });
} }
case CodeType.invalid: case CodeType.invalid:
_showFeedback('无效码:${result.barcode}', isError: true); _feedbackService.trigger(FeedbackEvent.scanInvalid);
_showStatusOverride(
'无效码:${result.barcode}',
StatusDotColor.red,
const Duration(seconds: 2),
);
} }
} }
@@ -98,28 +103,49 @@ class _RegistrationPageState extends State<RegistrationPage> {
}); });
} }
void _showFeedback(String message, {bool isError = false}) { // --- Status bar management ---
void _clearStatusOverride() {
_statusOverrideText = null;
_statusOverrideDot = null;
}
void _showStatusOverride(String text, StatusDotColor dot, Duration duration) {
setState(() { setState(() {
_snackbarMessage = message; _statusOverrideText = text;
_snackbarColor = isError ? Colors.red.shade700 : Colors.green.shade700; _statusOverrideDot = dot;
}); });
Future.delayed(const Duration(seconds: 2), () { Future.delayed(duration, () {
if (mounted) setState(() => _snackbarMessage = null); if (mounted) setState(() => _clearStatusOverride());
}); });
} }
String get _statusText { void _updateBaseStatus() {
if (_isSubmitting) return '正在提交…'; if (_isSubmitting) {
if (_successMessage != null) return _successMessage!; _statusDot = StatusDotColor.orange;
_statusText = '正在提交…';
return;
}
if (_isLocked && _locationCode != null && _zongpaiNos.isEmpty) { if (_isLocked && _locationCode != null && _zongpaiNos.isEmpty) {
return '货位已锁定,请扫描下一张执行卡'; _statusDot = StatusDotColor.blue;
_statusText = '货位已锁定,请扫描下一张执行卡';
return;
} }
final hasZ = _zongpaiNos.isNotEmpty; final hasZ = _zongpaiNos.isNotEmpty;
final hasL = _locationCode != null; final hasL = _locationCode != null;
if (hasZ && hasL) return '请确认信息并提交'; if (hasZ && hasL) {
if (hasZ && !hasL) return '请扫描目标货位号'; _statusDot = StatusDotColor.blue;
if (!hasZ && hasL) return '请扫描执行卡'; _statusText = '请确认信息并提交';
return '等待扫描总排号或货位号…'; } else if (hasZ && !hasL) {
_statusDot = StatusDotColor.blue;
_statusText = '请扫描目标货位号';
} else if (!hasZ && hasL) {
_statusDot = StatusDotColor.blue;
_statusText = '请扫描执行卡';
} else {
_statusDot = StatusDotColor.blue;
_statusText = '等待扫描总排号或货位号…';
}
} }
bool get _canSubmit => bool get _canSubmit =>
@@ -131,17 +157,20 @@ class _RegistrationPageState extends State<RegistrationPage> {
final configService = AppConfigService(); final configService = AppConfigService();
final baseUrl = await configService.getString('api_url') ?? ''; final baseUrl = await configService.getString('api_url') ?? '';
if (baseUrl.isEmpty) { if (baseUrl.isEmpty) {
_showFeedback('未配置 API 地址,请前往设置', isError: true); _feedbackService.trigger(FeedbackEvent.submitFailure);
_showStatusOverride(
'未配置 API 地址,请前往设置',
StatusDotColor.red,
const Duration(seconds: 2),
);
return; return;
} }
setState(() => _isSubmitting = true); setState(() => _isSubmitting = true);
if (_isLocked && _zongpaiNos.length > 1) { if (_isLocked && _zongpaiNos.length > 1) {
// 批量提交:逐个提交列表中的总排号
await _submitBatch(baseUrl); await _submitBatch(baseUrl);
} else { } else {
// 单条提交
await _submitOne(baseUrl, _zongpaiNos.first); await _submitOne(baseUrl, _zongpaiNos.first);
} }
} }
@@ -158,25 +187,29 @@ class _RegistrationPageState extends State<RegistrationPage> {
setState(() => _isSubmitting = false); setState(() => _isSubmitting = false);
if (result.success) { if (result.success) {
_vibrateSuccess(); _feedbackService.trigger(FeedbackEvent.submitSuccess);
setState(() { setState(() {
_zongpaiNos.remove(zongpaiNo); _zongpaiNos.remove(zongpaiNo);
if (!_isLocked) { if (!_isLocked) {
_locationCode = null; _locationCode = null;
_locationType = null; _locationType = null;
} }
_successMessage = _isLocked ? '货位已锁定,请扫描下一张执行卡' : '上架成功';
});
_showFeedback('上架成功', isError: false);
Future.delayed(const Duration(milliseconds: 1500), () {
if (mounted) setState(() => _successMessage = null);
}); });
final msg = _isLocked ? '货位已锁定,请扫描下一张执行卡' : '上架成功';
_showStatusOverride(msg, StatusDotColor.green, const Duration(milliseconds: 1500));
} else if (result.isDuplicate) { } else if (result.isDuplicate) {
_vibrateError(); _feedbackService.trigger(FeedbackEvent.duplicateError);
_showDuplicateDialog(zongpaiNo, result.duplicateInfo); _showDuplicateDialog(zongpaiNo, result.duplicateInfo);
} else { } else {
_vibrateError(); final isNetwork = result.errorMessage == '网络异常,请检查网络连接';
_showFeedback(result.errorMessage ?? '提交失败', isError: true); _feedbackService.trigger(
isNetwork ? FeedbackEvent.networkError : FeedbackEvent.submitFailure,
);
_showStatusOverride(
result.errorMessage ?? '提交失败',
isNetwork ? StatusDotColor.yellow : StatusDotColor.red,
const Duration(seconds: 2),
);
} }
} }
@@ -197,11 +230,11 @@ class _RegistrationPageState extends State<RegistrationPage> {
} else { } else {
failed.add(zp); failed.add(zp);
if (result.isDuplicate) { if (result.isDuplicate) {
// 重复上架弹窗中断批量流程
setState(() { setState(() {
_zongpaiNos.removeWhere((e) => toRemove.contains(e)); _zongpaiNos.removeWhere((e) => toRemove.contains(e));
_isSubmitting = false; _isSubmitting = false;
}); });
_feedbackService.trigger(FeedbackEvent.duplicateError);
_showDuplicateDialog(zp, result.duplicateInfo); _showDuplicateDialog(zp, result.duplicateInfo);
return; return;
} }
@@ -216,24 +249,22 @@ class _RegistrationPageState extends State<RegistrationPage> {
}); });
if (failed.isEmpty) { if (failed.isEmpty) {
_vibrateSuccess(); _feedbackService.trigger(FeedbackEvent.submitSuccess);
_showFeedback('批量上架成功($successCount 条)', isError: false); _showStatusOverride(
'批量上架成功($successCount 条)',
StatusDotColor.green,
const Duration(milliseconds: 1500),
);
} else { } else {
_vibrateError(); _feedbackService.trigger(FeedbackEvent.submitFailure);
_showFeedback('成功 $successCount 条,失败 ${failed.length}', isError: true); _showStatusOverride(
'成功 $successCount 条,失败 ${failed.length}',
StatusDotColor.red,
const Duration(seconds: 2),
);
} }
} }
void _vibrateSuccess() {
Vibration.vibrate(duration: 200);
_soundService.playSuccess();
}
void _vibrateError() {
Vibration.vibrate(duration: 1000);
_soundService.playFailure();
}
void _showDuplicateDialog(String zongpaiNo, Map<String, dynamic>? info) { void _showDuplicateDialog(String zongpaiNo, Map<String, dynamic>? info) {
showDialog( showDialog(
context: context, context: context,
@@ -265,7 +296,10 @@ class _RegistrationPageState extends State<RegistrationPage> {
), ),
actions: [ actions: [
TextButton( TextButton(
onPressed: () => Navigator.pop(ctx), onPressed: () {
_feedbackService.stopAlert();
Navigator.pop(ctx);
},
child: const Text('关闭'), child: const Text('关闭'),
), ),
], ],
@@ -275,13 +309,17 @@ class _RegistrationPageState extends State<RegistrationPage> {
void _toggleLock(bool value) { void _toggleLock(bool value) {
if (value && _locationCode == null) { if (value && _locationCode == null) {
_showFeedback('请先扫描货位号', isError: true); _feedbackService.trigger(FeedbackEvent.submitFailure);
_showStatusOverride(
'请先扫描货位号',
StatusDotColor.red,
const Duration(seconds: 2),
);
return; return;
} }
setState(() { setState(() {
_isLocked = value; _isLocked = value;
if (!value) { if (!value) {
// 退出锁定模式,只保留最后一条总排号
if (_zongpaiNos.length > 1) { if (_zongpaiNos.length > 1) {
_zongpaiNos.removeRange(0, _zongpaiNos.length - 1); _zongpaiNos.removeRange(0, _zongpaiNos.length - 1);
} }
@@ -303,240 +341,73 @@ class _RegistrationPageState extends State<RegistrationPage> {
Widget build(BuildContext context) { Widget build(BuildContext context) {
final colorScheme = Theme.of(context).colorScheme; final colorScheme = Theme.of(context).colorScheme;
// Compute effective status
_updateBaseStatus();
final effectiveDot = _statusOverrideDot ?? _statusDot;
final effectiveText = _statusOverrideText ?? _statusText;
return KeyboardListener( return KeyboardListener(
focusNode: _focusNode, focusNode: _focusNode,
onKeyEvent: _onKeyEvent, onKeyEvent: _onKeyEvent,
child: Scaffold( child: Scaffold(
appBar: AppBar( appBar: AppBar(
title: const Text('上架登记'), title: const Text('上架登记'),
actions: [ actions: [
Padding( Padding(
padding: const EdgeInsets.only(right: 4), padding: const EdgeInsets.only(right: 4),
child: Row( child: Row(
children: [ children: [
const Text('锁定货位', style: TextStyle(fontSize: 13)), const Text('锁定货位', style: TextStyle(fontSize: 13)),
Switch(value: _isLocked, onChanged: _toggleLock), Switch(value: _isLocked, onChanged: _toggleLock),
], ],
),
),
],
),
body: Column(
children: [
// Feedback banner
if (_snackbarMessage != null)
Container(
width: double.infinity,
padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 16),
color: _snackbarColor,
child: Text(
_snackbarMessage!,
style: const TextStyle(color: Colors.white, fontSize: 14),
textAlign: TextAlign.center,
), ),
), ),
],
// Main form area ),
Expanded( body: Column(
child: Padding( children: [
padding: const EdgeInsets.all(16), // Main form area (no top banner)
child: Column( Expanded(
children: [ child: Padding(
// ---- 目标货位 (上方) ---- padding: const EdgeInsets.all(16),
Row( child: Column(
children: [ children: [
const Text( // ---- 目标货位 (上方) ----
'目标货位', Row(
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w500,
),
),
if (_locationCode != null) ...[
const SizedBox(width: 8),
Container(
padding: const EdgeInsets.symmetric(
horizontal: 6,
vertical: 2,
),
decoration: BoxDecoration(
color: _locationLabelColor(
_locationType,
).withValues(alpha: 0.15),
borderRadius: BorderRadius.circular(4),
),
child: Text(
_locationLabel(_locationType),
style: TextStyle(
fontSize: 11,
color: _locationLabelColor(_locationType),
fontWeight: FontWeight.bold,
),
),
),
],
],
),
const SizedBox(height: 6),
Container(
width: double.infinity,
padding: const EdgeInsets.symmetric(
horizontal: 12,
vertical: 14,
),
decoration: BoxDecoration(
border: Border.all(
color: _locationCode != null
? Colors.green
: Colors.grey.shade400,
width: _locationCode != null ? 2 : 1,
),
borderRadius: BorderRadius.circular(8),
),
child: Row(
children: [ children: [
Expanded( const Text(
child: Text( '目标货位',
_locationCode ?? '', style: TextStyle(
style: TextStyle( fontSize: 14,
fontSize: 20, fontWeight: FontWeight.w500,
fontWeight: FontWeight.bold,
color: _locationCode != null
? Colors.black87
: Colors.grey,
),
), ),
), ),
if (_isLocked) if (_locationCode != null) ...[
const Icon( const SizedBox(width: 8),
Icons.lock, Container(
color: Colors.orange, padding: const EdgeInsets.symmetric(
size: 20, horizontal: 6,
vertical: 2,
),
decoration: BoxDecoration(
color: _locationLabelColor(
_locationType,
).withValues(alpha: 0.15),
borderRadius: BorderRadius.circular(4),
),
child: Text(
_locationLabel(_locationType),
style: TextStyle(
fontSize: 11,
color: _locationLabelColor(_locationType),
fontWeight: FontWeight.bold,
),
),
), ),
],
], ],
), ),
), const SizedBox(height: 6),
const SizedBox(height: 16),
// ---- 总排号 (下方) ----
Row(
children: [
const Text(
'总排号',
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w500,
),
),
if (_isLocked && _zongpaiNos.isNotEmpty) ...[
const SizedBox(width: 8),
Container(
padding: const EdgeInsets.symmetric(
horizontal: 6,
vertical: 2,
),
decoration: BoxDecoration(
color: Colors.blue.withValues(alpha: 0.15),
borderRadius: BorderRadius.circular(4),
),
child: Text(
'${_zongpaiNos.length}',
style: const TextStyle(
fontSize: 11,
color: Colors.blue,
fontWeight: FontWeight.bold,
),
),
),
],
],
),
const SizedBox(height: 6),
if (_isLocked) ...[
// 锁定模式:列表显示多条总排号
Expanded(
child: _zongpaiNos.isEmpty
? Container(
width: double.infinity,
padding: const EdgeInsets.all(14),
decoration: BoxDecoration(
border: Border.all(color: Colors.grey.shade400),
borderRadius: BorderRadius.circular(8),
),
child: const Text(
'',
style: TextStyle(
fontSize: 20,
color: Colors.grey,
),
),
)
: ListView.separated(
itemCount: _zongpaiNos.length,
separatorBuilder: (_, __) =>
const SizedBox(height: 6),
itemBuilder: (context, index) {
return Dismissible(
key: ValueKey('${_zongpaiNos[index]}-$index'),
direction: DismissDirection.endToStart,
onDismissed: (_) => _removeZongpai(index),
background: Container(
alignment: Alignment.centerRight,
padding: const EdgeInsets.only(right: 16),
decoration: BoxDecoration(
color: Colors.red.shade100,
borderRadius: BorderRadius.circular(8),
),
child: const Icon(
Icons.delete,
color: Colors.red,
),
),
child: Container(
padding: const EdgeInsets.symmetric(
horizontal: 12,
vertical: 12,
),
decoration: BoxDecoration(
border: Border.all(
color: Colors.green,
width: 2,
),
borderRadius: BorderRadius.circular(8),
),
child: Row(
children: [
Expanded(
child: Text(
_zongpaiNos[index],
style: const TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Colors.black87,
),
),
),
IconButton(
icon: const Icon(
Icons.close,
size: 20,
),
onPressed: () =>
_removeZongpai(index),
padding: EdgeInsets.zero,
constraints: const BoxConstraints(),
),
],
),
),
);
},
),
),
] else ...[
// 单次模式:单个显示
Container( Container(
width: double.infinity, width: double.infinity,
padding: const EdgeInsets.symmetric( padding: const EdgeInsets.symmetric(
@@ -545,100 +416,231 @@ class _RegistrationPageState extends State<RegistrationPage> {
), ),
decoration: BoxDecoration( decoration: BoxDecoration(
border: Border.all( border: Border.all(
color: _zongpaiNos.isNotEmpty color: _locationCode != null
? Colors.green ? Colors.green
: Colors.grey.shade400, : Colors.grey.shade400,
width: _zongpaiNos.isNotEmpty ? 2 : 1, width: _locationCode != null ? 2 : 1,
), ),
borderRadius: BorderRadius.circular(8), borderRadius: BorderRadius.circular(8),
), ),
child: Text( child: Row(
_zongpaiNos.isNotEmpty ? _zongpaiNos.first : '', children: [
style: TextStyle( Expanded(
fontSize: 20, child: Text(
fontWeight: FontWeight.bold, _locationCode ?? '',
color: _zongpaiNos.isNotEmpty style: TextStyle(
? Colors.black87 fontSize: 20,
: Colors.grey, fontWeight: FontWeight.bold,
color: _locationCode != null
? Colors.black87
: Colors.grey,
),
),
),
if (_isLocked)
const Icon(
Icons.lock,
color: Colors.orange,
size: 20,
),
],
),
),
const SizedBox(height: 16),
// ---- 总排号 (下方) ----
Row(
children: [
const Text(
'总排号',
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w500,
),
), ),
if (_isLocked && _zongpaiNos.isNotEmpty) ...[
const SizedBox(width: 8),
Container(
padding: const EdgeInsets.symmetric(
horizontal: 6,
vertical: 2,
),
decoration: BoxDecoration(
color: Colors.blue.withValues(alpha: 0.15),
borderRadius: BorderRadius.circular(4),
),
child: Text(
'${_zongpaiNos.length}',
style: const TextStyle(
fontSize: 11,
color: Colors.blue,
fontWeight: FontWeight.bold,
),
),
),
],
],
),
const SizedBox(height: 6),
if (_isLocked) ...[
// 锁定模式:列表显示多条总排号
Expanded(
child: _zongpaiNos.isEmpty
? Container(
width: double.infinity,
padding: const EdgeInsets.all(14),
decoration: BoxDecoration(
border: Border.all(color: Colors.grey.shade400),
borderRadius: BorderRadius.circular(8),
),
child: const Text(
'',
style: TextStyle(
fontSize: 20,
color: Colors.grey,
),
),
)
: ListView.separated(
itemCount: _zongpaiNos.length,
separatorBuilder: (_, __) =>
const SizedBox(height: 6),
itemBuilder: (context, index) {
return Dismissible(
key: ValueKey('${_zongpaiNos[index]}-$index'),
direction: DismissDirection.endToStart,
onDismissed: (_) => _removeZongpai(index),
background: Container(
alignment: Alignment.centerRight,
padding: const EdgeInsets.only(right: 16),
decoration: BoxDecoration(
color: Colors.red.shade100,
borderRadius: BorderRadius.circular(8),
),
child: const Icon(
Icons.delete,
color: Colors.red,
),
),
child: Container(
padding: const EdgeInsets.symmetric(
horizontal: 12,
vertical: 12,
),
decoration: BoxDecoration(
border: Border.all(
color: Colors.green,
width: 2,
),
borderRadius: BorderRadius.circular(8),
),
child: Row(
children: [
Expanded(
child: Text(
_zongpaiNos[index],
style: const TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Colors.black87,
),
),
),
IconButton(
icon: const Icon(
Icons.close,
size: 20,
),
onPressed: () =>
_removeZongpai(index),
padding: EdgeInsets.zero,
constraints: const BoxConstraints(),
),
],
),
),
);
},
),
),
] else ...[
// 单次模式:单个显示
Container(
width: double.infinity,
padding: const EdgeInsets.symmetric(
horizontal: 12,
vertical: 14,
),
decoration: BoxDecoration(
border: Border.all(
color: _zongpaiNos.isNotEmpty
? Colors.green
: Colors.grey.shade400,
width: _zongpaiNos.isNotEmpty ? 2 : 1,
),
borderRadius: BorderRadius.circular(8),
),
child: Text(
_zongpaiNos.isNotEmpty ? _zongpaiNos.first : '',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: _zongpaiNos.isNotEmpty
? Colors.black87
: Colors.grey,
),
),
),
],
const SizedBox(height: 16),
// Submit button
SizedBox(
width: double.infinity,
height: 48,
child: ElevatedButton(
onPressed: _canSubmit ? _submit : null,
style: ElevatedButton.styleFrom(
backgroundColor: _canSubmit
? colorScheme.primary
: Colors.grey.shade300,
foregroundColor: _canSubmit
? Colors.white
: Colors.grey.shade600,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
child: _isSubmitting
? const SizedBox(
width: 22,
height: 22,
child: CircularProgressIndicator(
strokeWidth: 2,
color: Colors.white,
),
)
: Text(
_isLocked && _zongpaiNos.length > 1
? '批量上架(${_zongpaiNos.length} 条)'
: '确认上架(P1)',
style: const TextStyle(fontSize: 18),
),
), ),
), ),
], ],
),
const SizedBox(height: 16),
// Submit button
SizedBox(
width: double.infinity,
height: 48,
child: ElevatedButton(
onPressed: _canSubmit ? _submit : null,
style: ElevatedButton.styleFrom(
backgroundColor: _canSubmit
? colorScheme.primary
: Colors.grey.shade300,
foregroundColor: _canSubmit
? Colors.white
: Colors.grey.shade600,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
child: _isSubmitting
? const SizedBox(
width: 22,
height: 22,
child: CircularProgressIndicator(
strokeWidth: 2,
color: Colors.white,
),
)
: Text(
_isLocked && _zongpaiNos.length > 1
? '批量上架(${_zongpaiNos.length} 条)'
: '确认上架(P1)',
style: const TextStyle(fontSize: 18),
),
),
),
],
), ),
), ),
),
// Status bar at bottom // Bottom status bar
Container( StatusBar(dotColor: effectiveDot, text: effectiveText),
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: _isSubmitting
? Colors.orange
: _successMessage != null
? Colors.green
: Colors.blue,
shape: BoxShape.circle,
),
),
const SizedBox(width: 8),
Expanded(
child: Text(
_statusText,
style: const TextStyle(fontSize: 14),
overflow: TextOverflow.ellipsis,
),
),
],
),
),
],
), ),
),
); );
} }
} }

View File

@@ -20,6 +20,9 @@ class _SettingsPageState extends State<SettingsPage> {
String? _successPath; String? _successPath;
String? _failurePath; String? _failurePath;
String? _beepPath;
String? _errorPath;
String? _alertPath;
@override @override
void initState() { void initState() {
@@ -35,12 +38,20 @@ class _SettingsPageState extends State<SettingsPage> {
} }
Future<void> _loadSoundPaths() async { Future<void> _loadSoundPaths() async {
final s = await _soundService.getSuccessPath(); final results = await Future.wait([
final f = await _soundService.getFailurePath(); _soundService.getSuccessPath(),
_soundService.getFailurePath(),
_soundService.getBeepPath(),
_soundService.getErrorPath(),
_soundService.getAlertPath(),
]);
if (mounted) { if (mounted) {
setState(() { setState(() {
_successPath = s; _successPath = results[0];
_failurePath = f; _failurePath = results[1];
_beepPath = results[2];
_errorPath = results[3];
_alertPath = results[4];
}); });
} }
} }
@@ -74,38 +85,66 @@ class _SettingsPageState extends State<SettingsPage> {
} }
} }
Future<void> _pickSound(bool isSuccess) async { Future<void> _pickSound(String key) async {
final result = await FilePicker.pickFiles( final result = await FilePicker.pickFiles(
type: FileType.audio, type: FileType.audio,
); );
if (result != null && result.files.single.path != null) { if (result != null && result.files.single.path != null) {
final path = result.files.single.path!; final path = result.files.single.path!;
if (isSuccess) { switch (key) {
await _soundService.setSuccessPath(path); case 'success':
} else { await _soundService.setSuccessPath(path);
await _soundService.setFailurePath(path); case 'failure':
await _soundService.setFailurePath(path);
case 'beep':
await _soundService.setBeepPath(path);
case 'error':
await _soundService.setErrorPath(path);
case 'alert':
await _soundService.setAlertPath(path);
} }
setState(() { setState(() {
if (isSuccess) { switch (key) {
_successPath = path; case 'success':
} else { _successPath = path;
_failurePath = path; case 'failure':
_failurePath = path;
case 'beep':
_beepPath = path;
case 'error':
_errorPath = path;
case 'alert':
_alertPath = path;
} }
}); });
} }
} }
Future<void> _clearSound(bool isSuccess) async { Future<void> _clearSound(String key) async {
if (isSuccess) { switch (key) {
await _soundService.setSuccessPath(null); case 'success':
} else { await _soundService.setSuccessPath(null);
await _soundService.setFailurePath(null); case 'failure':
await _soundService.setFailurePath(null);
case 'beep':
await _soundService.setBeepPath(null);
case 'error':
await _soundService.setErrorPath(null);
case 'alert':
await _soundService.setAlertPath(null);
} }
setState(() { setState(() {
if (isSuccess) { switch (key) {
_successPath = null; case 'success':
} else { _successPath = null;
_failurePath = null; case 'failure':
_failurePath = null;
case 'beep':
_beepPath = null;
case 'error':
_errorPath = null;
case 'alert':
_alertPath = null;
} }
}); });
} }
@@ -191,29 +230,67 @@ class _SettingsPageState extends State<SettingsPage> {
style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold)), style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold)),
const SizedBox(height: 16), const SizedBox(height: 16),
// 上架成功铃声 // 扫码提示音
_buildSoundRow( _buildSoundRow(
label: '上架成功铃声', label: '扫码提示音',
path: _successPath, path: _beepPath,
onPick: () => _pickSound(true), onPick: () => _pickSound('beep'),
onPreview: _successPath != null onPreview: _beepPath != null
? () => _previewSound(_successPath!) ? () => _previewSound(_beepPath!)
: null, : null,
onClear: _successPath != null ? () => _clearSound(true) : null, onClear: _beepPath != null ? () => _clearSound('beep') : null,
), ),
const SizedBox(height: 12), const SizedBox(height: 12),
// 上架失败铃声 // 成功铃声
_buildSoundRow( _buildSoundRow(
label: '上架失败铃声', label: '成功铃声',
path: _successPath,
onPick: () => _pickSound('success'),
onPreview: _successPath != null
? () => _previewSound(_successPath!)
: null,
onClear: _successPath != null ? () => _clearSound('success') : null,
),
const SizedBox(height: 12),
// 失败铃声
_buildSoundRow(
label: '失败铃声',
path: _failurePath, path: _failurePath,
onPick: () => _pickSound(false), onPick: () => _pickSound('failure'),
onPreview: _failurePath != null onPreview: _failurePath != null
? () => _previewSound(_failurePath!) ? () => _previewSound(_failurePath!)
: null, : null,
onClear: onClear: _failurePath != null ? () => _clearSound('failure') : null,
_failurePath != null ? () => _clearSound(false) : null, ),
const SizedBox(height: 12),
// 错误铃声(无效码)
_buildSoundRow(
label: '错误铃声(无效码)',
path: _errorPath,
onPick: () => _pickSound('error'),
onPreview: _errorPath != null
? () => _previewSound(_errorPath!)
: null,
onClear: _errorPath != null ? () => _clearSound('error') : null,
),
const SizedBox(height: 12),
// 警报铃声(重复上架)
_buildSoundRow(
label: '警报铃声(重复上架)',
path: _alertPath,
onPick: () => _pickSound('alert'),
onPreview: _alertPath != null
? () => _previewSound(_alertPath!)
: null,
onClear: _alertPath != null ? () => _clearSound('alert') : null,
), ),
], ],
), ),

View File

@@ -0,0 +1,83 @@
import 'package:vibration/vibration.dart';
import 'package:pad_scanner/services/sound_service.dart';
/// Unified feedback events used across the app.
enum FeedbackEvent {
scanValid, // Valid barcode scanned → beep + short vibrate
scanInvalid, // Invalid barcode → error sound + short vibrate
submitSuccess, // Submit succeeded → success sound + long vibrate
submitFailure, // Submit failed → failure sound + short vibrate
duplicateError, // Duplicate (shelf) → alert loop + continuous vibrate
networkError, // Network error → failure sound + short vibrate
duplicateBoxNo, // Duplicate box no → failure sound + short vibrate
modeSwitch, // Mode toggle → beep only
}
/// Centralized feedback dispatcher.
///
/// Each page manages its own bottom status bar state; this service handles
/// the sound, vibration channels only.
class FeedbackService {
final SoundService _soundService;
FeedbackService({SoundService? soundService})
: _soundService = soundService ?? SoundService();
/// Trigger feedback for the given [event].
Future<void> trigger(FeedbackEvent event) async {
switch (event) {
case FeedbackEvent.scanValid:
_soundService.playBeep();
_vibrateShort();
case FeedbackEvent.scanInvalid:
_soundService.playError();
_vibrateShort();
case FeedbackEvent.submitSuccess:
_soundService.playSuccess();
_vibrateLong();
case FeedbackEvent.submitFailure:
_soundService.playFailure();
_vibrateShort();
case FeedbackEvent.duplicateError:
_soundService.playAlertLoop();
_vibratePattern();
case FeedbackEvent.networkError:
_soundService.playFailure();
_vibrateShort();
case FeedbackEvent.duplicateBoxNo:
_soundService.playFailure();
_vibrateShort();
case FeedbackEvent.modeSwitch:
_soundService.playBeep();
}
}
/// Stop any ongoing alert (e.g. when duplicate dialog is closed).
Future<void> stopAlert() async {
_soundService.stopAlert();
}
void _vibrateShort() {
Vibration.vibrate(duration: 100);
}
void _vibrateLong() {
Vibration.vibrate(duration: 200);
}
/// Continuous vibration pattern: 3 bursts to signal critical error.
void _vibratePattern() {
Vibration.vibrate(pattern: [0, 300, 100, 300, 100, 300]);
}
Future<void> dispose() async {
await _soundService.dispose();
}
}

View File

@@ -1,38 +1,64 @@
import 'dart:async';
import 'package:audioplayers/audioplayers.dart'; import 'package:audioplayers/audioplayers.dart';
import 'package:pad_scanner/services/app_config_service.dart'; import 'package:pad_scanner/services/app_config_service.dart';
class SoundService { class SoundService {
static const _keySuccess = 'sound_success'; static const _keySuccess = 'sound_success';
static const _keyFailure = 'sound_failure'; static const _keyFailure = 'sound_failure';
static const _keyBeep = 'sound_beep';
static const _keyError = 'sound_error';
static const _keyAlert = 'sound_alert';
final _player = AudioPlayer(); final _player = AudioPlayer();
final _alertPlayer = AudioPlayer();
final _configService = AppConfigService(); final _configService = AppConfigService();
Future<String?> getSuccessPath() async { Timer? _alertTimer;
return _configService.getString(_keySuccess);
}
Future<String?> getFailurePath() async { // --- Path getters ---
return _configService.getString(_keyFailure);
}
Future<void> setSuccessPath(String? path) async { Future<String?> getSuccessPath() =>
_configService.getString(_keySuccess);
Future<String?> getFailurePath() =>
_configService.getString(_keyFailure);
Future<String?> getBeepPath() =>
_configService.getString(_keyBeep);
Future<String?> getErrorPath() =>
_configService.getString(_keyError);
Future<String?> getAlertPath() =>
_configService.getString(_keyAlert);
// --- Path setters ---
Future<void> setSuccessPath(String? path) =>
_setConfigPath(_keySuccess, path);
Future<void> setFailurePath(String? path) =>
_setConfigPath(_keyFailure, path);
Future<void> setBeepPath(String? path) =>
_setConfigPath(_keyBeep, path);
Future<void> setErrorPath(String? path) =>
_setConfigPath(_keyError, path);
Future<void> setAlertPath(String? path) =>
_setConfigPath(_keyAlert, path);
Future<void> _setConfigPath(String key, String? path) async {
if (path != null) { if (path != null) {
await _configService.setString(_keySuccess, path); await _configService.setString(key, path);
} else { } else {
final config = await _configService.loadConfig(); final config = await _configService.loadConfig();
config.remove(_keySuccess); config.remove(key);
} }
} }
Future<void> setFailurePath(String? path) async { // --- Playback methods ---
if (path != null) {
await _configService.setString(_keyFailure, path);
} else {
final config = await _configService.loadConfig();
config.remove(_keyFailure);
}
}
Future<void> playSuccess() async { Future<void> playSuccess() async {
final path = await getSuccessPath(); final path = await getSuccessPath();
@@ -48,11 +74,55 @@ class SoundService {
} }
} }
Future<void> playBeep() async {
final path = await getBeepPath();
if (path != null) {
await _player.play(DeviceFileSource(path));
}
}
Future<void> playError() async {
final path = await getErrorPath();
if (path != null) {
await _player.play(DeviceFileSource(path));
}
}
/// Play alert sound in a loop until [stopAlert] is called.
/// Loops up to 10 times as a safety guard (~15 seconds max).
Future<void> playAlertLoop() async {
final path = await getAlertPath();
if (path == null) return;
_alertTimer?.cancel();
var count = 0;
_alertTimer = Timer.periodic(const Duration(milliseconds: 1500), (_) async {
if (count >= 10) {
_alertTimer?.cancel();
return;
}
await _alertPlayer.play(DeviceFileSource(path));
count++;
});
// Play immediately first time
await _alertPlayer.play(DeviceFileSource(path));
count++;
}
/// Stop the alert loop.
void stopAlert() {
_alertTimer?.cancel();
_alertTimer = null;
_alertPlayer.stop();
}
Future<void> play(String path) async { Future<void> play(String path) async {
await _player.play(DeviceFileSource(path)); await _player.play(DeviceFileSource(path));
} }
Future<void> dispose() async { Future<void> dispose() async {
_alertTimer?.cancel();
await _player.dispose(); await _player.dispose();
await _alertPlayer.dispose();
} }
} }

View File

@@ -0,0 +1,60 @@
import 'package:flutter/material.dart';
/// Status bar dot colors matching PRD requirements.
enum StatusDotColor {
blue, // idle / waiting
orange, // submitting
green, // success
red, // error (invalid code / submit failure / duplicate)
yellow, // network error
amber, // warning (duplicate box number)
}
/// Reusable bottom status bar with a colored dot and text.
class StatusBar extends StatelessWidget {
final StatusDotColor dotColor;
final String text;
const StatusBar({super.key, required this.dotColor, required this.text});
Color _resolveDotColor(BuildContext context) {
return switch (dotColor) {
StatusDotColor.blue => Colors.blue,
StatusDotColor.orange => Colors.orange,
StatusDotColor.green => Colors.green,
StatusDotColor.red => Colors.red,
StatusDotColor.yellow => Colors.yellow.shade700,
StatusDotColor.amber => Colors.amber,
};
}
@override
Widget build(BuildContext context) {
final colorScheme = Theme.of(context).colorScheme;
return 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: _resolveDotColor(context),
shape: BoxShape.circle,
),
),
const SizedBox(width: 8),
Expanded(
child: Text(
text,
style: const TextStyle(fontSize: 14),
overflow: TextOverflow.ellipsis,
),
),
],
),
);
}
}