【问题标题】:How to force flutter device orientation for iOS如何为 iOS 强制颤动设备方向
【发布时间】:2020-02-08 23:43:05
【问题描述】:

我想强制我的应用程序的某些页面的方向。就像,我希望着陆页、游戏选项和玩家选择的方向为纵向,但我想在游戏开始时强制方向为横向...我尝试使用:

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

它确实适用于android,(除了它在获得正确方向之前改变方向3次......)但在iOS上,它不会强制改变......它只会在你转动设备时改变然后它工作并保持在横向模式。

有没有人遇到过同样的问题并知道如何解决?

我尝试将 SystemChrome.setPreferredOrientations 放入 Widget 构建函数中,但没有成功。

【问题讨论】:

标签: android ios flutter dart device-orientation


【解决方案1】:

请试试这个。

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

第二种方式:

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

支持的方向列表是:

landscapeLeft
landscapeRight
portraitDown
portraitUp

【讨论】:

  • 不起作用...它不会转动视图,它只是在您物理更改手机的方向时起作用。即使手机是纵向的,我也想这样做
【解决方案2】:

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

SystemChrome.setPreferredOrientations 放入Widget build() 方法中。

例子:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-19
    • 2019-05-18
    • 2017-08-25
    • 1970-01-01
    • 2015-03-24
    • 1970-01-01
    • 2016-07-24
    • 1970-01-01
    相关资源
    最近更新 更多