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>
相关推荐
一只大侠的侠12 分钟前
Flutter开源鸿蒙跨平台训练营 Day7Flutter+ArkTS双方案实现轮播图+搜索框+导航组件
flutter·开源·harmonyos
锐意无限1 小时前
Swift 扩展归纳--- UIView
开发语言·ios·swift
符哥20081 小时前
用Apollo + RxSwift + RxCocoa搭建一套网络请求框架
网络·ios·rxswift
一只大侠的侠1 小时前
Flutter开源鸿蒙跨平台训练营 Day9分类数据的获取与渲染实现
flutter·开源·harmonyos
一只大侠的侠1 小时前
Flutter开源鸿蒙跨平台训练营 Day 5Flutter开发鸿蒙电商应用
flutter·开源·harmonyos
lxysbly2 小时前
md模拟器安卓版带金手指2026
android
ZH15455891312 小时前
Flutter for OpenHarmony Python学习助手实战:GUI桌面应用开发的实现
python·学习·flutter
儿歌八万首2 小时前
硬核春节:用 Compose 打造“赛博鞭炮”
android·kotlin·compose·春节
一只大侠的侠3 小时前
Flutter开源鸿蒙跨平台训练营 Day6ArkUI框架实战
flutter·开源·harmonyos
一只大侠的侠4 小时前
Flutter开源鸿蒙跨平台训练营 Day 4实现流畅的下拉刷新与上拉加载效果
flutter·开源·harmonyos