diff --git a/android/src/main/kotlin/net/touchcapture/qr/flutterqr/QRView.kt b/android/src/main/kotlin/net/touchcapture/qr/flutterqr/QRView.kt index b3b6e70..4722d91 100644 --- a/android/src/main/kotlin/net/touchcapture/qr/flutterqr/QRView.kt +++ b/android/src/main/kotlin/net/touchcapture/qr/flutterqr/QRView.kt @@ -30,6 +30,7 @@ class QRView(private val registrar: PluginRegistry.Registrar, id: Int) : private val activity = registrar.activity() var cameraPermissionContinuation: Runnable? = null var requestingPermission = false + private var isTorchOn: Boolean = false val channel: MethodChannel init { @@ -80,14 +81,20 @@ class QRView(private val registrar: PluginRegistry.Registrar, id: Int) : barcodeView?.resume() } - fun flipFlash() { - barcodeView?.pause() - var settings = barcodeView?.cameraSettings - settings?.isAutoTorchEnabled = ! (settings?.isAutoTorchEnabled?:false) - barcodeView?.resume() + private fun toggleFlash() { + if (hasFlash()) { + barcodeView?.setTorch(!isTorchOn) + isTorchOn = !isTorchOn + } + } + private fun hasFlash(): Boolean { + return registrar.activeContext().packageManager + .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH) + } + override fun getView(): View { return initBarCodeView()?.apply { resume() @@ -132,7 +139,7 @@ class QRView(private val registrar: PluginRegistry.Registrar, id: Int) : private fun hasCameraPermission(): Boolean { return Build.VERSION.SDK_INT < Build.VERSION_CODES.M || - activity.checkSelfPermission(Manifest.permission.CAMERA) === PackageManager.PERMISSION_GRANTED + activity.checkSelfPermission(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED } @@ -144,8 +151,8 @@ class QRView(private val registrar: PluginRegistry.Registrar, id: Int) : "flipCamera" -> { flipCamera() } - "flipFlash" -> { - flipFlash() + "toggleFlash" -> { + toggleFlash() } } } diff --git a/example/lib/main.dart b/example/lib/main.dart index 00db09e..6f15e7f 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -50,7 +50,7 @@ class _QRViewExampleState extends State { child: RaisedButton( onPressed: () { if (controller != null) { - controller.flipFlash(); + controller.toggleFlash(); if (_isFlashOn(flashState)) setState(() { flashState = flash_off; diff --git a/ios/Classes/QRView.swift b/ios/Classes/QRView.swift index 76384b8..a83841d 100644 --- a/ios/Classes/QRView.swift +++ b/ios/Classes/QRView.swift @@ -48,8 +48,12 @@ public class QRView:NSObject,FlutterPlatformView { self?.setDimensions(width: arguments["width"] ?? 0,height: arguments["height"] ?? 0) case "flipCamera": self?.flipCamera() - case "flipFlash": - self?.flipFlash() + case "toggleFlash": + self?.toggleFlash() + case "pauseCamera": + self?.pauseCamera() + case "resumeCamera": + self?.resumeCamera() default: result(FlutterMethodNotImplemented) return @@ -72,7 +76,7 @@ public class QRView:NSObject,FlutterPlatformView { } } - func flipFlash(){ + func toggleFlash(){ if let sc: MTBBarcodeScanner = scanner { if sc.hasTorch() { sc.toggleTorch() diff --git a/lib/qr_code_scanner.dart b/lib/qr_code_scanner.dart index 7c463a4..0b0415f 100644 --- a/lib/qr_code_scanner.dart +++ b/lib/qr_code_scanner.dart @@ -83,12 +83,11 @@ class QRViewController { } } - void flipCamera(){ + void flipCamera() { channel.invokeMethod("flipCamera"); } - void flipFlash(){ - channel.invokeMethod("flipFlash"); + void toggleFlash() { + channel.invokeMethod("toggleFlash"); } - }