This short article will guide you on how to make your Flutter app portrait-only or landscape-only.
If you set your app to portrait-only, it will always stay in portrait mode — even if the user rotates the device or changes the screen orientation from system settings.
You might want to do this if your UI design is made specifically for portrait mode.
Similarly, if your UI is designed for landscape mode, you can lock it to landscape-only.
If your app’s design supports both orientations, then there’s no need to add this setting.
You should add this setting inside your main() function:
void main() {
WidgetsFlutterBinding.ensureInitialized();
//Portrait
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown
]);
//Landscape
// SystemChrome.setPreferredOrientations([
// DeviceOrientation.landscapeLeft,
// DeviceOrientation.landscapeRight
// ]);
runApp(const MyApp());
}Dart
WidgetsFlutterBinding.ensureInitialized() makes sure Flutter’s engine and framework are ready before running any system-level code, like SystemChrome in our case.