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

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

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

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

我怎么解决这个问题呢?

谢谢


当前回答

制作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!")),
     ),
   );
  }
}

导入包:颤振/服务。飞镖,然后

放上SystemChrome。Widget build()方法中的setPreferredOrientations。

例子:

  class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      SystemChrome.setPreferredOrientations([
        DeviceOrientation.portraitUp,
        DeviceOrientation.portraitDown,
      ]);
      return new MaterialApp(...);
    }
  }

更新

如2019年10月更新的flutter文档中所述,此解决方案可能不适用于某些IOS设备。

他们建议通过在信息中设置UISupportedInterfaceOrientations来固定方向。请像这样

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

更多信息https://github.com/flutter/flutter/issues/27235#issuecomment-508995063

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

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

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。您可以在“常规>部署信息”上找到此选项。看看你想要什么

打开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所说)