- 引入 DarkMode 工具类,提供系统栏、颜色、色调的暗色适配能力 - AppColors 调色板改为 CupertinoDynamicColor.withBrightness 自适应 - app.dart 增加 AnnotatedRegion 与 Material Theme 包裹以联动暗色系统栏 - 移除 system_ui_util 中硬编码的浅色图标配置,改由主题驱动 - CardBox、AppDivider、SectionTitle 等公共组件适配深色卡片与分割线 - settings_ui、change_mobile、change_password、login_devices、wechat_binding 等页面全面接入 DarkMode 颜色 - dio_client 新增 allowStale 响应缓存机制,网络异常时回退本地缓存 - token_storage 增加 currentAccountId(JWT 解析)及按用户隔离的设备偏好存取 - ApiException 改为通过私有构造持有真实 AppError 实例,appError getter 返回结构化错误 - 闹钟模块迁移至 FutureProvider.autoDispose.family,消除手动 loading 状态管理 - 闹钟编辑页 CupertinoTimerPicker 替换为 CupertinoDatePicker(24h 格式),标签输入改为 TextEditingController
394 lines
13 KiB
Dart
394 lines
13 KiB
Dart
import 'package:cupertino_ui/cupertino_ui.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
import 'package:image_picker/image_picker.dart';
|
|
import 'package:mobile_scanner/mobile_scanner.dart';
|
|
import 'package:permission_handler/permission_handler.dart';
|
|
import 'package:ttstd_family_care/core/widgets/app_page_scaffold.dart';
|
|
|
|
import '../../../core/widgets/feature_ui.dart';
|
|
import '../../../core/widgets/settings_ui.dart';
|
|
import '../../../l10n/app_localizations.dart';
|
|
|
|
/// 二维码扫描页。
|
|
///
|
|
/// 支持两种识别方式:
|
|
/// 1. 相机实时扫码([MobileScanner] 的 `onDetect` 回调);
|
|
/// 2. 从相册选取图片后离线解析([MobileScannerController.analyzeImage])。
|
|
///
|
|
/// 识别成功后通过 `context.pop(code)` 把结果回传给调用方(绑定设备页)。
|
|
class DeviceScanPage extends StatefulWidget {
|
|
const DeviceScanPage({super.key});
|
|
|
|
@override
|
|
State<DeviceScanPage> createState() => _DeviceScanPageState();
|
|
}
|
|
|
|
class _DeviceScanPageState extends State<DeviceScanPage>
|
|
with WidgetsBindingObserver {
|
|
MobileScannerController _controller = _newController();
|
|
|
|
/// 防止连续回调导致多次 pop。
|
|
bool _handled = false;
|
|
|
|
/// 相册解析中,避免重复选图。
|
|
bool _pickingImage = false;
|
|
|
|
bool _torchOn = false;
|
|
|
|
/// 相机权限被拒(与相机启动失败区分展示)。
|
|
bool _permissionDenied = false;
|
|
|
|
/// 启动流程进行中,避免权限弹窗/生命周期回调重复触发。
|
|
bool _starting = false;
|
|
|
|
static MobileScannerController _newController() => MobileScannerController(
|
|
autoStart: false,
|
|
detectionSpeed: DetectionSpeed.noDuplicates,
|
|
formats: const [BarcodeFormat.qrCode],
|
|
);
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
WidgetsBinding.instance.addObserver(this);
|
|
_startScanner();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
WidgetsBinding.instance.removeObserver(this);
|
|
_controller.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
void didChangeAppLifecycleState(AppLifecycleState state) {
|
|
if (state == AppLifecycleState.resumed) {
|
|
// 从系统设置页返回:只检查权限,不再主动弹窗,避免已拒绝时反复弹/关。
|
|
_startScanner(requestPermission: false);
|
|
} else if (state == AppLifecycleState.inactive) {
|
|
_controller.stop();
|
|
}
|
|
}
|
|
|
|
/// 主动申请相机权限后再启动扫码。
|
|
///
|
|
/// 不用 mobile_scanner 自带的权限弹窗流程:部分设备(如 Android 15)
|
|
/// 上它可能在授权后仍返回失败,这里统一走 permission_handler。
|
|
Future<void> _startScanner({bool requestPermission = true}) async {
|
|
if (_starting || _handled || !mounted) return;
|
|
_starting = true;
|
|
try {
|
|
PermissionStatus status = await Permission.camera.status;
|
|
if (!status.isGranted &&
|
|
requestPermission &&
|
|
!status.isPermanentlyDenied) {
|
|
status = await Permission.camera.request();
|
|
}
|
|
if (!mounted) return;
|
|
if (!status.isGranted) {
|
|
debugPrint('[Scan] camera permission => ${status.name}');
|
|
if (!_permissionDenied) setState(() => _permissionDenied = true);
|
|
return;
|
|
}
|
|
if (_permissionDenied) setState(() => _permissionDenied = false);
|
|
if (_controller.value.error != null) {
|
|
// 5.2.3 在出错后 start() 不会重新尝试(权限错误时还会直接返回),
|
|
// 重建控制器才能恢复;MobileScanner 以 key 绑定控制器实例。
|
|
final oldController = _controller;
|
|
setState(() => _controller = _newController());
|
|
await oldController.dispose();
|
|
}
|
|
await _controller.start();
|
|
} catch (error) {
|
|
debugPrint('[Scan] start failed => $error');
|
|
} finally {
|
|
_starting = false;
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final l10n = AppLocalizations.of(context);
|
|
|
|
return AppPageScaffold(
|
|
navigationBar: CupertinoNavigationBar(
|
|
middle: Text(l10n.scanTitle),
|
|
leading: CupertinoButton(
|
|
padding: EdgeInsets.zero,
|
|
child: const Icon(CupertinoIcons.back),
|
|
onPressed: () => context.pop(),
|
|
),
|
|
trailing: CupertinoButton(
|
|
padding: EdgeInsets.zero,
|
|
onPressed: _toggleTorch,
|
|
child: Icon(
|
|
_torchOn ? CupertinoIcons.lightbulb_fill : CupertinoIcons.lightbulb,
|
|
size: 24,
|
|
),
|
|
),
|
|
),
|
|
child: SafeArea(
|
|
child: Column(
|
|
children: [
|
|
Expanded(
|
|
child: Stack(
|
|
fit: StackFit.expand,
|
|
children: [
|
|
_buildPreview(l10n),
|
|
// 取景框 + 提示文案;相机不可用时让位给错误提示。
|
|
ValueListenableBuilder<MobileScannerState>(
|
|
valueListenable: _controller,
|
|
builder: (context, state, child) {
|
|
if (_permissionDenied || state.error != null) {
|
|
return const SizedBox.shrink();
|
|
}
|
|
return child!;
|
|
},
|
|
child: IgnorePointer(
|
|
child: Center(
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Container(
|
|
width: 230,
|
|
height: 230,
|
|
decoration: BoxDecoration(
|
|
border: Border.all(
|
|
color: AppColors.green,
|
|
width: 3,
|
|
),
|
|
borderRadius: BorderRadius.circular(20),
|
|
),
|
|
),
|
|
const SizedBox(height: 18),
|
|
Padding(
|
|
padding: const EdgeInsets.symmetric(
|
|
horizontal: 40,
|
|
),
|
|
child: Text(
|
|
l10n.scanHint,
|
|
textAlign: TextAlign.center,
|
|
style: const TextStyle(
|
|
fontSize: 13.5,
|
|
color: CupertinoColors.white,
|
|
height: 1.5,
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
if (_pickingImage)
|
|
const Positioned.fill(
|
|
child: ColoredBox(
|
|
color: Color(0x66000000),
|
|
child: Center(
|
|
child: CupertinoActivityIndicator(radius: 14),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
Padding(
|
|
padding: const EdgeInsets.symmetric(vertical: 10),
|
|
child: SecondaryButton(
|
|
text: l10n.scanFromGallery,
|
|
icon: CupertinoIcons.photo_on_rectangle,
|
|
onPressed: _pickingImage ? null : () => _pickFromGallery(l10n),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
/// 相机预览;无权限或启动失败时展示对应提示,而不是统一报“没有权限”。
|
|
Widget _buildPreview(AppLocalizations l10n) {
|
|
if (_permissionDenied) {
|
|
return _CameraHint(
|
|
message: l10n.scanPermissionDenied,
|
|
showSettings: true,
|
|
retryText: l10n.commonRetry,
|
|
settingsText: l10n.scanOpenSettings,
|
|
onRetry: _startScanner,
|
|
onOpenSettings: _openSettings,
|
|
);
|
|
}
|
|
return MobileScanner(
|
|
key: ValueKey(_controller),
|
|
controller: _controller,
|
|
onDetect: (capture) => _onDetect(capture, l10n),
|
|
errorBuilder: (context, error, child) {
|
|
final denied =
|
|
error.errorCode == MobileScannerErrorCode.permissionDenied;
|
|
debugPrint(
|
|
'[Scan] scanner error => ${error.errorCode.name} '
|
|
'${error.errorDetails?.message ?? ''}',
|
|
);
|
|
return _CameraHint(
|
|
message: denied ? l10n.scanPermissionDenied : l10n.scanCameraError,
|
|
showSettings: denied,
|
|
retryText: l10n.commonRetry,
|
|
settingsText: l10n.scanOpenSettings,
|
|
onRetry: _startScanner,
|
|
onOpenSettings: _openSettings,
|
|
);
|
|
},
|
|
);
|
|
}
|
|
|
|
Future<void> _openSettings() async {
|
|
await openAppSettings();
|
|
}
|
|
|
|
/// 相机识别回调:取首个非空条码值后回传并关闭页面。
|
|
void _onDetect(BarcodeCapture capture, AppLocalizations l10n) {
|
|
if (_handled) return;
|
|
final code = _firstValue(capture);
|
|
if (code == null) return;
|
|
_handled = true;
|
|
context.pop(code);
|
|
}
|
|
|
|
/// 从相册选取图片并离线解析二维码。
|
|
Future<void> _pickFromGallery(AppLocalizations l10n) async {
|
|
setState(() => _pickingImage = true);
|
|
try {
|
|
final picked = await ImagePicker().pickImage(source: ImageSource.gallery);
|
|
if (picked == null) return;
|
|
|
|
final capture = await _controller.analyzeImage(picked.path);
|
|
final code = capture == null ? null : _firstValue(capture);
|
|
if (!mounted) return;
|
|
|
|
if (code == null) {
|
|
await AppDialogs.alert(
|
|
context,
|
|
message: l10n.scanNoCodeFound,
|
|
confirmText: l10n.commonConfirm,
|
|
);
|
|
return;
|
|
}
|
|
if (!mounted) return;
|
|
_handled = true;
|
|
context.pop(code);
|
|
} finally {
|
|
if (mounted) setState(() => _pickingImage = false);
|
|
}
|
|
}
|
|
|
|
/// 切换闪光灯,失败时静默忽略(部分设备/模拟器不支持)。
|
|
Future<void> _toggleTorch() async {
|
|
try {
|
|
await _controller.toggleTorch();
|
|
if (mounted) setState(() => _torchOn = !_torchOn);
|
|
} catch (_) {
|
|
// 不支持闪光灯,忽略。
|
|
}
|
|
}
|
|
|
|
/// 提取扫描结果中第一个有效的二维码文本。
|
|
String? _firstValue(BarcodeCapture capture) {
|
|
for (final barcode in capture.barcodes) {
|
|
final value = barcode.rawValue;
|
|
if (value != null && value.trim().isNotEmpty) return value.trim();
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/// 相机不可用(权限被拒或初始化失败)时的占位提示,附带重试/去设置操作。
|
|
class _CameraHint extends StatelessWidget {
|
|
const _CameraHint({
|
|
required this.message,
|
|
required this.showSettings,
|
|
required this.retryText,
|
|
required this.settingsText,
|
|
required this.onRetry,
|
|
required this.onOpenSettings,
|
|
});
|
|
|
|
final String message;
|
|
final bool showSettings;
|
|
final String retryText;
|
|
final String settingsText;
|
|
final VoidCallback onRetry;
|
|
final VoidCallback onOpenSettings;
|
|
|
|
@override
|
|
Widget build(BuildContext context) => ColoredBox(
|
|
color: const Color(0xFF111827),
|
|
child: Center(
|
|
child: Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 40),
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
const Icon(
|
|
CupertinoIcons.video_camera,
|
|
size: 46,
|
|
color: CupertinoColors.white,
|
|
),
|
|
const SizedBox(height: 14),
|
|
Text(
|
|
message,
|
|
textAlign: TextAlign.center,
|
|
style: const TextStyle(
|
|
fontSize: 14,
|
|
color: CupertinoColors.white,
|
|
height: 1.5,
|
|
),
|
|
),
|
|
const SizedBox(height: 22),
|
|
Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
if (showSettings) ...[
|
|
CupertinoButton(
|
|
padding: const EdgeInsets.symmetric(
|
|
horizontal: 22,
|
|
vertical: 10,
|
|
),
|
|
borderRadius: BorderRadius.circular(12),
|
|
color: AppColors.green,
|
|
onPressed: onOpenSettings,
|
|
child: Text(
|
|
settingsText,
|
|
style: const TextStyle(
|
|
color: CupertinoColors.white,
|
|
fontWeight: FontWeight.w600,
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(width: 12),
|
|
],
|
|
CupertinoButton(
|
|
padding: const EdgeInsets.symmetric(
|
|
horizontal: 22,
|
|
vertical: 10,
|
|
),
|
|
borderRadius: BorderRadius.circular(12),
|
|
color: const Color(0xFF374151),
|
|
onPressed: onRetry,
|
|
child: Text(
|
|
retryText,
|
|
style: const TextStyle(
|
|
color: CupertinoColors.white,
|
|
fontWeight: FontWeight.w600,
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|