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

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

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

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

我怎么解决这个问题呢?

谢谢


当前回答

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

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

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

“包:颤振/ services.dart”

其他回答

iOS:

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

Android:

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

Try

 void main() async {
      WidgetsFlutterBinding.ensureInitialized();
      await SystemChrome.setPreferredOrientations(
          [DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]); 
    
      runApp(MyApp());
 }

你也可以在android manifest和ios info中更改屏幕方向设置。plist文件。

下面是颤振队的官方例子。 https://github.com/flutter/samples/blob/master/veggieseasons/lib/main.dart

import 'package:flutter/services.dart' show DeviceOrientation, SystemChrome;

void main() {
    WidgetsFlutterBinding.ensureInitialized();
    SystemChrome.setPreferredOrientations([
        DeviceOrientation.portraitUp,
        DeviceOrientation.portraitDown,
    ]);
    runApp(HomeScreen());
}

android有两个选项

1. 写在主

    main() {
      WidgetsFlutterBinding.ensureInitialized();
      SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp])
          .then((_) {
        runApp(
          child: MyApp(),
        );
      });
}

2. 在AndroidManifest.xml文件中设置

iOS上也有两种选择

1. 从info.plist

将这一行添加到您的信息中。plist文件

<array>
    <string>UIInterfaceOrientationPortrait</string>
</array>

2. 从跑步者 打开你的奔跑者。Xcode的iOS文件夹中的xcworkspace。点击Runner而不是Pods。您可以在“常规>部署信息”上找到此选项。看看你想要什么

颤振方向锁定:仅限纵向

在Flutter中,我们有SystemChrome.setPreferredOrientation()(参见文档)来定义首选方向。您可以在所有关于Flutter中设置方向的文章中找到这种方法。让我们看看如何使用它:

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() {
  // We need to call it manually,
  // because we going to call setPreferredOrientations()
  // before the runApp() call
  WidgetsFlutterBinding.ensureInitialized();
  
  // Than we setup preferred orientations,
  // and only after it finished we run our app
  SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp])
      .then((value) => runApp(MyApp()));
}

iOS

在Xcode (ios/Runner.xcworkspace)中打开项目,在项目导航器中选择运行器,选择目标运行器,在“常规”选项卡的“部署信息”部分,我们可以设置设备方向:

同样,我们也可以在不打开Xcode的情况下手动完成——只需编辑ios/Runner/Info.plist。将其作为文本文件打开,找到键UISupportedInterfaceOrientation,只留下所需的值:

<key>UISupportedInterfaceOrientations</key>
<array>
    <string>UIInterfaceOrientationPortrait</string>
</array>

安卓

为了定义Android的方向,我们需要编辑ApplicationManifest。打开android/app/src/main/AndroidManifest.xml,为主活动添加属性screenOrientation:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.orientation_lock_example">
    <application ...>
        <activity
            android:name=".MainActivity"
            android:screenOrientation="portrait"
            ...
            />
            ... 
        </activity>
        ... 
    </application>
</manifest>

就这些。下面是一个示例应用程序的存储库:

希望这对你有帮助。