From 28917a546e10e2ab524e5e32282747836e13436f Mon Sep 17 00:00:00 2001 From: Julian Steenbakker Date: Mon, 25 Jan 2021 15:48:37 +0100 Subject: [PATCH 1/3] Fixed permission callback. --- ios/Classes/QRView.swift | 18 ++++++++++++++++-- lib/src/qr_code_scanner.dart | 24 +++++++++--------------- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/ios/Classes/QRView.swift b/ios/Classes/QRView.swift index 1674187..7dcd106 100644 --- a/ios/Classes/QRView.swift +++ b/ios/Classes/QRView.swift @@ -46,7 +46,11 @@ public class QRView:NSObject,FlutterPlatformView { switch(call.method){ case "setDimensions": let arguments = call.arguments as! Dictionary - self?.setDimensions(result, width: arguments["width"] ?? 0, height: arguments["height"] ?? 0, scanArea: arguments["scanArea"] ?? 0) + self?.setDimensions(result, + width: arguments["width"] ?? 0, + height: arguments["height"] ?? 0, + scanArea: arguments["scanArea"] ?? 0, + scanAreaOffset: arguments["scanAreaOffset"] ?? 0) case "startScan": self?.startScan(call.arguments as! Array, result) case "flipCamera": @@ -71,7 +75,7 @@ public class QRView:NSObject,FlutterPlatformView { return previewView } - func setDimensions(_ result: @escaping FlutterResult, width: Double, height: Double, scanArea: Double) -> Void { + func setDimensions(_ result: @escaping FlutterResult, width: Double, height: Double, scanArea: Double, scanAreaOffset: Double) -> Void { MTBBarcodeScanner.requestCameraPermission(success: { permissionGranted in if permissionGranted { // First set the size of the preview area. @@ -88,6 +92,11 @@ public class QRView:NSObject,FlutterPlatformView { } if (scanArea != 0) { sc.scanRect = CGRect(x: Double(midX) - (scanArea / 2), y: Double(midY) - (scanArea / 2), width: scanArea, height: scanArea) + + // Set offset if provided. + if (scanAreaOffset != 0) { + sc.scanRect = sc.scanRect.offsetBy(dx: 0, dy: CGFloat(scanAreaOffset)) + } } } else { // Create a scanner view if it doesn't exist yet. @@ -96,6 +105,11 @@ public class QRView:NSObject,FlutterPlatformView { if (scanArea != 0) { self.scanner?.didStartScanningBlock = { self.scanner?.scanRect = CGRect(x: Double(midX) - (scanArea / 2), y: Double(midY) - (scanArea / 2), width: scanArea, height: scanArea) + + // Set offset if provided. + if (scanAreaOffset != 0) { + self.scanner?.scanRect = (self.scanner?.scanRect.offsetBy(dx: 0, dy: CGFloat(scanAreaOffset)))! + } } } } diff --git a/lib/src/qr_code_scanner.dart b/lib/src/qr_code_scanner.dart index 0ef137e..db7d2a1 100644 --- a/lib/src/qr_code_scanner.dart +++ b/lib/src/qr_code_scanner.dart @@ -73,8 +73,7 @@ class _QRViewState extends State { bool onNotification(notification) { Future.microtask(() => { QRViewController.updateDimensions(widget.key, _channel, - scanArea: - widget.overlay != null ? widget.overlay.cutOutSize : 0.0) + overlay: widget.overlay) }); return false; } @@ -122,18 +121,12 @@ class _QRViewState extends State { } void _onPlatformViewCreated(int id) { - // We pass the cutout size so that the scanner respects the scan area. - var cutOutSize = 0.0; - if (widget.overlay != null) { - cutOutSize = widget.overlay.cutOutSize; - } - _channel = MethodChannel('net.touchcapture.qr.flutterqr/qrview_$id'); // Start scan after creation of the view final controller = QRViewController._( - _channel, widget.key, cutOutSize, widget.onPermissionSet) - .._startScan(widget.key, cutOutSize, widget.formatsAllowed); + _channel, widget.key, widget.overlay, widget.onPermissionSet) + .._startScan(widget.key, widget.overlay, widget.formatsAllowed); // Initialize the controller for controlling the QRView if (widget.onQRViewCreated != null) { @@ -157,7 +150,7 @@ class _QrCameraSettings { } class QRViewController { - QRViewController._(MethodChannel channel, GlobalKey qrKey, double scanArea, + QRViewController._(MethodChannel channel, GlobalKey qrKey, QrScannerOverlayShape overlasdfay, PermissionSetCallback onPermissionSet) : _channel = channel { _channel.setMethodCallHandler((call) async { @@ -208,12 +201,12 @@ class QRViewController { bool get hasPermissions => _hasPermissions; /// Starts the barcode scanner - Future _startScan(GlobalKey key, double cutOutSize, + Future _startScan(GlobalKey key, QrScannerOverlayShape overlay, List barcodeFormats) async { // We need to update the dimension before the scan is started. try { await QRViewController.updateDimensions(key, _channel, - scanArea: cutOutSize); + overlay: overlay); return await _channel.invokeMethod( 'startScan', barcodeFormats?.map((e) => e.asInt())?.toList() ?? []); } on PlatformException catch (e) { @@ -295,14 +288,15 @@ class QRViewController { /// Updates the view dimensions for iOS. static Future updateDimensions(GlobalKey key, MethodChannel channel, - {double scanArea}) async { + {QrScannerOverlayShape overlay}) async { if (defaultTargetPlatform == TargetPlatform.iOS) { final RenderBox renderBox = key.currentContext.findRenderObject(); try { await channel.invokeMethod('setDimensions', { 'width': renderBox.size.width, 'height': renderBox.size.height, - 'scanArea': scanArea ?? 0 + 'scanArea': overlay.cutOutSize ?? 0, + 'scanAreaOffset': overlay.cutOutBottomOffset ?? 0 }); } on PlatformException catch (e) { throw CameraException(e.code, e.message); From 159bb3cc8228efb1c4fcfbaa6b84c46834709001 Mon Sep 17 00:00:00 2001 From: Julian Steenbakker Date: Wed, 3 Feb 2021 23:26:50 +0100 Subject: [PATCH 2/3] Removed overlay from controller --- lib/src/qr_code_scanner.dart | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/src/qr_code_scanner.dart b/lib/src/qr_code_scanner.dart index db7d2a1..a63fc98 100644 --- a/lib/src/qr_code_scanner.dart +++ b/lib/src/qr_code_scanner.dart @@ -124,9 +124,9 @@ class _QRViewState extends State { _channel = MethodChannel('net.touchcapture.qr.flutterqr/qrview_$id'); // Start scan after creation of the view - final controller = QRViewController._( - _channel, widget.key, widget.overlay, widget.onPermissionSet) - .._startScan(widget.key, widget.overlay, widget.formatsAllowed); + final controller = + QRViewController._(_channel, widget.key, widget.onPermissionSet) + .._startScan(widget.key, widget.overlay, widget.formatsAllowed); // Initialize the controller for controlling the QRView if (widget.onQRViewCreated != null) { @@ -150,7 +150,7 @@ class _QrCameraSettings { } class QRViewController { - QRViewController._(MethodChannel channel, GlobalKey qrKey, QrScannerOverlayShape overlasdfay, + QRViewController._(MethodChannel channel, GlobalKey qrKey, PermissionSetCallback onPermissionSet) : _channel = channel { _channel.setMethodCallHandler((call) async { @@ -205,8 +205,7 @@ class QRViewController { List barcodeFormats) async { // We need to update the dimension before the scan is started. try { - await QRViewController.updateDimensions(key, _channel, - overlay: overlay); + await QRViewController.updateDimensions(key, _channel, overlay: overlay); return await _channel.invokeMethod( 'startScan', barcodeFormats?.map((e) => e.asInt())?.toList() ?? []); } on PlatformException catch (e) { From 12e36c04e5031410ea3a3c24846c19974b2721d9 Mon Sep 17 00:00:00 2001 From: Julian Steenbakker Date: Wed, 3 Feb 2021 23:29:27 +0100 Subject: [PATCH 3/3] Removed deprecated enableR8 --- example/android/gradle.properties | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/example/android/gradle.properties b/example/android/gradle.properties index a673820..4d3226a 100644 --- a/example/android/gradle.properties +++ b/example/android/gradle.properties @@ -1,4 +1,3 @@ org.gradle.jvmargs=-Xmx1536M android.useAndroidX=true -android.enableJetifier=true -android.enableR8=true +android.enableJetifier=true \ No newline at end of file