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>
相关推荐
北京自在科技39 分钟前
苹果折叠屏iPhone突破折痕难题 或将在2026年发布
ios·iphone·findmy·折叠屏·库克
一粒沙-40 分钟前
iOS 将GIF图分享至微信
前端·ios
Mr.pyZhang4 小时前
Android构建系统 - 04 编译产物
android·linux
守城小轩10 小时前
Brave 132 编译指南 Android 篇 - 编译准备:系统要求与推荐工具 (三)
android·chrome·chrome devtools·指纹浏览器·浏览器开发·brave
m0_7482384210 小时前
MySQL Workbench菜单汉化为中文
android·数据库·mysql
程序员江同学11 小时前
Kotlin 技术月报 | 2025 年 2 月
android·kotlin
Mamong11 小时前
Metal 学习笔记三:渲染管线
ios·swift·metal
甜于酸12 小时前
MuMu模拟器Pro for Mac 安卓手机平板模拟器
android·macos·智能手机
yidahis15 小时前
用DeepSeek学源码-SDImageCodersManager 中的工厂模式解析
ios·源码
顾林海15 小时前
深入解析 Android Activity 生命周期
android