Reputation: 2213
I have tried using this code I found on a thread on this site but all that happens is my application loads a white screen. Is there a better way to force portrait view in flutter? Seems like simple thing to set.
void main() async {
///
/// Force the layout to Portrait mode
///
await SystemChrome.setPreferredOrientations(
[DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]);
runApp(new MaterialApp(
debugShowCheckedModeBanner: false,
home: LoginScreen(),
));
}
Upvotes: 1
Views: 1467
Reputation: 5838
From the exception in the comment:
Unhandled Exception: ServicesBinding.defaultBinaryMessenger was accessed before the binding was initialized
You need to call
WidgetsFlutterBinding.ensureInitialized();
Before your call to SystemChrome.setPreferredOrientations
Upvotes: 1
Reputation: 3602
void main() async {
WidgetsFlutterBinding.ensureInitialized();
///
/// Force the layout to Portrait mode
///
await SystemChrome.setPreferredOrientations(
[DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]);
runApp(new MaterialApp(
debugShowCheckedModeBanner: false,
home: LoginScreen(),
));
}
Require Full Screen
to true
. This will reflect in your ios/Runner/Info.plist to have the following value: <key>UIRequiresFullScreen</key>
<true/>
Well there are a couple of points to be addressed here:
WidgetsFlutterBinding.ensureInitialized()
. This IS important since you're "awaiting" on async main
method.setPreferredOrientations
's documentation, there's a limitation regarding iPad multitasking:This setting will only be respected on iPad if multitasking is disabled.
To alleviate #2, from the docs:
Should you decide to opt out of multitasking you can do this by setting "Requires full screen" to true in the Xcode Deployment Info.
Upvotes: 1
Reputation: 1180
Change your code as
void main() {
WidgetsFlutterBinding.ensureInitialized();
SystemChrome.setPreferredOrientations(
[DeviceOrientation.portraitUp, DeviceOrientation.portraitDown])
.then((_) => runApp(new MaterialApp(
debugShowCheckedModeBanner: false,
home: LoginScreen(),
)));
}
`
Upvotes: 5