Flutter 用户尝试将设备转到横屏模式,应用界面将不会旋转以适应横屏

9 min read

在Flutter中,你可以通过设置orientation属性来控制应用界面是否旋转以适应横屏模式。默认情况下,Flutter应用会自动适应设备的横竖屏模式,但你可以通过以下步骤禁止应用界面旋转:

首先,在你的Flutter项目的AndroidManifest.xml文件中添加以下代码:

<activity android:name="io.flutter.embedding.android.FlutterActivity"
          android:screenOrientation="portrait">

上述代码将screenOrientation属性设置为portrait,这将固定应用界面为竖屏模式,禁止旋转。

然后,在你的Flutter项目的AppDelegate.swift文件中添加以下代码:

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    return UIInterfaceOrientationMask.portrait
}

上述代码将supportedInterfaceOrientations函数返回值设置为portrait,这也将固定应用界面为竖屏模式,禁止旋转。

请注意,对于Android和iOS平台,分别需要在AndroidManifest.xmlAppDelegate.swift文件中进行上述设置。

如果你想允许应用界面在横屏模式下旋转,只需将screenOrientation属性或supportedInterfaceOrientations函数返回值设置为unspecified即可。