How to set Orientation Preference in Flutter

Viewed 171

On my flutter app, I need the screen to be portrait mode only. Either portraitUp or portraitDown. So I added this code in the main file:

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

But when I tested this on an emulator. It only stays in portraitUp. Even when I switch the emulator to portraitDown. What's causing this and how do I fix this?

1 Answers

SystemChrome code should be inside the build method of the main widget which returns MaterialApp.

Or do it natively,

In your AndroidManifest.xml, in <activity> tag add this android:screenOrientation="portrait". Like,

<activity
...
android:screenOrientation="portrait"
>
...
</activity>

And in iOS, select target (Runner) > General > Deployment Info > Device Orientation and select desired orientations

Related