From 301e9f22e3fe3fb927b76a6b4b4b10d30e8e85cc Mon Sep 17 00:00:00 2001 From: Alex Li Date: Fri, 27 Oct 2023 14:48:08 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20Improve=20judgement?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/src/states/camera_picker_state.dart | 39 ++++++++++--------------- 1 file changed, 15 insertions(+), 24 deletions(-) diff --git a/lib/src/states/camera_picker_state.dart b/lib/src/states/camera_picker_state.dart index 50cf77a..b6d4c85 100644 --- a/lib/src/states/camera_picker_state.dart +++ b/lib/src/states/camera_picker_state.dart @@ -483,7 +483,8 @@ class CameraPickerState extends State /// Lock capture orientation according to the current status of the device, /// which enables the captured file stored the correct orientation. void handleAccelerometerEvent(AccelerometerEvent event) { - if (pickerConfig.lockCaptureOrientation != null || + if (!mounted || + pickerConfig.lockCaptureOrientation != null || innerController == null || !controller.value.isInitialized || controller.value.isPreviewPaused || @@ -492,36 +493,26 @@ class CameraPickerState extends State return; } final x = event.x, y = event.y, z = event.z; - // realDebugPrint('X:$x Y:$y Z:$z'); - final bool isLeft; - if (x > 0) { - isLeft = Platform.isAndroid ? true : false; - } else { - isLeft = Platform.isAndroid ? false : true; - } - if (!mounted) { - return; - } - final DeviceOrientation newOrientation; - if (z < 9) { - if (y > 5) { - // realDebugPrint('Accelerometer portrait'); - newOrientation = DeviceOrientation.portraitUp; - } else if (y < 5) { - // realDebugPrint('Accelerometer landscape'); - newOrientation = isLeft - ? DeviceOrientation.landscapeLeft - : DeviceOrientation.landscapeRight; + final DeviceOrientation? newOrientation; + if (x.abs() > y.abs() && x.abs() > z.abs()) { + if (x > 0) { + newOrientation = DeviceOrientation.landscapeLeft; } else { + newOrientation = DeviceOrientation.landscapeRight; + } + } else if (y.abs() > x.abs() && y.abs() > z.abs()) { + if (y > 0) { newOrientation = DeviceOrientation.portraitUp; + } else { + newOrientation = DeviceOrientation.portraitDown; } } else { - // Fallback. - newOrientation = DeviceOrientation.portraitUp; + newOrientation = null; } // Throttle. - if (lockedCaptureOrientation != newOrientation) { + if (newOrientation != null && lockedCaptureOrientation != newOrientation) { lockedCaptureOrientation = newOrientation; + realDebugPrint('Locking new capture orientation: $newOrientation'); controller.lockCaptureOrientation(newOrientation); } }