Skip to content

Commit

Permalink
⚡️ Improve judgement
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexV525 committed Oct 27, 2023
1 parent 7c9849f commit 301e9f2
Showing 1 changed file with 15 additions and 24 deletions.
39 changes: 15 additions & 24 deletions lib/src/states/camera_picker_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,8 @@ class CameraPickerState extends State<CameraPicker>
/// 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 ||
Expand All @@ -492,36 +493,26 @@ class CameraPickerState extends State<CameraPicker>
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);
}
}
Expand Down

0 comments on commit 301e9f2

Please sign in to comment.