我想阻止我的应用程序改变其方向,并迫使布局坚持“纵向”。

大体上是这样。省道,我放:

void main(){
  SystemChrome.setPreferredOrientations([
    DeviceOrientation.portraitUp,
    DeviceOrientation.portraitDown
  ]);
  runApp(new MyApp());
}

但当我使用Android模拟器旋转按钮时,布局“遵循”新的设备方向…

我怎么解决这个问题呢?

谢谢


当前回答

首先主要导入这一点。飞镖文件

import 'package:flutter/services.dart';

然后不要复制粘贴,而是看(记住)和写下面的代码在主要。飞镖文件

强制进入纵向模式:

void main() {
  SystemChrome.setPreferredOrientations(
      [DeviceOrientation.portraitUp,DeviceOrientation.portraitDown])
      .then((_) => runApp(MyApp()),
  );

在横屏模式下强制:

   void main() {
      SystemChrome.setPreferredOrientations(
          [DeviceOrientation.landscapeLeft,DeviceOrientation.landscapeRight])
          .then((_) => runApp(MyApp()),
      );

其他回答

首先主要导入这一点。飞镖文件

import 'package:flutter/services.dart';

然后不要复制粘贴,而是看(记住)和写下面的代码在主要。飞镖文件

强制进入纵向模式:

void main() {
  SystemChrome.setPreferredOrientations(
      [DeviceOrientation.portraitUp,DeviceOrientation.portraitDown])
      .then((_) => runApp(MyApp()),
  );

在横屏模式下强制:

   void main() {
      SystemChrome.setPreferredOrientations(
          [DeviceOrientation.landscapeLeft,DeviceOrientation.landscapeRight])
          .then((_) => runApp(MyApp()),
      );

iOS:

调用SystemChrome.setPreferredOrientations()对我来说不起作用,我必须在Xcode项目中更改设备方向,如下所示:

Android:

在文件android/app/src/main/AndroidManifest.xml中设置主活动的screenOrientation属性为portrait,如下所示:

在iOS情况下,可以启用“要求全屏”选项。

void main() async {
      WidgetsFlutterBinding.ensureInitialized();
      SystemChrome.setPreferredOrientations([
        DeviceOrientation.landscapeRight,
        DeviceOrientation.landscapeLeft,
      ]).then((_) {
        runApp(MediaQuery(
            data: MediaQueryData(),
            child:
                MaterialApp(debugShowCheckedModeBanner: false, home: LoginPage())));
      });
    }

导入导入“包:flutter/services.dart”;

然后在main中包含下面这行代码。Dart文件,并在你的main方法中像这样:

WidgetsFlutterBinding.ensureInitialized();
  SystemChrome.setPreferredOrientations([
    DeviceOrientation.portraitDown,
    DeviceOrientation.portraitUp,
  ]);

runApp(myApp());

setPreferredOrientations方法返回一个Future对象。 对于每个文档,Future表示将来某个地方可用的某个值。这就是为什么您应该等到它可用后再继续应用程序的原因。因此,有应使用'then'方法,根据定义,它“注册当Future完成时要调用的回调”。因此,您将使用以下代码:

  void main() {
  SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]).then((_) {
      runApp(new App());
    });
   }

同时,还需要导入以下文件:

“包:颤振/ services.dart”