Quellcode durchsuchen

Fixed permission callback.

flutter-beta
Julian Steenbakker vor 5 Jahren
Ursprung
Commit
28917a546e
2 geänderte Dateien mit 25 neuen und 17 gelöschten Zeilen
  1. +16
    -2
      ios/Classes/QRView.swift
  2. +9
    -15
      lib/src/qr_code_scanner.dart

+ 16
- 2
ios/Classes/QRView.swift Datei anzeigen

@@ -46,7 +46,11 @@ public class QRView:NSObject,FlutterPlatformView {
switch(call.method){
case "setDimensions":
let arguments = call.arguments as! Dictionary<String, Double>
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<Int>, 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)))!
}
}
}
}


+ 9
- 15
lib/src/qr_code_scanner.dart Datei anzeigen

@@ -73,8 +73,7 @@ class _QRViewState extends State<QRView> {
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<QRView> {
}

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<void> _startScan(GlobalKey key, double cutOutSize,
Future<void> _startScan(GlobalKey key, QrScannerOverlayShape overlay,
List<BarcodeFormat> 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<void> 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);


Laden…
Abbrechen
Speichern