如何为 flutter 应用程序设置横向模式?

How to set landscape orientation mode for flutter app?

我一直在寻找可以强制设置我的 Flutter 应用景观方向的代码。

强制启用

导入包:main.dart 文件中的 import 'package:flutter/services.dart';

1.横向模式:

// Set landscape orientation
SystemChrome.setPreferredOrientations([
  DeviceOrientation.landscapeLeft,
  DeviceOrientation.landscapeRight,
]);

2。纵向模式:

// Set portrait orientation
SystemChrome.setPreferredOrientations([
   DeviceOrientation.portraitDown,
   DeviceOrientation.portraitUp,
]);

import 'package:flutter/services.dart';

void main()  {
   WidgetsFlutterBinding.ensureInitialized();
   SystemChrome.setPreferredOrientations([DeviceOrientation.landscapeLeft])
   .then((_) {
    runApp(new MyApp());
  });
}

SystemChrome.setPreferredOrientations 适用于应用程序的 Flutter 部分,但它不是完整的解决方案。因为当您启动应用程序时 - Flutter 尚未创建。所以你也应该在原生部分设置方向。

这里有详细说明的文章https://medium.com/@greymag/flutter-orientation-lock-portrait-only-c98910ebd769