prevent display orientation flip when device rotation is locked

This commit is contained in:
Thibault Deckers 2025-06-02 19:26:13 +02:00
parent a0163001bd
commit 5e0f0b59d8
2 changed files with 12 additions and 7 deletions

View file

@ -7,6 +7,7 @@ All notable changes to this project will be documented in this file.
### Changed ### Changed
- downgraded Flutter to stable v3.27.4 - downgraded Flutter to stable v3.27.4
- prevent display orientation flip when device rotation is locked
### Fixed ### Fixed

View file

@ -74,20 +74,24 @@ class PlatformWindowService implements WindowService {
return false; return false;
} }
// cf https://developer.android.com/guide/topics/manifest/activity-element#screen
// cf Android `ActivityInfo.ScreenOrientation`
static const screenOrientationUnspecified = -1; // SCREEN_ORIENTATION_UNSPECIFIED
// use the `USER` variants rather than the `SENSOR` ones,
// so that it does not flip even if it is reversed by sensor
static const screenOrientationUserLandscape = 11; // SCREEN_ORIENTATION_USER_LANDSCAPE
static const screenOrientationUserPortrait = 12; // SCREEN_ORIENTATION_USER_PORTRAIT
@override @override
Future<void> requestOrientation([Orientation? orientation]) async { Future<void> requestOrientation([Orientation? orientation]) async {
// cf Android `ActivityInfo.ScreenOrientation`
late final int orientationCode; late final int orientationCode;
switch (orientation) { switch (orientation) {
case Orientation.landscape: case Orientation.landscape:
// SCREEN_ORIENTATION_SENSOR_LANDSCAPE orientationCode = screenOrientationUserLandscape;
orientationCode = 6;
case Orientation.portrait: case Orientation.portrait:
// SCREEN_ORIENTATION_SENSOR_PORTRAIT orientationCode = screenOrientationUserPortrait;
orientationCode = 7;
default: default:
// SCREEN_ORIENTATION_UNSPECIFIED orientationCode = screenOrientationUnspecified;
orientationCode = -1;
} }
try { try {
await _platform.invokeMethod('requestOrientation', <String, dynamic>{ await _platform.invokeMethod('requestOrientation', <String, dynamic>{