flutter / flutter

Flutter makes it easy and fast to build beautiful apps for mobile and beyond
https://flutter.dev
BSD 3-Clause "New" or "Revised" License
162.17k stars 26.65k forks source link

`SystemChrome.setPreferredOrientations` landscape right or left doesn't work in some conditions #147460

Open Tom3652 opened 2 weeks ago

Tom3652 commented 2 weeks ago

Steps to reproduce

  1. Run the sample code
  2. Rotate the device to landscape mode according to the PreferredOrientations

Expected results

The device should rotate according to the PreferredOrientations selected in the List.

Actual results

  1. If you set :
 await SystemChrome.setPreferredOrientations([
    DeviceOrientation.portraitUp,
    DeviceOrientation.landscapeRight
  ]);

or :

 await SystemChrome.setPreferredOrientations([
    DeviceOrientation.portraitUp,
    DeviceOrientation.landscapeLeft
  ]);

The device doesn't rotate at all from portraitUp

  1. If you set :
 await SystemChrome.setPreferredOrientations([
    DeviceOrientation.portraitUp,
    DeviceOrientation.landscapeRight,
    DeviceOrientation.landscapeLeft
  ]);

The device rotates in all 3 orientations perfectly.

Code sample

Code sample ```dart Future main() async { WidgetsFlutterBinding.ensureInitialized(); await SystemChrome.setPreferredOrientations([ DeviceOrientation.portraitUp, DeviceOrientation.landscapeRight ]); runApp(const TestWidget()); } class TestWidget extends StatefulWidget { const TestWidget({super.key}); @override State createState() => _TestWidgetState(); } class _TestWidgetState extends State { @override Widget build(BuildContext context) { return const MaterialApp( home: Scaffold( body: Center( child: Text("Hello world !"), ), ), ); } } ```

Screenshots or Video

Screenshots / Video demonstration [Upload media here]

Logs

Logs ```console [Paste your logs here] ```

Flutter Doctor output

Doctor output ```console [✓] Flutter (Channel stable, 3.19.6, on macOS 14.4.1 23E224 darwin-x64, locale fr-FR) • Flutter version 3.19.6 on channel stable at /Users/foxtom/Desktop/flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision 54e66469a9 (10 days ago), 2024-04-17 13:08:03 -0700 • Engine revision c4cd48e186 • Dart version 3.3.4 • DevTools version 2.31.1 [✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0) • Android SDK at /Users/foxtom/Library/Android/sdk • Platform android-34, build-tools 34.0.0 • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 17.0.9+0-17.0.9b1087.7-11185874) • All Android licenses accepted. [✓] Xcode - develop for iOS and macOS (Xcode 15.3) • Xcode at /Applications/Xcode.app/Contents/Developer • Build 15E204a • CocoaPods version 1.15.2 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [✓] Android Studio (version 2023.2) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 17.0.9+0-17.0.9b1087.7-11185874) [✓] VS Code (version 1.88.1) • VS Code at /Users/foxtom/Desktop/Visual Studio Code.app/Contents • Flutter extension version 3.46.0 [✓] Connected device (5 available) • moto g 8 power (mobile) • ZY22BNDW2C • android-arm64 • Android 11 (API 30) • iPhone de Nathalie (mobile) • 00008030-000E64110A8A802E • ios • iOS 17.4.1 21E236 • Now You See Me (mobile) • 00008020-001204401E78002E • ios • iOS 17.4.1 21E236 • macOS (desktop) • macos • darwin-x64 • macOS 14.4.1 23E224 darwin-x64 • Chrome (web) • chrome • web-javascript • Google Chrome 124.0.6367.92 ! Error: Browsing on the local area network for Apple Watch de Tom. Ensure the device is unlocked and discoverable via Bluetooth. (code -27) [✓] Network resources • All expected network resources are available. • No issues found! ```
darshankawar commented 2 weeks ago

Thanks for the report @Tom3652 Is this specific to iOS or Android ? Also, please check below issues and see if they help in your case or not.

https://github.com/flutter/flutter/issues/34230 https://github.com/flutter/flutter/issues/73651

Tom3652 commented 2 weeks ago

Since you showed me older issues, i have tested them and i have also tested everything using the latest flutter version 3.19.6.

The below tests are made using my sample code in the issue.

  1. IOS :
    • Every rotation (or combination of rotation) is working fine except portraitDown with the error : Failed to change device orientation: Error Domain=UISceneErrorDomain Code=101 "None of the requested orientations are supported by the view controller. Requested: portraitUpsideDown; Supported: landscapeRight" UserInfo={NSLocalizedDescription=None of the requested orientations are supported by the view controller. Requested: portraitUpsideDown; Supported: landscapeRight}

Note : Rotations are blocked if you uncheck the rotation directly in Xcode (in that case, SystemChrome can't do anything)

Capture d’écran 2024-04-29 à 16 57 02
  1. Android :
    • Use portraitUp && landscapeRight or portraitUp && landscapeLeft, only portraitUp is working
    • Use portraitUp && landscapeRight && landscapeLeft it works fine
    • Use landscapeRight || landscapeLeft alone, it starts in the correct orientations but doesn't rotate to the other

This follows my initial issue comment.

For now it's all the tests i have made, i have not tried portraitDown on Android but will update the comment once i do it of course

mossmana commented 2 weeks ago

FYI team-ios: I'm already working on an issue that relates to orientation changes for Android - https://github.com/flutter/flutter/issues/144307. So, I can investigate this area of code as it relates to Android. However, there seems to be a cross-platform issue with SystemChrome.setPreferredOrientations. As noted above, there have also been a couple other issues reported in the past for iOS. In response to https://github.com/flutter/flutter/issues/147460#issuecomment-2083191592, we may want to clarify the expected iOS behavior in regards to Xcode.

mossmana commented 4 days ago

The solution to this issue will warrant additional discussion (see https://github.com/flutter/flutter/issues/148136).

So, I am unassigning myself for now.