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 mai
This solution has worked for me on two different projects for Android. Can't guarantee it will work for iOS too. I've read all the previous answers and I think the best and simplest solution is to do it like this:
This way you avoid all the "await"s, "async"s and "then"s that might mess around with your code
/// this is in main.dart
main(){
WidgetsFlutterBinding.ensureInitialized();
runApp(
MaterialApp(
initialRoute: '/root',
routes: {
'/root': (context) => MyApp(),
},
title: "Your App Title",
),
);
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
/// ENFORCE DEVICE ORIENTATION PORTRAIT ONLY
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown
]);
/// RUN THE APP
return MaterialApp(
home: HomeScreen(),
);
}
}
I think this answer's update is your best bet for iOS.
!! DISCLAIMER !! I did a little bit of research and apparently, the documentation here specifically says:
setPreferredOrientations method Limitations:
"This setting will only be respected on iPad if multitasking is disabled."
Here is how you can disable multitasking (AKA turn on "Requires Full Screen" option) from XCode
You might also try this one out. I haven't tried it personally, since I don't have an iPad or XCode on my PC, but it's worth a shot
First of all import this in main.dart file
import 'package:flutter/services.dart';
Then don't copy paste rather see(remember) and write below code in main.dart file
To force in portrait mode:
void main() {
SystemChrome.setPreferredOrientations(
[DeviceOrientation.portraitUp,DeviceOrientation.portraitDown])
.then((_) => runApp(MyApp()),
);
To force in landscape mode:
void main() {
SystemChrome.setPreferredOrientations(
[DeviceOrientation.landscapeLeft,DeviceOrientation.landscapeRight])
.then((_) => runApp(MyApp()),
);
As of new flutter versions along with setting the preferred Orientation
we need to add one extra line i.e
WidgetsFlutterBinding.ensureInitialized();
So working code for this is -
import 'package:flutter/services.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown
]);
runApp(MyApp());
}
The best solution will be to use it in the build method of MyApp().
@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());
});
}
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'