import 'package:flutter/material.dart'; import 'package:mobile_scanner/mobile_scanner.dart'; import '../services/medicine_parser_service.dart'; class MedicineScanScreen extends StatefulWidget { const MedicineScanScreen({super.key}); @override State createState() => _MedicineScanScreenState(); } class _MedicineScanScreenState extends State { final MobileScannerController _controller = MobileScannerController( detectionSpeed: DetectionSpeed.normal, facing: CameraFacing.back, torchEnabled: false, ); bool _isProcessing = false; bool _isTorchOn = false; final TextEditingController _manualInputController = TextEditingController(); @override void dispose() { _controller.dispose(); _manualInputController.dispose(); super.dispose(); } Future _handleBarcodeDetected(String rawCode) async { if (_isProcessing) return; setState(() => _isProcessing = true); try { final info = await MedicineParserService.parseRawBarcode(rawCode); if (mounted) { Navigator.of(context).pop(info); } } catch (e) { if (mounted) { ScaffoldMessenger.of(context).showSnackBar( SnackBar(content: Text('解析条码失败: $e')), ); setState(() => _isProcessing = false); } } } void _showManualInputDialog() { _manualInputController.clear(); showDialog( context: context, builder: (ctx) => AlertDialog( title: const Text('手动输入条形码/追溯码'), content: Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.start, children: [ const Text( '支持输入 13 位商品条形码 (如 69...) 或 20 位中国药品电子监管码:', style: TextStyle(fontSize: 13, color: Colors.black54), ), const SizedBox(height: 12), TextField( controller: _manualInputController, keyboardType: TextInputType.number, autofocus: true, decoration: const InputDecoration( border: OutlineInputBorder(), hintText: '如 6921147600010 或 8188...', labelText: '条码编号', ), ), ], ), actions: [ TextButton( onPressed: () => Navigator.pop(ctx), child: const Text('取消'), ), ElevatedButton( onPressed: () { final text = _manualInputController.text.trim(); if (text.isNotEmpty) { Navigator.pop(ctx); _handleBarcodeDetected(text); } }, child: const Text('识别'), ), ], ), ); } @override Widget build(BuildContext context) { return Scaffold( backgroundColor: Colors.black, appBar: AppBar( title: const Text('扫描药品条码/追溯码'), backgroundColor: Colors.black87, foregroundColor: Colors.white, actions: [ IconButton( icon: Icon( _isTorchOn ? Icons.flash_on : Icons.flash_off, color: _isTorchOn ? Colors.amber : Colors.white, ), tooltip: '开关手电筒', onPressed: () { setState(() => _isTorchOn = !_isTorchOn); _controller.toggleTorch(); }, ), IconButton( icon: const Icon(Icons.keyboard_outlined), tooltip: '手动输入条码', onPressed: _showManualInputDialog, ), ], ), body: Stack( alignment: Alignment.center, children: [ MobileScanner( controller: _controller, onDetect: (capture) { final barcodes = capture.barcodes; for (final barcode in barcodes) { final val = barcode.rawValue; if (val != null && val.isNotEmpty) { _handleBarcodeDetected(val); break; } } }, errorBuilder: (context, error) { return Center( child: Padding( padding: const EdgeInsets.all(24.0), child: Column( mainAxisSize: MainAxisSize.min, children: [ const Icon(Icons.camera_alt_outlined, color: Colors.white54, size: 64), const SizedBox(height: 16), Text( '无法打开摄像头: ${error.errorDetails?.message ?? "无权限或设备不支持"}', textAlign: TextAlign.center, style: const TextStyle(color: Colors.white70), ), const SizedBox(height: 20), ElevatedButton.icon( icon: const Icon(Icons.edit_note), label: const Text('手动输入条形码'), onPressed: _showManualInputDialog, ), ], ), ), ); }, ), // 扫描取景框覆盖层 IgnorePointer( child: Container( decoration: BoxDecoration( border: Border.all(color: Colors.white24, width: 1), ), child: Stack( children: [ Center( child: Container( width: 280, height: 200, decoration: BoxDecoration( border: Border.all(color: const Color(0xFF2563EB), width: 2.5), borderRadius: BorderRadius.circular(16), ), ), ), ], ), ), ), // 底部操作引导提示 Positioned( bottom: 40, left: 20, right: 20, child: Column( children: [ Container( padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 10), decoration: BoxDecoration( color: Colors.black.withValues(alpha: 0.7), borderRadius: BorderRadius.circular(20), ), child: const Text( '请将药盒上的【20位追溯码】或【条形码】对准框内', textAlign: TextAlign.center, style: TextStyle(color: Colors.white, fontSize: 13), ), ), const SizedBox(height: 12), TextButton.icon( onPressed: _showManualInputDialog, icon: const Icon(Icons.edit, color: Colors.white70, size: 16), label: const Text( '扫不出来?点击手动输入', style: TextStyle(color: Colors.white70, fontSize: 13), ), ), ], ), ), if (_isProcessing) Container( color: Colors.black54, child: const Center( child: Column( mainAxisSize: MainAxisSize.min, children: [ CircularProgressIndicator(color: Colors.white), SizedBox(height: 16), Text( '正在查询药品信息...', style: TextStyle(color: Colors.white, fontSize: 14), ), ], ), ), ), ], ), ); } }