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

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

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”

其他回答

颤振方向锁定:仅限纵向

在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>

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

希望这对你有帮助。

@boeledi,如果你想“锁定”设备方向,不允许它随着用户旋转他们的手机而改变,这很容易设置如下:

// This did not work as requirement
void main() {
  SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
  runApp(new MyApp());
}

你必须等到setPreferredOrientations完成,然后 启动应用程序

// This will works always for lock screen Orientation.
void main() {
  WidgetsFlutterBinding.ensureInitialized();
  SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp])
    .then((_) {
      runApp(new MyApp());
    });
}

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

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

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

runApp(myApp());

下面是颤振队的官方例子。 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());
}

要在iPad中强制竖屏,你还必须修改关键的UISupportedInterfaceOrientations~ iPad in ios/Runner/Info.plist:

    <key>UISupportedInterfaceOrientations~ipad</key>
    <array>
            <string>UIInterfaceOrientationPortrait</string>
            <string>UIInterfaceOrientationPortraitUpsideDown</string>
    </array>

这也可以通过XCode实现。

以上将在本地工作。为了通过XCode上传到App Store,你还需要将以下内容添加到Info.plist。这也可以通过检查“目标->常规->部署信息”下的“要求全屏”来完成。

   <key>UIRequiresFullScreen</key>
   <true/>

否则你会在上传时得到一个错误。