瀏覽代碼

Fixed rotation on iOS.

flutter-beta
Julian Steenbakker 5 年之前
父節點
當前提交
352bd1ed9e
沒有發現已知的金鑰在資料庫的簽署中 GPG 金鑰 ID: 16E437C7A3D94250
共有 3 個檔案被更改,包括 43 行新增23 行删除
  1. +24
    -15
      example/lib/main.dart
  2. +10
    -3
      ios/Classes/QRView.swift
  3. +9
    -5
      lib/src/qr_code_scanner.dart

+ 24
- 15
example/lib/main.dart 查看文件

@@ -29,21 +29,7 @@ class _QRViewExampleState extends State<QRViewExample> {
return Scaffold(
body: Column(
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(
flex: 1,
child: FittedBox(
@@ -141,6 +127,29 @@ class _QRViewExampleState extends State<QRViewExample> {
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) {
this.controller = controller;
controller.scannedDataStream.listen((scanData) {


+ 10
- 3
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(){


+ 9
- 5
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});
}
}
}

Loading…
取消
儲存