80 lines
2.2 KiB
Dart
80 lines
2.2 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:mobile_scanner/mobile_scanner.dart';
|
|
|
|
/// StatefulWidget für den Barcode-Scanner mit grünem Border-Feedback
|
|
class BarcodeScannerWidget extends StatefulWidget {
|
|
final Function(String) onBarcodeDetected;
|
|
|
|
const BarcodeScannerWidget({
|
|
Key? key,
|
|
required this.onBarcodeDetected,
|
|
}) : super(key: key);
|
|
|
|
@override
|
|
State<BarcodeScannerWidget> createState() => _BarcodeScannerWidgetState();
|
|
}
|
|
|
|
class _BarcodeScannerWidgetState extends State<BarcodeScannerWidget> {
|
|
bool _isDetected = false;
|
|
DateTime? _lastScannedTime;
|
|
final Duration _scanTimeout = const Duration(milliseconds: 2000); // 2 Sekunden Cooldown
|
|
|
|
void _handleBarcodeDetected(String barcode) {
|
|
final now = DateTime.now();
|
|
|
|
// Prüfe ob genug Zeit seit dem letzten erfolgreichen Scan vergangen ist
|
|
if (_lastScannedTime != null &&
|
|
now.difference(_lastScannedTime!).inMilliseconds < _scanTimeout.inMilliseconds) {
|
|
// Timeout nicht abgelaufen - ignoriere diesen Scan
|
|
debugPrint('Scan ignoriert - Cooldown aktiv');
|
|
return;
|
|
}
|
|
|
|
// Update letzte Scan-Zeit
|
|
_lastScannedTime = now;
|
|
|
|
// Rand grün färben
|
|
setState(() {
|
|
_isDetected = true;
|
|
});
|
|
|
|
// Nach 500ms wieder zurücksetzen
|
|
Future.delayed(const Duration(milliseconds: 500), () {
|
|
if (mounted) {
|
|
setState(() {
|
|
_isDetected = false;
|
|
});
|
|
}
|
|
});
|
|
|
|
// Callback aufrufen
|
|
widget.onBarcodeDetected(barcode);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final screenHeight = MediaQuery.of(context).size.height;
|
|
final scannerHeight = screenHeight / 4;
|
|
|
|
return Container(
|
|
height: scannerHeight,
|
|
decoration: BoxDecoration(
|
|
border: Border.all(
|
|
color: _isDetected ? Colors.green : Colors.grey,
|
|
width: _isDetected ? 4 : 2,
|
|
),
|
|
),
|
|
child: MobileScanner(
|
|
onDetect: (capture) {
|
|
final List<Barcode> barcodes = capture.barcodes;
|
|
|
|
for (final barcode in barcodes) {
|
|
if (barcode.rawValue != null) {
|
|
_handleBarcodeDetected(barcode.rawValue!);
|
|
}
|
|
}
|
|
},
|
|
),
|
|
);
|
|
}
|
|
} |