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>
相关推荐
开维游戏引擎3 小时前
AI自动生成游戏时,deepseek和mimo对比
android·游戏·语言模型·游戏引擎·ai编程
鹤卿1239 小时前
(OC)UI学习——网易云仿写
ui·ios·objective-c
不自律的笨鸟9 小时前
最新屏蔽 iOS 系统更新描述文件保姆级教程
ios
BreezeDove10 小时前
【Android】AS项目自动连接mumu模拟器配置
android
开心猴爷10 小时前
Flutter 如何自动上传 可以 IPA 把构建和上传分开处理
后端·ios
乐世东方客12 小时前
备份脚本记录(binlog文件+mysql+mongo)
android·数据库·mysql
风华圆舞12 小时前
在 Flutter 鸿蒙项目里接入文本转语音的完整思路
flutter·华为·harmonyos
私人珍藏库13 小时前
[Android] 视频下载鸟 v20.02 会员
android·人工智能·智能手机·app·工具·多功能
zh_xuan13 小时前
tv浏览网页工具
android·tv浏览网页
秋雨梧桐叶落莳14 小时前
iOS——QQ音乐仿写项目总结
学习·macos·ui·ios·mvc·objective-c·xcode