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 createState() => _ScanScreenState(); } class _ScanScreenState extends State { 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, ), ); } }