如何在 Flutter 中将我的应用程序设置为横向模式?

How to set my app to landscape mode in Flutter?

我想将我的应用程序的默认方向设置为横向,就像我打开部落冲突或移动传奇等游戏应用程序时一样。我怎样才能做到这一点?

您必须单独配置 iOS 和 Android。

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

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

启动主窗口小部件时使用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
            ]);
          }

将此代码放入 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

对于 Android,setPreferredOrientations 工作正常。

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