Files
app-grosir/lib/screens/scan_screen.dart
2026-07-23 22:12:32 +07:00

55 lines
1.2 KiB
Dart

import 'package:flutter/material.dart';
import 'package:mobile_scanner/mobile_scanner.dart';
class ScanScreen extends StatefulWidget {
final String title;
const ScanScreen({
super.key,
required this.title,
});
@override
State<ScanScreen> createState() => _ScanScreenState();
}
class _ScanScreenState extends State<ScanScreen> {
final MobileScannerController _controller = MobileScannerController();
bool _isScanned = false;
void _handleBarcode(BarcodeCapture capture) {
if (_isScanned) return;
final barcode = capture.barcodes.firstOrNull;
if (barcode != null && barcode.rawValue != null) {
final code = barcode.rawValue!.trim();
if (code.isNotEmpty) {
_isScanned = true;
_controller.stop();
Navigator.pop(context, code);
}
}
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
centerTitle: true,
),
body: MobileScanner(
controller: _controller,
onDetect: _handleBarcode,
),
);
}
}