【问题标题】:Flutter. Changing screen to landscape, it automatically turns back to portrait扑。将屏幕更改为横向,它会自动变回纵向
【发布时间】:2020-01-29 00:27:47
【问题描述】:

在主菜单屏幕上,我调用forcePortrait(),即:

static Future forcePortrait() async {
await SystemChrome.setPreferredOrientations([
  DeviceOrientation.portraitUp,
  DeviceOrientation.portraitDown,
]);


 }

然后...我从主菜单屏幕中调用以下内容:

Navigator.push(context, MaterialPageRoute(builder: (context) => ScreenBoard()));

在董事会屏幕中,我拨打UScreen.forceLandscape()如下

class ScreenBoard extends StatefulWidget {
 @override
 _ScreenBoardState createState() => _ScreenBoardState();
}

class _ScreenBoardState extends State<ScreenBoard> {
  @override
  Widget build(BuildContext context) {
    UScreen.forceLandscape();

    return Scaffold(
      backgroundColor: AppColors.redBckgnd,
      body: Container(

      ),
    );
  }
}

这是forceLandscape() method

static void forceLandscape() async {
await SystemChrome.setPreferredOrientations([
  DeviceOrientation.landscapeLeft,
      DeviceOrientation.landscapeRight,
    ]);
  }

我的问题是它确实移动到了新屏幕并且它确实将屏幕旋转为横向,但几秒钟后,它又将它移回纵向。这是为什么 ?!我想不通!

编辑

我注意到,如果我不在菜单屏幕上调用forcePortrait(),板屏幕不会切换回纵向。但还是不知道为什么!

干杯!

【问题讨论】:

    标签: flutter flutter-layout


    【解决方案1】:

    哇。我想我想通了。 forcePortraitforceLandscape 方法是从 build() 方法内部调用的。显然,在推到 Board 屏幕后,主菜单屏幕又运行了一次 build() 方法,它在 Board 屏幕之后执行了此操作,因此在 forceLandscape 之后调用了 forcePortrait

    解决方案是将它们移至只调用一次的initState 方法。我现在遇到另一个问题,当单击返回并返回菜单屏幕时,它不会调用initState 并且屏幕仍然是纵向的,但这是以后的工作。

    无论如何,有趣的机制,我不知道发生了这种情况。

    免责声明:我只使用 Flutter 2 周

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-03
      • 1970-01-01
      • 2015-12-30
      • 1970-01-01
      相关资源
      最近更新 更多