From d09c9ebc39adc1b04483736598b020f6cd20514f Mon Sep 17 00:00:00 2001 From: Khoren Markosyan Date: Mon, 6 Jun 2022 13:45:13 +0400 Subject: [PATCH] added flash support --- example/pubspec.lock | 2 +- lib/reader_widget.dart | 68 ++++++++++++++++++++++++++++++++---------- zxscanner/pubspec.lock | 4 +-- zxscanner/pubspec.yaml | 2 +- 4 files changed, 57 insertions(+), 19 deletions(-) diff --git a/example/pubspec.lock b/example/pubspec.lock index 1cc25db..04054b6 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -141,7 +141,7 @@ packages: path: ".." relative: true source: path - version: "0.1.3" + version: "0.2.0" font_awesome_flutter: dependency: "direct main" description: diff --git a/lib/reader_widget.dart b/lib/reader_widget.dart index a138d07..c00433e 100644 --- a/lib/reader_widget.dart +++ b/lib/reader_widget.dart @@ -18,7 +18,8 @@ class ReaderWidget extends StatefulWidget { this.codeFormat = Format.Any, this.beep = true, this.showCroppingRect = true, - this.scanDelay = const Duration(milliseconds: 500), // 500ms delay + this.showFlashlight = true, + this.scanDelay = const Duration(milliseconds: 1000), // 1000ms delay this.cropPercent = 0.5, // 50% of the screen this.resolution = ResolutionPreset.high, }) : super(key: key); @@ -28,6 +29,7 @@ class ReaderWidget extends StatefulWidget { final int codeFormat; final bool beep; final bool showCroppingRect; + final bool showFlashlight; final Duration scanDelay; final double cropPercent; final ResolutionPreset resolution; @@ -112,6 +114,7 @@ class _ReaderWidgetState extends State try { await controller?.initialize(); + await controller?.setFlashMode(FlashMode.off); controller?.startImageStream(processCameraImage); } on CameraException catch (e) { debugPrint('${e.code}: ${e.description}'); @@ -202,22 +205,57 @@ class _ReaderWidgetState extends State ), ), ), - widget.showCroppingRect - ? Container( - decoration: ShapeDecoration( - shape: ScannerOverlay( - borderColor: Theme.of(context).primaryColor, - overlayColor: const Color.fromRGBO(0, 0, 0, 0.5), - borderRadius: 1, - borderLength: 16, - borderWidth: 8, - cutOutSize: cropSize, - ), - ), - ) - : Container() + if (widget.showCroppingRect) + Container( + decoration: ShapeDecoration( + shape: ScannerOverlay( + borderColor: Theme.of(context).primaryColor, + overlayColor: const Color.fromRGBO(0, 0, 0, 0.5), + borderRadius: 1, + borderLength: 16, + borderWidth: 8, + cutOutSize: cropSize, + ), + ), + ), + if (widget.showFlashlight) + Positioned( + top: 20, + left: 20, + child: FloatingActionButton( + onPressed: () { + FlashMode mode = cameraController.value.flashMode; + switch (mode) { + case FlashMode.torch: + mode = FlashMode.off; + break; + case FlashMode.off: + mode = FlashMode.torch; + break; + default: + } + cameraController.setFlashMode(mode); + setState(() {}); + }, + mini: true, + backgroundColor: Colors.black26, + child: Icon(_flashIcon(cameraController)), + ), + ) ], ); } } + + IconData _flashIcon(CameraController cameraController) { + final FlashMode mode = cameraController.value.flashMode; + switch (mode) { + case FlashMode.torch: + return Icons.flash_on; + case FlashMode.off: + return Icons.flash_off; + default: + return Icons.flash_off; + } + } } diff --git a/zxscanner/pubspec.lock b/zxscanner/pubspec.lock index 4b6247c..391758a 100644 --- a/zxscanner/pubspec.lock +++ b/zxscanner/pubspec.lock @@ -300,7 +300,7 @@ packages: path: ".." relative: true source: path - version: "0.1.3" + version: "0.2.0" font_awesome_flutter: dependency: "direct main" description: @@ -629,7 +629,7 @@ packages: name: share_plus url: "https://pub.dartlang.org" source: hosted - version: "4.0.6" + version: "4.0.7" share_plus_linux: dependency: transitive description: diff --git a/zxscanner/pubspec.yaml b/zxscanner/pubspec.yaml index 0be9e7c..68e3b9c 100644 --- a/zxscanner/pubspec.yaml +++ b/zxscanner/pubspec.yaml @@ -27,7 +27,7 @@ dependencies: intl: ^0.17.0 mobx: ^2.0.7 path_provider: ^2.0.10 - share_plus: ^4.0.6 + share_plus: ^4.0.7 shared_preferences: ^2.0.15 url_launcher: ^6.1.2