简体   繁体   English

如何为 iOS 强制 flutter 设备方向

[英]How to force flutter device orientation for iOS

I wanna force the orientation of some pages of my application.我想强制我的应用程序的某些页面的方向。 Like, I want the orientation to be Portrait for the landing page, the options of my game, and the player selection, but I want to force the orientation to be landscape when the game begins... I tried using:就像,我希望登录页面的方向为纵向,我的游戏选项和玩家选择,但我想在游戏开始时强制方向为横向......我尝试使用:

super.initState();
  SystemChrome.setPreferredOrientations([
    DeviceOrientation.landscapeLeft,
    DeviceOrientation.landscapeRight,
])

and it does work on android, (except that it change the orientation 3 times before getting the right orientation...) But on iOS, it doesn't force the change... It only change when you turn the device and then it works and stays in landscape mode.它确实适用于 android,(除了它在获得正确方向之前改变方向 3 次......)但在 iOS 上,它不会强制改变......它只会在你转动设备然后它时改变工作并保持横向模式。

Does anyone had the same issue and know how to fix it?有没有人有同样的问题并且知道如何解决它?

I tried to put the SystemChrome.setPreferredOrientations in the Widget build function but it didn't worked.我试图将SystemChrome.setPreferredOrientations放入 Widget 构建 function 但它没有用。

Please try this.请试试这个。

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

2nd Way:第二种方式:

await SystemChrome.setPreferredOrientations([
    DeviceOrientation.landscapeLeft,
    DeviceOrientation.landscapeRight
  ]);

The list of supported orientations are:支持的方向列表是:

landscapeLeft
landscapeRight
portraitDown
portraitUp

Import package:flutter/services.dart , then导入package:flutter/services.dart ,然后

Put the SystemChrome.setPreferredOrientations inside the Widget build() method.SystemChrome.setPreferredOrientations放入 Widget build() 方法中。

Example:例子:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 React Native 中根据设备类型设置强制设备方向? - How to set force device orientation based on device type in React Native? 强制 Flutter Android 应用程序的行为就像在 iOS 设备上一样 - Force Flutter Android Application to behave like it is on an iOS Device 强制定位,同时保持设备方向更改? - Force orientation, while keep allowing device orientation changes? 如何在 Android 和 iOS 设备上颤动壁纸? - How to wallpaper on android and iOS device in flutter? 如何获得设备方向 - How to get device orientation Android:即使设备自动旋转屏幕关闭,如何强制反转肖像方向? - Android: How to force reverse portrait orientation even if the device auto-rotate screen is off? 在Flutter中获取设备方向(偏航,横摇和俯仰) - Getting device orientation (yaw, roll and pitch) in Flutter Flutter /相机插件0.5.3上的设备方向错误 - Device Orientation Incorrect on Flutter/Camera Plugin 0.5.3 Android(例如iOS)中是否有设备和界面方向? - Is there a device and interface orientation in Android (like in iOS)? Flutter:如何构建移动设备改变方向时移动块的逻辑? - Flutter: how to build the logic of moving blocks when changing the orientation of a mobile device?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM