444 lines
17 KiB
Dart
444 lines
17 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:mobile_scanner/mobile_scanner.dart';
|
|
|
|
import '../services/connection_service.dart';
|
|
import '../widgets/scanner_overlay.dart';
|
|
import 'connect_screen.dart';
|
|
import 'history_screen.dart';
|
|
import 'settings_screen.dart';
|
|
|
|
class ScannerScreen extends StatefulWidget {
|
|
final ConnectionService connectionService;
|
|
|
|
const ScannerScreen({super.key, required this.connectionService});
|
|
|
|
@override
|
|
State<ScannerScreen> createState() => _ScannerScreenState();
|
|
}
|
|
|
|
class _ScannerScreenState extends State<ScannerScreen> with WidgetsBindingObserver {
|
|
late MobileScannerController _scannerController;
|
|
bool _isTorchOn = false;
|
|
String? _lastScannedValue;
|
|
DateTime _lastScannedTime = DateTime.now().subtract(const Duration(seconds: 10));
|
|
bool _isProcessing = false;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
WidgetsBinding.instance.addObserver(this);
|
|
_scannerController = MobileScannerController(
|
|
detectionSpeed: DetectionSpeed.normal,
|
|
facing: CameraFacing.back,
|
|
torchEnabled: false,
|
|
);
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
WidgetsBinding.instance.removeObserver(this);
|
|
_scannerController.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
void didChangeAppLifecycleState(AppLifecycleState state) {
|
|
if (!_scannerController.value.isInitialized) return;
|
|
if (state == AppLifecycleState.inactive || state == AppLifecycleState.paused) {
|
|
_scannerController.stop();
|
|
} else if (state == AppLifecycleState.resumed) {
|
|
_scannerController.start();
|
|
}
|
|
}
|
|
|
|
void _onDetect(BarcodeCapture capture) async {
|
|
final barcodes = capture.barcodes;
|
|
if (barcodes.isEmpty) return;
|
|
|
|
final first = barcodes.first;
|
|
final rawValue = first.rawValue ?? first.displayValue;
|
|
if (rawValue == null || rawValue.trim().isEmpty) return;
|
|
|
|
final now = DateTime.now();
|
|
final isSameCode = rawValue == _lastScannedValue;
|
|
final durationSinceLast = now.difference(_lastScannedTime);
|
|
|
|
// Debounce: se è lo stesso codice, aspetta almeno 1.5s (o 1.0s in continuo); se codice diverso, aspetta 600ms
|
|
final minInterval = isSameCode
|
|
? (widget.connectionService.continuousMode ? const Duration(milliseconds: 1000) : const Duration(milliseconds: 1800))
|
|
: const Duration(milliseconds: 600);
|
|
|
|
if (durationSinceLast < minInterval || _isProcessing) {
|
|
return;
|
|
}
|
|
|
|
_isProcessing = true;
|
|
_lastScannedValue = rawValue;
|
|
_lastScannedTime = now;
|
|
|
|
final formatName = first.format.name.replaceAll('BarcodeFormat.', '');
|
|
await widget.connectionService.sendCode(rawValue, format: formatName);
|
|
|
|
if (mounted) {
|
|
setState(() {
|
|
_isProcessing = false;
|
|
});
|
|
}
|
|
}
|
|
|
|
void _showManualInputDialog() {
|
|
final textController = TextEditingController();
|
|
showDialog(
|
|
context: context,
|
|
builder: (ctx) => AlertDialog(
|
|
title: const Row(
|
|
children: [
|
|
Icon(Icons.keyboard_outlined),
|
|
SizedBox(width: 8),
|
|
Text('Digitazione Manuale'),
|
|
],
|
|
),
|
|
content: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
const Text(
|
|
'Digita o incolla il codice da inviare direttamente al PC:',
|
|
style: TextStyle(fontSize: 13, color: Colors.grey),
|
|
),
|
|
const SizedBox(height: 12),
|
|
TextField(
|
|
controller: textController,
|
|
autofocus: true,
|
|
decoration: InputDecoration(
|
|
hintText: 'Es. 8001234567890 o ABC-123',
|
|
border: const OutlineInputBorder(),
|
|
suffixIcon: IconButton(
|
|
icon: const Icon(Icons.paste),
|
|
tooltip: 'Incolla dagli appunti',
|
|
onPressed: () async {
|
|
final data = await Clipboard.getData('text/plain');
|
|
if (data?.text != null) {
|
|
textController.text = data!.text!;
|
|
}
|
|
},
|
|
),
|
|
),
|
|
onSubmitted: (val) {
|
|
if (val.trim().isNotEmpty) {
|
|
widget.connectionService.sendCode(val.trim(), format: 'Manuale');
|
|
Navigator.pop(ctx);
|
|
}
|
|
},
|
|
),
|
|
],
|
|
),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.pop(ctx),
|
|
child: const Text('Annulla'),
|
|
),
|
|
FilledButton.icon(
|
|
icon: const Icon(Icons.send),
|
|
label: const Text('Invia al PC'),
|
|
onPressed: () {
|
|
final val = textController.text.trim();
|
|
if (val.isNotEmpty) {
|
|
widget.connectionService.sendCode(val, format: 'Manuale');
|
|
Navigator.pop(ctx);
|
|
}
|
|
},
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildStatusPill() {
|
|
final status = widget.connectionService.status;
|
|
Color color;
|
|
IconData icon;
|
|
String text;
|
|
|
|
switch (status) {
|
|
case ConnectionStatus.connected:
|
|
color = Colors.greenAccent.shade700;
|
|
icon = Icons.check_circle;
|
|
text = widget.connectionService.serverName;
|
|
break;
|
|
case ConnectionStatus.connecting:
|
|
color = Colors.orange;
|
|
icon = Icons.sync;
|
|
text = 'Connessione...';
|
|
break;
|
|
case ConnectionStatus.error:
|
|
case ConnectionStatus.disconnected:
|
|
color = Colors.redAccent;
|
|
icon = Icons.error_outline;
|
|
text = 'Disconnesso';
|
|
break;
|
|
}
|
|
|
|
return InkWell(
|
|
onTap: () {
|
|
Navigator.push(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (_) => ConnectScreen(connectionService: widget.connectionService),
|
|
),
|
|
);
|
|
},
|
|
borderRadius: BorderRadius.circular(20),
|
|
child: Container(
|
|
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
|
|
decoration: BoxDecoration(
|
|
color: Colors.black.withValues(alpha: 0.65),
|
|
borderRadius: BorderRadius.circular(20),
|
|
border: Border.all(color: color, width: 1.5),
|
|
),
|
|
child: Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Icon(icon, color: color, size: 16),
|
|
const SizedBox(width: 6),
|
|
Text(
|
|
text,
|
|
style: TextStyle(
|
|
color: color,
|
|
fontSize: 13,
|
|
fontWeight: FontWeight.w600,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return ListenableBuilder(
|
|
listenable: widget.connectionService,
|
|
builder: (context, _) {
|
|
final lastScan = widget.connectionService.lastScan;
|
|
|
|
return Scaffold(
|
|
body: Stack(
|
|
children: [
|
|
// Vista Fotocamera con MobileScanner
|
|
MobileScanner(
|
|
controller: _scannerController,
|
|
onDetect: _onDetect,
|
|
),
|
|
|
|
// Mirino centrale per allineamento codice
|
|
const ScannerOverlay(),
|
|
|
|
// Barra superiore: Status pill, Flash, Cambio camera, Impostazioni
|
|
SafeArea(
|
|
child: Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
|
|
child: Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
children: [
|
|
_buildStatusPill(),
|
|
Row(
|
|
children: [
|
|
IconButton.filledTonal(
|
|
icon: Icon(_isTorchOn ? Icons.flash_on : Icons.flash_off),
|
|
onPressed: () async {
|
|
await _scannerController.toggleTorch();
|
|
setState(() {
|
|
_isTorchOn = !_isTorchOn;
|
|
});
|
|
},
|
|
),
|
|
const SizedBox(width: 8),
|
|
IconButton.filledTonal(
|
|
icon: const Icon(Icons.cameraswitch),
|
|
onPressed: () => _scannerController.switchCamera(),
|
|
),
|
|
const SizedBox(width: 8),
|
|
IconButton.filledTonal(
|
|
icon: const Icon(Icons.history),
|
|
tooltip: 'Cronologia',
|
|
onPressed: () {
|
|
Navigator.push(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (_) => HistoryScreen(connectionService: widget.connectionService),
|
|
),
|
|
);
|
|
},
|
|
),
|
|
const SizedBox(width: 8),
|
|
IconButton.filledTonal(
|
|
icon: const Icon(Icons.settings),
|
|
tooltip: 'Impostazioni',
|
|
onPressed: () {
|
|
Navigator.push(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (_) => SettingsScreen(connectionService: widget.connectionService),
|
|
),
|
|
);
|
|
},
|
|
),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
|
|
// Controlli rapidi sotto la barra superiore: Modalità continuo & Suffisso rapido
|
|
Positioned(
|
|
top: 100,
|
|
left: 16,
|
|
right: 16,
|
|
child: Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
children: [
|
|
// Pillola modalità continua
|
|
ActionChip(
|
|
avatar: Icon(
|
|
widget.connectionService.continuousMode ? Icons.repeat_one : Icons.looks_one,
|
|
size: 16,
|
|
),
|
|
label: Text(
|
|
widget.connectionService.continuousMode ? 'Scansione Continua' : 'Scansione Singola',
|
|
style: const TextStyle(fontSize: 12),
|
|
),
|
|
backgroundColor: widget.connectionService.continuousMode
|
|
? Theme.of(context).colorScheme.primaryContainer
|
|
: Colors.black54,
|
|
onPressed: () {
|
|
widget.connectionService.setContinuousMode(!widget.connectionService.continuousMode);
|
|
},
|
|
),
|
|
|
|
// Suffisso tasto rapido (Enter / Tab / Nessuno)
|
|
PopupMenuButton<String>(
|
|
initialValue: widget.connectionService.suffix,
|
|
tooltip: 'Cambia tasto finale',
|
|
onSelected: (val) => widget.connectionService.setSuffix(val),
|
|
itemBuilder: (context) => [
|
|
const PopupMenuItem(value: 'enter', child: Text('Invio (Enter)')),
|
|
const PopupMenuItem(value: 'tab', child: Text('Tabulatore (Tab)')),
|
|
const PopupMenuItem(value: 'none', child: Text('Nessun tasto')),
|
|
],
|
|
child: Chip(
|
|
avatar: const Icon(Icons.keyboard_return, size: 14),
|
|
label: Text(
|
|
widget.connectionService.suffix.toUpperCase(),
|
|
style: const TextStyle(fontSize: 12, fontWeight: FontWeight.bold),
|
|
),
|
|
backgroundColor: Colors.black54,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
|
|
// Floating Action Button per digitazione manuale
|
|
Positioned(
|
|
bottom: lastScan != null ? 150 : 32,
|
|
right: 16,
|
|
child: FloatingActionButton.extended(
|
|
heroTag: 'manual_input',
|
|
icon: const Icon(Icons.keyboard),
|
|
label: const Text('Manuale'),
|
|
onPressed: _showManualInputDialog,
|
|
),
|
|
),
|
|
|
|
// Scheda inferiore: Ultimo codice scansionato
|
|
if (lastScan != null)
|
|
Positioned(
|
|
left: 16,
|
|
right: 16,
|
|
bottom: 24,
|
|
child: Card(
|
|
elevation: 8,
|
|
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
|
|
color: Theme.of(context).colorScheme.surface.withValues(alpha: 0.95),
|
|
child: Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
|
|
child: Row(
|
|
children: [
|
|
Icon(
|
|
lastScan.sentSuccessfully ? Icons.check_circle : Icons.warning_amber_rounded,
|
|
color: lastScan.sentSuccessfully ? Colors.green : Colors.orange,
|
|
size: 28,
|
|
),
|
|
const SizedBox(width: 12),
|
|
Expanded(
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Row(
|
|
children: [
|
|
Text(
|
|
lastScan.format,
|
|
style: TextStyle(
|
|
fontSize: 11,
|
|
fontWeight: FontWeight.bold,
|
|
color: Theme.of(context).colorScheme.primary,
|
|
),
|
|
),
|
|
const Spacer(),
|
|
Text(
|
|
'${lastScan.timestamp.hour.toString().padLeft(2, '0')}:${lastScan.timestamp.minute.toString().padLeft(2, '0')}:${lastScan.timestamp.second.toString().padLeft(2, '0')}',
|
|
style: const TextStyle(fontSize: 11, color: Colors.grey),
|
|
),
|
|
],
|
|
),
|
|
const SizedBox(height: 2),
|
|
Text(
|
|
lastScan.code,
|
|
style: const TextStyle(
|
|
fontSize: 16,
|
|
fontWeight: FontWeight.bold,
|
|
),
|
|
maxLines: 1,
|
|
overflow: TextOverflow.ellipsis,
|
|
),
|
|
],
|
|
),
|
|
),
|
|
const SizedBox(width: 8),
|
|
IconButton(
|
|
icon: const Icon(Icons.copy, size: 20),
|
|
tooltip: 'Copia',
|
|
onPressed: () {
|
|
Clipboard.setData(ClipboardData(text: lastScan.code));
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(
|
|
content: Text('Codice copiato negli appunti'),
|
|
duration: Duration(seconds: 1),
|
|
),
|
|
);
|
|
},
|
|
),
|
|
IconButton(
|
|
icon: const Icon(Icons.replay, size: 20),
|
|
tooltip: 'Invia di nuovo al PC',
|
|
onPressed: () {
|
|
widget.connectionService.sendCode(lastScan.code, format: lastScan.format);
|
|
},
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
},
|
|
);
|
|
}
|
|
}
|