-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
496a8ae
commit 3a19a8d
Showing
5 changed files
with
88 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 33 additions & 73 deletions
106
frontend/lib/qr_code_scanner/qr_code_scanner_controls.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,44 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:flutter/material.dart'; | ||
import 'package:qr_code_scanner/qr_code_scanner.dart'; | ||
import 'package:mobile_scanner/mobile_scanner.dart'; | ||
|
||
class QrCodeScannerControls extends StatelessWidget { | ||
final QRViewController controller; | ||
|
||
// Not really nice. We need to update the button texts after we pressed them. | ||
// Other possibility would be to convert this to a stateful widget without | ||
// state and just call setState({}) to trigger redrawing of whole widget. | ||
final StreamController<bool> flashStreamController = StreamController(); | ||
final StreamController<CameraFacing> cameraStreamController = StreamController<CameraFacing>(); | ||
|
||
QrCodeScannerControls({super.key, required this.controller}) { | ||
updateFlashStream(); | ||
updateCameraStream(); | ||
} | ||
|
||
Future<void> updateFlashStream() => | ||
controller.getFlashStatus().then((flashStatus) => flashStreamController.add(flashStatus ?? false)); | ||
final MobileScannerController controller; | ||
|
||
Future<void> updateCameraStream() => controller.getCameraInfo().then(cameraStreamController.add); | ||
const QrCodeScannerControls({super.key, required this.controller}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return FutureBuilder<SystemFeatures>( | ||
future: _tryToGetSystemFeatures(), | ||
builder: (context, snapshot) { | ||
final SystemFeatures? systemFeatures = snapshot.data; | ||
|
||
if (snapshot.hasData || systemFeatures == null) { | ||
return const Center(); | ||
} | ||
|
||
return Row( | ||
mainAxisAlignment: MainAxisAlignment.center, | ||
crossAxisAlignment: CrossAxisAlignment.center, | ||
children: <Widget>[ | ||
if (systemFeatures.hasFlash) | ||
Container( | ||
margin: const EdgeInsets.all(8), | ||
child: OutlinedButton( | ||
onPressed: () => controller.toggleFlash().whenComplete(updateFlashStream), | ||
child: StreamBuilder<bool>( | ||
stream: flashStreamController.stream, | ||
builder: (ctx, snapshot) => Text( | ||
snapshot.data == null ? "Blitz aus" : "Blitz an", | ||
style: const TextStyle(fontSize: 16), | ||
), | ||
), | ||
), | ||
return Row( | ||
mainAxisAlignment: MainAxisAlignment.center, | ||
crossAxisAlignment: CrossAxisAlignment.center, | ||
children: <Widget>[ | ||
Container( | ||
margin: const EdgeInsets.all(8), | ||
child: OutlinedButton( | ||
onPressed: () => controller.toggleTorch(), | ||
child: ValueListenableBuilder( | ||
valueListenable: controller.torchState, | ||
builder: (ctx, state, child) => Text( | ||
state == TorchState.on ? "Blitz an" : "Blitz aus", | ||
style: const TextStyle(fontSize: 16), | ||
), | ||
), | ||
), | ||
), | ||
Container( | ||
margin: const EdgeInsets.all(8), | ||
child: OutlinedButton( | ||
onPressed: () => controller.switchCamera(), | ||
child: ValueListenableBuilder( | ||
valueListenable: controller.cameraFacingState, | ||
builder: (ctx, state, child) => Text( | ||
state == CameraFacing.back ? "Frontkamera" : "Standard-Kamera", | ||
style: const TextStyle(fontSize: 16), | ||
), | ||
if (systemFeatures.hasBackCamera && systemFeatures.hasFrontCamera) | ||
Container( | ||
margin: const EdgeInsets.all(8), | ||
child: OutlinedButton( | ||
onPressed: () => controller.flipCamera().whenComplete(updateCameraStream), | ||
child: StreamBuilder<CameraFacing>( | ||
stream: cameraStreamController.stream, | ||
builder: (ctx, snapshot) => Text( | ||
snapshot.data == CameraFacing.back ? "Frontkamera" : "Standard-Kamera", | ||
style: const TextStyle(fontSize: 16), | ||
), | ||
), | ||
), | ||
) | ||
], | ||
); | ||
}, | ||
), | ||
), | ||
) | ||
], | ||
); | ||
} | ||
|
||
Future<SystemFeatures> _tryToGetSystemFeatures() async { | ||
for (var i = 0; i < 10; i++) { | ||
try { | ||
return await controller.getSystemFeatures(); | ||
} on CameraException { | ||
await Future.delayed(const Duration(milliseconds: 50)); | ||
} | ||
} | ||
return SystemFeatures(true, true, true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters