Lock Device Orientation in Flutter
We may have noticed, in some mobile apps, we just can’t rotate them. Even if auto-rotation (or whatever thing it is) on, we can’t rotate the phone while we’re using those apps. To be honest, I first used this feature, just because I was too lazy to create a UI for landscape mode 😊. So I locked it and people will never be able to on Landscape mode.
In Flutter, It is quite easy to implement this.
First of all, we have to import flutter/services.dart
import 'package:flutter/services.dart';
Then, In our Apps main function, just before we call runApp
, call the below code,
await SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
This will lock our app’s orientation to portrait mode.
And if you wanna lock the orientation to landscape mode,
you can change DeviceOrientation.portraitUp
to
DeviceOrientation.landscapeLeft
or DeviceOrientation.landscapeRight
await SystemChrome.setPreferredOrientations([DeviceOrientation.landscapeLeft]);
await SystemChrome.setPreferredOrientations([DeviceOrientation.landscapeRight]);
I think you found something useful here. You can read more posts related to Flutter, by going to this section.
I’m always open to suggestions!
Let me know your suggestions and opinions in Twitter DM or drop a mail a [email protected].
If you’re feeling too generous, you can support me through Buy Me a Coffee.
Finally, if you found this helpful, please share this within your reach so that more people can benefit from this. And Follow me on Twitter for getting more posts like these 😉.