1.Flutter 設置
在 main 函數 加載app前添加以下代碼
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp,DeviceOrientation.portraitDown,])
添加后的結果
void main() async {WidgetsBinding widgetsBinding = WidgetsFlutterBinding.ensureInitialized();// 在此處添加代碼SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp,DeviceOrientation.portraitDown,]);runApp(App());
}
*上面的代碼 可以應對很多情況,但是在ipad 中 可能會失效。android pad 沒問題。 為了避免出現問題可以在adnroid、ios 系統層面設置
Android - app-> main- > src ->??AndroidManifest.xml
在activity 標簽中增加?
android:screenOrientation="portrait"
添加后的樣子
<activityandroid:name=".MainActivity"android:exported="true"android:launchMode="singleTop"android:screenOrientation="portrait"android:theme="@style/LaunchTheme"android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"android:windowSoftInputMode="adjustResize">
.......
Ios -?Runner ->?Info.plist?
Iphone: 在?<key>UISupportedInterfaceOrientations</key>? 標簽中增加
<array><!-- <string>UIInterfaceOrientationPortrait</string><string>UIInterfaceOrientationLandscapeLeft</string><string>UIInterfaceOrientationLandscapeRight</string> --><string>UIInterfaceOrientationPortraitUpsideDown</string></array>
如果已經存在 你要注釋掉 左右 標簽。
Ipad? <key>UISupportedInterfaceOrientations~ipad</key> 此標簽 是設置ipad 屏幕轉向的
<array><!-- <string>UIInterfaceOrientationPortrait</string> --><string>UIInterfaceOrientationPortraitUpsideDown</string><!-- <string>UIInterfaceOrientationLandscapeLeft</string><string>UIInterfaceOrientationLandscapeRight</string> --></array>
以上標簽,沒有的添加上 已經存在的 進行修改即可。
最終樣子:
<key>UISupportedInterfaceOrientations</key><array><!-- <string>UIInterfaceOrientationPortrait</string><string>UIInterfaceOrientationLandscapeLeft</string><string>UIInterfaceOrientationLandscapeRight</string> --><string>UIInterfaceOrientationPortraitUpsideDown</string></array><key>UISupportedInterfaceOrientations~ipad</key><array><!-- <string>UIInterfaceOrientationPortrait</string> --><string>UIInterfaceOrientationPortraitUpsideDown</string><!-- <string>UIInterfaceOrientationLandscapeLeft</string><string>UIInterfaceOrientationLandscapeRight</string> --></array>