I'm using the following mobile_scanner code to scan for a barcode. I would like to give the user the option to exit the scanner before scanning a barcode so I'm providing an IconButton to do so.
When I click the IconButton, I get a black screen and no errors instead of being taken to the Home screen as expected. What do I need to do to make this work?
final MobileScannerController scannerController = MobileScannerController();
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Builder(builder: (context) {
return Stack(
children: [
MobileScanner(
allowDuplicates: false,
controller: scannerController,
onDetect: (barcode, args) async {
final String code = barcode.rawValue!;
Map<String, dynamic> productData =
await Networking().getProductByBarcode(code);
Provider.of<AppData>(context, listen: false)
.setProduct(productData);
Navigator.pushNamed(context, ProductDetails.id);
},
),
Container(
alignment: Alignment.bottomLeft,
margin: const EdgeInsets.only(
bottom: 35.0,
left: 25.0,
),
child: IconButton(
onPressed: () {
scannerController
..stop()
..dispose();
Navigator.pop(context);
},
icon: const Icon(
Icons.close_outlined,
size: 40.0,
color: Colors.white,
),
),
),
],
);
},
),
),
);
}