Keep Your App in Portrait Mode
Ever found yourself wishing your app just stayed upright? Sometimes, you design an app specifically for portrait view, and having it switch to landscape just doesn't make sense.
Think about it, how often do you really use certain apps sideways? For many, keeping the screen vertical is key.
How to Lock to Portrait
Flutter makes it simple to lock your app's screen orientation. You can tell your app to only allow portrait modes.
The Simple Code
Here's the little piece of magic code you need:
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
Where to Put It
You need to run this code right at the start of your app. The best place is inside your main()
function, just before you call runApp()
.
void main() {
WidgetsFlutterBinding.ensureInitialized(); // Good practice
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
runApp(MyApp()); // Your app widget
}
What it Does
By setting the preferred orientations to only portraitUp
and portraitDown
, you prevent the app from rotating into landscape mode, no matter how the user holds their device.
And just like that, your app stays nicely in portrait mode!