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

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

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

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

我怎么解决这个问题呢?

谢谢


当前回答

setPreferredOrientation返回一个Future<void>,所以它是异步的。最易读的方法是将main定义为异步:

Future<void> main() async {
  await SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
  return runApp(new MyApp());
}

其他回答

颤振方向锁定:仅限纵向

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

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

希望这对你有帮助。

打开android/app/src/main/AndroidManifest.xml,在MainActivity中添加如下一行:

android:screenOrientation="portrait"

如果你有这个:

<activity
        android:name=".MainActivity"
        android:launchMode="singleTop"
        android:theme="@style/LaunchTheme"
        android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
        android:hardwareAccelerated="true"
        android:windowSoftInputMode="adjustResize">

你应该得到这样的结果:

<activity
        android:name=".MainActivity"
        android:launchMode="singleTop"
        android:theme="@style/LaunchTheme"
        android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
        android:hardwareAccelerated="true"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="adjustResize">

这适用于Android。在iOS上,你将不得不从Xcode页面更改:https://i.stack.imgur.com/hswoe.png(正如Hejazi所说)

如果你从xcode界面编辑这个,它不会在ipad上被改变。对于ipad部分,您必须编辑信息。Plist文件从android工作室。你可以在数组列表中看到“~ ipad”。在info中有两个部分可用。Plist,你必须为iPhone和ipad手动编辑它。

制作SystemChrome。setPreferredOrientations在iPad上工作,在Xcode项目编辑器中启用“要求全屏”,或者简单地在/ios/Runner/Info中添加以下行。Plist在你的项目文件夹。

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

只需在主程序中添加以下代码行。飞镖文件。

SystemChrome.setPreferredOrientations([
  DeviceOrientation.portraitUp,
]);

并且记得导入服务。飞镖文件。下面给出了一个例子!

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

void main() {
    runApp(MyApp());
}


class MyApp extends StatelessWidget {
     @override
     Widget build(BuildContext context) {

      SystemChrome.setPreferredOrientations([
        DeviceOrientation.portraitUp,
      ]);

      return MaterialApp(
        home: Scaffold(
          body: Center(child: Text("A Flutter Example!")),
     ),
   );
  }
}