Flutter: how to prevent device orientation changes and force portrait?

flutter change screen orientation
flutter detect orientation change
flutter force app to portrait
how to make flutter app only portrait
flutter fix app orientation
flutter only allow portrait mode
flutter setpreferredorientations not working
how to use landscape mode in flutter

I would like to prevent my application from changing its orientation and force the layout to stick to "portrait".

In the main.dart, I put:

void main(){
  SystemChrome.setPreferredOrientations([
    DeviceOrientation.portraitUp,
    DeviceOrientation.portraitDown
  ]);
  runApp(new MyApp());
}

but when I use the Android Simulator rotate buttons, the layout "follows" the new device orientation...

How could I solve this?

Thanks


Import package:flutter/services.dart, then

Put the SystemChrome.setPreferredOrientations inside the Widget build() method.

Example:

  class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      SystemChrome.setPreferredOrientations([
        DeviceOrientation.portraitUp,
        DeviceOrientation.portraitDown,
      ]);
      return new MaterialApp(...);
    }
  }

Update

This solution mightn't work for some IOS devices as mentioned in the updated flutter documentation on Oct 2019.

They Advise to fixed the orientation by setting UISupportedInterfaceOrientations in Info.plist like this

<array>
    <string>UIInterfaceOrientationPortrait</string>
</array>

For more information https://github.com/flutter/flutter/issues/27235#issuecomment-508995063

How to restrict device orientation in Flutter - Jon Delgado, Ideally you'd want to “lock” the device orientation and not allow it to change as the user rotates their phone; and this was surprisingly tricky (and easy) to set… In mobile devices the default screen orientation mode is portrait. Every mobile phone has a Auto Rotation button present on Scrollable Menu bar. By default the Auto Rotation is disabled in mobile phones but we can enable them via Menu bar and the complete mobile phone screen will rotate on device rotation. But sometimes app developer wants to prevent Landscape mode and active only Portrait mode in flutter application.


@boeledi, If you want to "lock" the device orientation and not allow it to change as the user rotates their phone, this was easily set as below,

// This did not work as requirement
void main() {
  SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
  runApp(new MyApp());
}

You have to wait until setPreferredOrientations is done and then start the app

// This will works always for lock screen Orientation.
void main() {
  WidgetsFlutterBinding.ensureInitialized();
  SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp])
    .then((_) {
      runApp(new MyApp());
    });
}

Disable Screen Rotation Orientation in Flutter Enable Portrait Mode , DeviceOrientation.portraitUp enable only Portrait mode in flutter app.Disable Screen Rotation Orientation in Flutter Android iOS App Enable  Using the style of Flutter Logo, you can alter as : horizontal — Show Flutter’s logo on the left, and the “Flutter” label to its right. markOnly — Show only Flutter’s logo, not the “Flutter” label. This is the default behavior for FlutterLogoDecoration objects.


For iOS, calling SystemChrome.setPreferredOrientations() doesn't work for me, and I had to change that in the Xcode project.

Flutter, Flutter - How to force the Flutter application to remain in Portrait (vertical) mode even if the user is setPreferredOrientations([ DeviceOrientation. If you want to force to “Landscape” (horizontal) mode, change lines #5-8 to:. How to restrict device orientation in Flutter. Ideally you’d want to “lock” the device orientation and not allow it to change as the user rotates their phone; and this was surprisingly


The 'setPreferredOrientations' method returns a Future object. Per documentation a Future represents some value that will be available somewhere in future. That's why you shall wait until it's available and then move on with the application. Hence, there shall be used 'then' method, which, per definition, "registers callbacks to be called when the Future completes". Hence, you shall use this code:

  void main() {
  SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]).then((_) {
      runApp(new App());
    });
   }

Also, the following file must be imported:

'package:flutter/services.dart'

Flutter - Setting Orientation to Landscape or Portrait, How to force a Flutter application to use certain orientations app must always in portrait mode despite the device is in landscape orientation  Flutter: How to set and lock screen orientation on-demand. On one of my flutter pages, I need the screen to set to landscape mode and lock it so it can't rotate into portrait mode, but only on the one page. So need a way to enable this function on-the-fly.


Open android/app/src/main/AndroidManifest.xml and update this

<activity
        android:name=".MainActivity"
        android:launchMode="singleTop"
        android:theme="@style/LaunchTheme"
        android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
        android:hardwareAccelerated="true"
        android:windowSoftInputMode="adjustResize">

with this code

<activity
        android:name=".MainActivity"
        android:launchMode="singleTop"
        android:theme="@style/LaunchTheme"
        android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
        android:hardwareAccelerated="true"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="adjustResize">

It will work for android. For iOS, you can change from Xcode from this page https://i.stack.imgur.com/hswoe.png

That is How we can lock screen orientation in Flutter, In the most of the appliations you have to lock the orientation of mobile app most of the time in only portrait mode. We do it in android and ios  In Flutter, you can build different layouts depending on a given Orientation . In this example, build a list that displays two columns in portrait mode and three columns in landscape mode using the following steps: Build a GridView with two columns. Use an OrientationBuilder to change the number of columns.


Update the UI based on orientation, Respond to a change in the screen's orientation. want to update the display of an app when the user rotates the screen from portrait mode to landscape mode. start the project in a iOS device / simulator; tap the + button to change the orientation of the interface; Notice that: If the previous device orientation was one of the supported ones (e.g. you keep the device in portraitUp mode or you lock the screen rotation), pressing the button won't produce any visible effect.


SystemChrome.setPreferredOrientations does not force the device , Steps to Reproduce Running on iOS 10/11 Start an app in portrait mode and create a new flutter project; replace the contents on main.dart with this code: (​e.g. you keep the device in portraitUp mode or you lock the screen rotation), Issue 13238: on iOS, force an orientation change when the current  Caution: Beginning with Android 3.2 (API level 13), the "screen size" also changes when the device switches between portrait and landscape orientation. Thus, if you want to prevent runtime restarts due to orientation change when developing for API level 13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes), you must include the "screenSize" value in addition to the "orientation" value.


auto_orientation, Set the device orientation programmatically for iOS and Android. to programmatically change to landscape and back to portrait if the scaffold  Summary. Using the above methodology, you can force an orientation onto a single Xamarin.Forms page. Also, the above code is quite flexible and can easily be changed according to your needs. If I’ve missed something, go ahead and add it in the comments. I’ll make sure to add any needed changes to the post.