在 Flutter 中锁定每个屏幕的屏幕方向在 iPad 上不起作用

问题描述 投票:0回答:2

我正在开发一个应用程序,根据它是哪个屏幕将屏幕方向锁定为纵向或横向。 IE。我有一个使用纵向模式的设置流程,但完成并启动“应用程序”后,我切换到横向模式。

无论如何,每个屏幕锁定代码(在谷歌搜索时推荐)在我的 iPhone 上工作正常但在我的 iPad 上它不起作用。

在 iPad 上,屏幕旋转并变黑。

锁定设置屏幕的代码:

  @override
  void initState() {
    super.initState();
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.portraitUp,
      DeviceOrientation.portraitDown,
    ]);
  }

是的,我已经完成了

 WidgetsFlutterBinding.ensureInitialized();
flutter ipad screen-orientation
2个回答
2
投票

我终于通过手动编辑 Info.plist 文件修复了它。

使用文本编辑器打开它并更改您找到的支持的方向值:

<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
</array>

第一个块用于 iPhone,第二个块用于 iPad(这将阻止多任务处理)


0
投票

默认情况下,ios 设备的方向设置为Portrait。要启用其他方向,请转到

{your_project}/ios/runner/
目录并打开
info.plist
文件并添加以下行

<key>UISupportedInterfaceOrientations</key> //This line indicates the preferred orientations for ios devices
<array>
    <string>UIInterfaceOrientationPortrait</string>  //This Line is there by default
    <string>UIInterfaceOrientationLandscapeLeft</string> //Add this line to enable landscape orientation
</array>
<key>UISupportedInterfaceOrientations~ipad</key>  //This line indicates the preferred orientations for ipados devices
<array>
    <string>UIInterfaceOrientationPortrait</string>
    <string>UIInterfaceOrientationPortraitUpsideDown</string>
    <string>UIInterfaceOrientationLandscapeLeft</string>
    <string>UIInterfaceOrientationLandscapeRight</string>
</array>

保存

info.plist
文件后重建你的flutter项目,它应该可以正常工作。

© www.soinside.com 2019 - 2024. All rights reserved.