diff --git a/README.md b/README.md index 476deff..096e8cb 100644 --- a/README.md +++ b/README.md @@ -64,12 +64,16 @@ class _QRViewExampleState extends State { Barcode result; QRViewController controller; - /// Overriding reassemble and pausing the camera - /// ensures us that hot reload won't give a black screen + // In order to get hot reload to work we need to pause the camera if the platform + // is android, or resume the camera if the platform is iOS. @override void reassemble() { super.reassemble(); - controller.pauseCamera(); + if (Platform.isAndroid) { + controller.pauseCamera(); + } else if (Platform.isIOS) { + controller.resumeCamera(); + } } @override diff --git a/example/README.md b/example/README.md index 866b9a4..20d5579 100644 --- a/example/README.md +++ b/example/README.md @@ -47,12 +47,16 @@ class _QRViewExampleState extends State { QRViewController controller; final GlobalKey qrKey = GlobalKey(debugLabel: 'QR'); - /// Overriding reassemble and pausing the camera - /// ensures us that hot reload won't give a black screen + // In order to get hot reload to work we need to pause the camera if the platform + // is android, or resume the camera if the platform is iOS. @override void reassemble() { super.reassemble(); - controller.pauseCamera(); + if (Platform.isAndroid) { + controller.pauseCamera(); + } else if (Platform.isIOS) { + controller.resumeCamera(); + } } @override @@ -69,7 +73,8 @@ class _QRViewExampleState extends State { mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ if (result != null) - Text('Barcode Type: ${describeEnum(result.format)} Data: ${result.code}') + Text( + 'Barcode Type: ${describeEnum(result.format)} Data: ${result.code}') else Text('Scan a code'), Row( @@ -162,11 +167,17 @@ class _QRViewExampleState extends State { } Widget _buildQrView(BuildContext context) { + // For this example we check how width or tall the device is and change the scanArea and overlay accordingly. + var scanArea = (MediaQuery.of(context).size.width < 400 || + MediaQuery.of(context).size.height < 400) + ? 150.0 + : 300.0; // To ensure the Scanner view is properly sizes after rotation // we need to listen for Flutter SizeChanged notification and update controller return NotificationListener( onNotification: (notification) { - Future.microtask(() => controller?.updateDimensions(qrKey)); + Future.microtask( + () => controller?.updateDimensions(qrKey, scanArea: scanArea)); return false; }, child: SizeChangedLayoutNotifier( @@ -179,7 +190,7 @@ class _QRViewExampleState extends State { borderRadius: 10, borderLength: 30, borderWidth: 10, - cutOutSize: 300, + cutOutSize: scanArea, ), ))); } @@ -201,6 +212,7 @@ class _QRViewExampleState extends State { } + ``` diff --git a/example/ios/Runner.xcodeproj/project.pbxproj b/example/ios/Runner.xcodeproj/project.pbxproj index 1bb97b9..df30a3b 100644 --- a/example/ios/Runner.xcodeproj/project.pbxproj +++ b/example/ios/Runner.xcodeproj/project.pbxproj @@ -384,7 +384,7 @@ "$(inherited)", "$(PROJECT_DIR)/Flutter", ); - PRODUCT_BUNDLE_IDENTIFIER = dev.steenbakker.capture; + PRODUCT_BUNDLE_IDENTIFIER = dev.steenbakker.qr; PRODUCT_NAME = "$(TARGET_NAME)"; PROVISIONING_PROFILE_SPECIFIER = ""; SWIFT_VERSION = 4.0; @@ -522,7 +522,7 @@ "$(inherited)", "$(PROJECT_DIR)/Flutter", ); - PRODUCT_BUNDLE_IDENTIFIER = dev.steenbakker.capture; + PRODUCT_BUNDLE_IDENTIFIER = dev.steenbakker.qr; PRODUCT_NAME = "$(TARGET_NAME)"; PROVISIONING_PROFILE_SPECIFIER = ""; SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h"; @@ -553,7 +553,7 @@ "$(inherited)", "$(PROJECT_DIR)/Flutter", ); - PRODUCT_BUNDLE_IDENTIFIER = dev.steenbakker.capture; + PRODUCT_BUNDLE_IDENTIFIER = dev.steenbakker.qr; PRODUCT_NAME = "$(TARGET_NAME)"; PROVISIONING_PROFILE_SPECIFIER = ""; SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h"; diff --git a/example/lib/main.dart b/example/lib/main.dart index 4d5680c..beb8d51 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -1,3 +1,5 @@ +import 'dart:io'; + import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:qr_code_scanner/qr_code_scanner.dart'; @@ -25,10 +27,16 @@ class _QRViewExampleState extends State { QRViewController controller; final GlobalKey qrKey = GlobalKey(debugLabel: 'QR'); + // In order to get hot reload to work we need to pause the camera if the platform + // is android, or resume the camera if the platform is iOS. @override void reassemble() { super.reassemble(); - controller.pauseCamera(); + if (Platform.isAndroid) { + controller.pauseCamera(); + } else if (Platform.isIOS) { + controller.resumeCamera(); + } } @override @@ -139,11 +147,17 @@ class _QRViewExampleState extends State { } Widget _buildQrView(BuildContext context) { + // For this example we check how width or tall the device is and change the scanArea and overlay accordingly. + var scanArea = (MediaQuery.of(context).size.width < 400 || + MediaQuery.of(context).size.height < 400) + ? 150.0 + : 300.0; // To ensure the Scanner view is properly sizes after rotation // we need to listen for Flutter SizeChanged notification and update controller return NotificationListener( onNotification: (notification) { - Future.microtask(() => controller?.updateDimensions(qrKey)); + Future.microtask( + () => controller?.updateDimensions(qrKey, scanArea: scanArea)); return false; }, child: SizeChangedLayoutNotifier( @@ -156,7 +170,7 @@ class _QRViewExampleState extends State { borderRadius: 10, borderLength: 30, borderWidth: 10, - cutOutSize: 300, + cutOutSize: scanArea, ), ))); } diff --git a/ios/Classes/QRView.swift b/ios/Classes/QRView.swift index eeb3bfd..298f7a9 100644 --- a/ios/Classes/QRView.swift +++ b/ios/Classes/QRView.swift @@ -53,7 +53,6 @@ public class QRView:NSObject,FlutterPlatformView { default: return } - guard let stringValue = code.stringValue else { continue } let result = ["code": stringValue, "type": typeString] self?.channel.invokeMethod("onRecognizeQR", arguments: result) @@ -74,7 +73,7 @@ public class QRView:NSObject,FlutterPlatformView { switch(call.method){ case "setDimensions": let arguments = call.arguments as! Dictionary - self?.setDimensions(width: arguments["width"] ?? 0,height: arguments["height"] ?? 0) + self?.setDimensions(width: arguments["width"] ?? 0, height: arguments["height"] ?? 0, scanArea: arguments["scanArea"] ?? 0) case "flipCamera": self?.flipCamera() case "toggleFlash": @@ -91,15 +90,24 @@ public class QRView:NSObject,FlutterPlatformView { return previewView } - func setDimensions(width: Double, height: Double) -> Void { + func setDimensions(width: Double, height: Double, scanArea: Double) -> Void { previewView.frame = CGRect(x: 0, y: 0, width: width, height: height) - + let midX = self.view().bounds.midX + let midY = self.view().bounds.midY if let sc: MTBBarcodeScanner = scanner { if let previewLayer = sc.previewLayer { previewLayer.frame = previewView.bounds; } } else { scanner = MTBBarcodeScanner(previewView: previewView) + + if (scanArea != 0) { + scanner?.didStartScanningBlock = { + self.scanner?.scanRect = CGRect(x: Double(midX) - (scanArea / 2), y: Double(midY) - (scanArea / 2), width: scanArea, height: scanArea) + } + } + + MTBBarcodeScanner.requestCameraPermission(success: isCameraAvailable) } } diff --git a/lib/src/qr_code_scanner.dart b/lib/src/qr_code_scanner.dart index 451f240..aa7d20a 100644 --- a/lib/src/qr_code_scanner.dart +++ b/lib/src/qr_code_scanner.dart @@ -3,6 +3,7 @@ import 'dart:async'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; +import 'package:qr_code_scanner/qr_code_scanner.dart'; typedef QRViewCreatedCallback = void Function(QRViewController); @@ -110,7 +111,7 @@ class _QRViewState extends State { Widget build(BuildContext context) { return Stack( children: [ - _getPlatformQrView(), + _getPlatformQrView(widget.key), if (widget.overlay != null) Container( padding: widget.overlayMargin, @@ -124,7 +125,7 @@ class _QRViewState extends State { ); } - Widget _getPlatformQrView() { + Widget _getPlatformQrView(GlobalKey key) { Widget _platformQrView; switch (defaultTargetPlatform) { case TargetPlatform.android: @@ -137,7 +138,9 @@ class _QRViewState extends State { _platformQrView = UiKitView( viewType: 'net.touchcapture.qr.flutterqr/qrview', onPlatformViewCreated: _onPlatformViewCreated, - creationParams: _CreationParams.fromWidget(0, 0).toMap(), + creationParams: + _CreationParams.fromWidget(MediaQuery.of(context).size.width, 400) + .toMap(), creationParamsCodec: StandardMessageCodec(), ); break; @@ -152,7 +155,9 @@ class _QRViewState extends State { if (widget.onQRViewCreated == null) { return; } - widget.onQRViewCreated(QRViewController._(id, widget.key)); + + widget.onQRViewCreated(QRViewController._( + id, widget.key, (widget.overlay as QrScannerOverlayShape).cutOutSize)); } } @@ -178,9 +183,9 @@ class _CreationParams { } class QRViewController { - QRViewController._(int id, GlobalKey qrKey) + QRViewController._(int id, GlobalKey qrKey, double scanArea) : _channel = MethodChannel('net.touchcapture.qr.flutterqr/qrview_$id') { - updateDimensions(qrKey); + updateDimensions(qrKey, scanArea: scanArea); _channel.setMethodCallHandler( (call) async { switch (call.method) { @@ -231,11 +236,14 @@ class QRViewController { _scanUpdateController.close(); } - void updateDimensions(GlobalKey key) { + void updateDimensions(GlobalKey key, {double scanArea}) { if (defaultTargetPlatform == TargetPlatform.iOS) { final RenderBox renderBox = key.currentContext.findRenderObject(); - _channel.invokeMethod('setDimensions', - {'width': renderBox.size.width, 'height': renderBox.size.height}); + _channel.invokeMethod('setDimensions', { + 'width': renderBox.size.width, + 'height': renderBox.size.height, + 'scanArea': scanArea ?? 0 + }); } } }