Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restricting the flutter app's orientation

How can I restrict the app orientation on flutter?

SystemChrome.setPreferredOrientations([DeviceOrientation.portraitDown])

I've tried this code but it doesn't work out.

like image 991
Mohamed Nusky Avatar asked Dec 01 '25 07:12

Mohamed Nusky


2 Answers

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">
like image 54
King Elisha Avatar answered Dec 02 '25 21:12

King Elisha


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.

like image 36
drogel Avatar answered Dec 02 '25 23:12

drogel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!