| @@ -29,21 +29,7 @@ class _QRViewExampleState extends State<QRViewExample> { | |||||
| return Scaffold( | return Scaffold( | ||||
| body: Column( | body: Column( | ||||
| children: <Widget>[ | children: <Widget>[ | ||||
| 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( | Expanded( | ||||
| flex: 1, | flex: 1, | ||||
| child: FittedBox( | child: FittedBox( | ||||
| @@ -141,6 +127,29 @@ class _QRViewExampleState extends State<QRViewExample> { | |||||
| return backCamera == current; | 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<SizeChangedLayoutNotification>( | |||||
| 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) { | void _onQRViewCreated(QRViewController controller) { | ||||
| this.controller = controller; | this.controller = controller; | ||||
| controller.scannedDataStream.listen((scanData) { | controller.scannedDataStream.listen((scanData) { | ||||
| @@ -63,9 +63,16 @@ public class QRView:NSObject,FlutterPlatformView { | |||||
| } | } | ||||
| func setDimensions(width: Double, height: Double) -> Void { | 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(){ | func flipCamera(){ | ||||
| @@ -100,11 +100,7 @@ class _CreationParams { | |||||
| class QRViewController { | class QRViewController { | ||||
| QRViewController._(int id, GlobalKey qrKey) | QRViewController._(int id, GlobalKey qrKey) | ||||
| : _channel = MethodChannel('net.touchcapture.qr.flutterqr/qrview_$id') { | : _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( | _channel.setMethodCallHandler( | ||||
| (call) async { | (call) async { | ||||
| switch (call.method) { | switch (call.method) { | ||||
| @@ -145,4 +141,12 @@ class QRViewController { | |||||
| void dispose() { | void dispose() { | ||||
| _scanUpdateController.close(); | _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}); | |||||
| } | |||||
| } | |||||
| } | } | ||||