How can I restrict the app orientation on flutter?
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitDown])
I've tried this code but it doesn't work out.
For android, you can define the orientation in your AndroidManifest.xml using the android:screenOrientation property
<activity
android:name=".MainActivity"
android:launchMode="singleTop"
android:screenOrientation="portrait"
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
What you tried works, actually. However, it is not forcing the orientation, it is merely preferring it. That means, for example, that if you try to set your orientation to landscape with
SystemChrome.setPreferredOrientations([DeviceOrientation.landscape])
Flutter will not rotate the orientation, but if you rotate your device from portrait to landscape, then the app orientation will be locked to landscape.
If you want Flutter to programatically rotate the orientation, forcing it instead of preferring it, consider using the auto_orientation package.
After importing the package you can use AutoOrientation.landscapeAutoMode(); to lock the orientation to landscape.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With