Reputation: 3108
I restricted my app for only portrait mode. The orientation was locked until I played a video full screen within my app with Youtube plugin. Once I exit the video, the app now can go landscape orientation as well if I tilt my phone.
Below code, I used for orientation restriction:
void main() {
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp])
.then((_) {
runApp(new MyApp());
});
}
Any Ideas, how can I keep the orientation locked?
Upvotes: 0
Views: 2608
Reputation: 1326
If you not want to go back from whole screen just change the orientation on back button use the given below code:
Future<bool> _checkForLandscape(BuildContext ctx) async {
if (MediaQuery.of(ctx).orientation == Orientation.landscape) {
await SystemChrome.setPreferredOrientations(
[
DeviceOrientation.portraitUp,
],
);
_controller.toggleFullScreenMode();
return false;
}
return true;
}
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () => _checkForLandscape(context),
child: _anyChild());}
Upvotes: 1
Reputation: 122
Use this on dispose:
@override
void dispose() {
_controller.dispose();
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
]);
super.dispose();
}
Upvotes: 3
Reputation: 240
Using
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
on build
method
@override
Widget build(BuildContext context) {
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
]);
return MaterialApp(...);
}
Upvotes: 1