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

Ash*_*iya 18 orientation flutter flutter-layout

我正在寻找可以有力地设置我的颤振应用程序景观方向的代码。

Ash*_*iya 28

强行启用

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

1.横向模式:

// Set landscape orientation
SystemChrome.setPreferredOrientations([
  DeviceOrientation.landscapeLeft,
  DeviceOrientation.landscapeRight,
]);
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

2. 人像模式:

// Set portrait orientation
SystemChrome.setPreferredOrientations([
   DeviceOrientation.portraitDown,
   DeviceOrientation.portraitUp,
]);
Run Code Online (Sandbox Code Playgroud)

设置人像模式


小智 9

import 'package:flutter/services.dart';

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


小智 5

SystemChrome.setPreferredOrientations适用于应用程序的 Flutter 部分,但它不是完整的解决方案。因为当您启动应用程序时 - Flutter 尚未创建。因此,您还应该在本机部件中设置方向。

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