Skip to content

Commit

Permalink
add flashlight and camera switch
Browse files Browse the repository at this point in the history
  • Loading branch information
MrCyjaneK committed Jul 17, 2024
1 parent 13be92f commit 5c105f1
Showing 1 changed file with 102 additions and 1 deletion.
103 changes: 102 additions & 1 deletion lib/entities/qr_scanner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,119 @@ class _BarcodeScannerSimpleState extends State<BarcodeScannerSimple> {
}
}

final MobileScannerController ctrl = MobileScannerController();

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Scan')),
appBar: AppBar(
title: const Text('Scan'),
actions: [
SwitchCameraButton(controller: ctrl),
ToggleFlashlightButton(controller: ctrl),
],
),
backgroundColor: Colors.black,
body: Stack(
children: [
MobileScanner(
onDetect: _handleBarcode,
controller: ctrl,
),
],
),
);
}
}


class ToggleFlashlightButton extends StatelessWidget {
const ToggleFlashlightButton({required this.controller, super.key});

final MobileScannerController controller;

@override
Widget build(BuildContext context) {
return ValueListenableBuilder(
valueListenable: controller,
builder: (context, state, child) {
if (!state.isInitialized || !state.isRunning) {
return const SizedBox.shrink();
}

switch (state.torchState) {
case TorchState.auto:
return IconButton(
iconSize: 32.0,
icon: const Icon(Icons.flash_auto),
onPressed: () async {
await controller.toggleTorch();
},
);
case TorchState.off:
return IconButton(
iconSize: 32.0,
icon: const Icon(Icons.flash_off),
onPressed: () async {
await controller.toggleTorch();
},
);
case TorchState.on:
return IconButton(
iconSize: 32.0,
icon: const Icon(Icons.flash_on),
onPressed: () async {
await controller.toggleTorch();
},
);
case TorchState.unavailable:
return const Icon(
Icons.no_flash,
color: Colors.grey,
);
}
},
);
}
}

class SwitchCameraButton extends StatelessWidget {
const SwitchCameraButton({required this.controller, super.key});

final MobileScannerController controller;

@override
Widget build(BuildContext context) {
return ValueListenableBuilder(
valueListenable: controller,
builder: (context, state, child) {
if (!state.isInitialized || !state.isRunning) {
return const SizedBox.shrink();
}

final int? availableCameras = state.availableCameras;

if (availableCameras != null && availableCameras < 2) {
return const SizedBox.shrink();
}

final Widget icon;

switch (state.cameraDirection) {
case CameraFacing.front:
icon = const Icon(Icons.camera_front);
case CameraFacing.back:
icon = const Icon(Icons.camera_rear);
}

return IconButton(
iconSize: 32.0,
icon: icon,
onPressed: () async {
await controller.switchCamera();
},
);
},
);
}
}

0 comments on commit 5c105f1

Please sign in to comment.