diff --git a/example/lib/main.dart b/example/lib/main.dart index cb5701c..8d957bd 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -29,21 +29,7 @@ class _QRViewExampleState extends State { return Scaffold( body: Column( children: [ - Expanded( - flex: 4, - child: QRView( - key: qrKey, - onQRViewCreated: _onQRViewCreated, - overlay: QrScannerOverlayShape( - borderColor: Colors.red, - borderRadius: 10, - borderLength: 30, - borderWidth: 10, - cutOutSize: 300, - cutOutBottomOffset: 24, - ), - ), - ), + Expanded(flex: 4, child: _buildQrView(context)), Expanded( flex: 1, child: FittedBox( @@ -141,6 +127,29 @@ class _QRViewExampleState extends State { return backCamera == current; } + Widget _buildQrView(BuildContext context) { + // To ensure the Scanner view is properly sizes after rotation + // we need to listen for Flutter SizeChanged notifiation and update controller + return NotificationListener( + onNotification: (notification) { + Future.microtask(() => controller?.updateDimensions(qrKey)); + return false; + }, + child: SizeChangedLayoutNotifier( + key: const Key('qr-size-notifier'), + child: QRView( + key: qrKey, + onQRViewCreated: _onQRViewCreated, + overlay: QrScannerOverlayShape( + borderColor: Colors.red, + borderRadius: 10, + borderLength: 30, + borderWidth: 10, + cutOutSize: 300, + ), + ))); + } + void _onQRViewCreated(QRViewController controller) { this.controller = controller; controller.scannedDataStream.listen((scanData) { diff --git a/ios/Classes/QRView.swift b/ios/Classes/QRView.swift index 376cec1..55426f0 100644 --- a/ios/Classes/QRView.swift +++ b/ios/Classes/QRView.swift @@ -63,9 +63,16 @@ public class QRView:NSObject,FlutterPlatformView { } func setDimensions(width: Double, height: Double) -> Void { - previewView.frame = CGRect(x: 0, y: 0, width: width, height: height) - scanner = MTBBarcodeScanner(previewView: previewView) - MTBBarcodeScanner.requestCameraPermission(success: isCameraAvailable) + previewView.frame = CGRect(x: 0, y: 0, width: width, height: height) + + if let sc: MTBBarcodeScanner = scanner { + if let previewLayer = sc.previewLayer { + previewLayer.frame = previewView.bounds; + } + } else { + scanner = MTBBarcodeScanner(previewView: previewView) + MTBBarcodeScanner.requestCameraPermission(success: isCameraAvailable) + } } func flipCamera(){ diff --git a/lib/src/qr_code_scanner.dart b/lib/src/qr_code_scanner.dart index 8bedf5e..70a96a4 100644 --- a/lib/src/qr_code_scanner.dart +++ b/lib/src/qr_code_scanner.dart @@ -100,11 +100,7 @@ class _CreationParams { class QRViewController { QRViewController._(int id, GlobalKey qrKey) : _channel = MethodChannel('net.touchcapture.qr.flutterqr/qrview_$id') { - if (defaultTargetPlatform == TargetPlatform.iOS) { - final RenderBox renderBox = qrKey.currentContext.findRenderObject(); - _channel.invokeMethod('setDimensions', - {'width': renderBox.size.width, 'height': renderBox.size.height}); - } + updateDimensions(qrKey); _channel.setMethodCallHandler( (call) async { switch (call.method) { @@ -145,4 +141,12 @@ class QRViewController { void dispose() { _scanUpdateController.close(); } + + void updateDimensions(GlobalKey key) { + if (defaultTargetPlatform == TargetPlatform.iOS) { + final RenderBox renderBox = key.currentContext.findRenderObject(); + _channel.invokeMethod('setDimensions', + {'width': renderBox.size.width, 'height': renderBox.size.height}); + } + } }