I have a final user design with landscape orientation. User doesn't want/need portrait and it is needed to avoid automatic orientation change on iOS/Android. How can I achieve that?
Asked
Active
Viewed 8,926 times
8
-
please find this updated answer https://stackoverflow.com/a/50884081/348589 – Shady Mohamed Sherif Jan 04 '20 at 16:40
3 Answers
18
SystemChrome
is what you want
You can do something like in main.dart (don't forget import 'package:flutter/services.dart')
SystemChrome.setPreferredOrientations([
DeviceOrientation.landscapeRight,
DeviceOrientation.landscapeLeft,
]);
Unfortunately, when I do this in my application, the orientation will always be landscapeRight
.
To lock the orientation for iOS you need to change the settings for the XCode project (use command 'open ios/Runner.xcworkspace' in terminal to open it)

Ernesto Campohermoso
- 7,213
- 1
- 40
- 51

Rémi Rousselet
- 256,336
- 79
- 519
- 432
-
1Any import needed? Inside main()? or class MyApp? Please be a little more specific. Red underlined over SystemChrome (Undefined name) and DeviceOrientation too. Thanks. – abnerh69 Nov 20 '17 at 14:36
-
2Any import needed? YES! import 'package:flutter/services.dart'. Inside main()? YES. or class MyApp? MAYBE YES!. Thanks. – abnerh69 Nov 20 '17 at 14:42
-
2@abnerh69 - To lock the orientation for iOS you need to change the settings for the XCode project (use command 'open ios/Runner.xcworkspace' in terminal to open it) – Nathan Bird Mar 01 '18 at 23:21
-
Did somebody know, why upside down doesn't work? DeviceOrientation.portraitDown, DeviceOrientation.portraitUp does not working. – Auryn May 21 '18 at 21:43
-
1Is there any way to lock the orientation of a specific screen of an app, rather than for all screens of the app? – Michael Nelson Jun 15 '18 at 15:29
15
void main() {
SystemChrome.setPreferredOrientations([DeviceOrientation.landscapeLeft])
.then((_) {
runApp(new MyApp());
});
}
SystemChrome.setPreferredOrientations
returns a Future. Wait until that’s done and then we can start our app.

Dhaval
- 2,724
- 2
- 24
- 33
3
Add this line of code in your main.dart file
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);

Fayaz
- 1,846
- 2
- 16
- 21