flutter 禁止横屏设置

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"

添加后的样子

复制代码
<activity
            android: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>
相关推荐
每次的天空1 小时前
Android-RecyclerView学习总结
android·学习
死也不注释2 小时前
【1——Android端添加隐私协议(unity)1/3】
android·unity·游戏引擎
老梁学Android4 小时前
oppo手机安装APK失败报错:安装包异常
android·智能手机
tmacfrank10 小时前
Android 性能优化入门(二)—— 内存优化
android·性能优化
安和昂10 小时前
iOS 初识RunLoop
macos·ios·cocoa
奔跑吧 android10 小时前
【android bluetooth 协议分析 02】【bluetooth hal 层详解 3】【高通蓝牙hal主要流程介绍-上】
android·bluetooth·qcom·bt_hal_1.0·qcom_bt·高通蓝牙hal
追随远方12 小时前
Android OkHttp控制链:深入理解网络请求的流程管理
android·网络·okhttp
stevenzqzq13 小时前
kotlin flow的两种SharingStarted策略的区别
android·flow
I烟雨云渊T14 小时前
iOS Runtime与RunLoop的对比和使用
ios
掘金-我是哪吒14 小时前
分布式微服务系统架构第138集:打包发布全流程(iOS + Android)
android·微服务·云原生·架构