【问题标题】:How to set my app to landscape mode in Flutter?如何在 Flutter 中将我的应用设置为横向模式?
【发布时间】:2019-02-21 00:49:45
【问题描述】:

我想将我的应用程序的默认方向设置为横向,就像我打开一个游戏应用程序(如部落冲突或移动传奇)时一样。我怎么能在颤振中做到这一点?

【问题讨论】:

  • 欢迎来到 SO。你能更详细地描述你的问题吗?例如。通过添加描述您的问题的代码、命令或屏幕截图。另请查看帮助中心,尤其是askingminimal examples。谢谢。

标签: flutter flutter-layout


【解决方案1】:

将此代码放在 MyApp() 中

  SystemChrome.setPreferredOrientations([
    DeviceOrientation.landscapeLeft,
    DeviceOrientation.landscapeRight,
  ]);

如下:

class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      SystemChrome.setPreferredOrientations([
        DeviceOrientation.landscapeLeft,
        DeviceOrientation.landscapeRight,
      ]);
      return new MaterialApp();
    }
  }

如果您想要纵向模式,请查看此answer

【讨论】:

  • 通过使用上述解决方案,我认为第一次应用程序将以纵向模式打开,然后在将方向纵向更改为横向后保持横向。我的意思是您必须将第一次纵向更改为横向应用程序不会以横向打开。
【解决方案2】:

您必须单独配置 iOS 和 Android。

对于 iOS,请转到 Xcode,选择您的项目 > 常规 > 部署信息 > 设备方向并仅选择横向选项。

对于 Android,将 android:screenOrientation="landscape" 添加到您的 <activity> 标记中。

【讨论】:

  • 我发现我需要这样做,@diegoveloper 建议强制 Flutter 应用立即启动并仅保持横向模式。
【解决方案3】:

在您启动主窗口小部件时使用SystemChrome.setPreferredOrientations 设置横向或纵向模式。

         void main() { 
          runApp(MyApp()); 
          }

        class MyApp extends StatelessWidget {
          // This widget is the root of your application.
          @override
          Widget build(BuildContext context) {
            config();
            return MaterialApp(
              title: ...
            );
          }

         void config() {
            SystemChrome.setPreferredOrientations([
              DeviceOrientation.landscapeLeft,
              DeviceOrientation.landscapeRight
            ]);
          }

【讨论】:

  • 这对我有用。确保您正在调用 config() 方法,最好在您的 main 方法中。
【解决方案4】:

对于 Android,setPreferredOrientations 可以正常工作。

但是对于iOs,我们必须使用这个解决方案(手机和iPad):https://github.com/flutter/flutter/issues/13238#issuecomment-432981342

【讨论】:

    猜你喜欢
    • 2020-12-26
    • 1970-01-01
    • 1970-01-01
    • 2014-01-17
    • 2019-01-19
    • 2011-05-03
    • 1970-01-01
    • 2014-08-14
    • 1970-01-01
    相关资源
    最近更新 更多