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】设备操作
android
五味香1 小时前
C++学习,信号处理
android·c语言·开发语言·c++·学习·算法·信号处理
图王大胜3 小时前
Android Framework AMS(01)AMS启动及相关初始化1-4
android·framework·ams·systemserver
工程师老罗5 小时前
Android Button “No speakable text present” 问题解决
android
小雨cc5566ru6 小时前
hbuilderx+uniapp+Android健身房管理系统 微信小程序z488g
android·微信小程序·uni-app
小雨cc5566ru7 小时前
微信小程序hbuilderx+uniapp+Android 新农村综合风貌旅游展示平台
android·微信小程序·uni-app
小雨cc5566ru7 小时前
小程序 uniapp+Android+hbuilderx体育场地预约管理系统的设计与实现
android·小程序·uni-app
奇客软件8 小时前
如何从相机的记忆棒(存储卡)中恢复丢失照片
深度学习·数码相机·ios·智能手机·电脑·笔记本电脑·iphone
佛系小嘟嘟9 小时前
Android-由switch-case和view.getId()引起的bug:错误:需要常量表达式 的解决办法
android·bug
勿问东西9 小时前
【Android】事件
android