Skip to content

Instantly share code, notes, and snippets.

@r-yeates
Created March 16, 2022 19:22
Show Gist options
  • Save r-yeates/8775f539486ff992e1c6ffea9e42c575 to your computer and use it in GitHub Desktop.
Save r-yeates/8775f539486ff992e1c6ffea9e42c575 to your computer and use it in GitHub Desktop.
MobileScannerController cameraController = MobileScannerController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Mobile Scanner'),
actions: [
IconButton(
color: Colors.white,
icon: ValueListenableBuilder(
valueListenable: cameraController.torchState,
builder: (context, state, child) {
switch (state as TorchState) {
case TorchState.off:
return const Icon(Icons.flash_off, color: Colors.grey);
case TorchState.on:
return const Icon(Icons.flash_on, color: Colors.yellow);
}
},
),
iconSize: 32.0,
onPressed: () => cameraController.toggleTorch(),
),
IconButton(
color: Colors.white,
icon: ValueListenableBuilder(
valueListenable: cameraController.cameraFacingState,
builder: (context, state, child) {
switch (state as CameraFacing) {
case CameraFacing.front:
return const Icon(Icons.camera_front);
case CameraFacing.back:
return const Icon(Icons.camera_rear);
}
},
),
iconSize: 32.0,
onPressed: () => cameraController.switchCamera(),
),
const SizedBox(width: 20)
],
),
body: MobileScanner(
allowDuplicates: false,
controller: cameraController,
onDetect: (barcode, args) {
final String? code = barcode.rawValue;
debugPrint('Barcode found! $code');
}),
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment