首页 文章

颤动 - 如果应用程序处于横向模式,则有条件地隐藏状态栏

提问于
浏览
0

Related question中,app可以隐藏状态栏

SystemChrome.setEnabledSystemUIOverlays([]);

但是,如果我使用媒体查询来检测主应用程序中的方向,则会导致应用程序出错,因为没有重要的祖先小部件 .

// app error
  if (MediaQuery.of(context).orientation == Orientation.landscape) {
    SystemChrome.setEnabledSystemUIOverlays([]);
  }

如果设备旋转到横向模式,有没有办法在Flutter中有条件地隐藏整个应用程序的状态栏?

1 回答

  • 0

    只有当您访问 MediaQuery 的窗口小部件的祖先是素材窗口小部件时,才能访问 MediaQuery 窗口小部件 .

    没有 MediaQuery 的更通用的解决方案就像:

    第一

    import "dart:ui" as ui;
    

    然后

    Size s = ui.window.physicalSize/ui.window.devicePixelRatio;
    bool landscape = s.width>s.height;
    
    if (landscape) {
      SystemChrome.setEnabledSystemUIOverlays([]);
    }
    

    要么


    使用Orientation构建器作为root . 在构建函数内部,

    new OrientationBuilder(
       builder: (BuildContext context, Orientation orientation){
                 if (orientation == Orientation.landscape) {
                   SystemChrome.setEnabledSystemUIOverlays([]);
                 } else {
                   SystemChrome.setEnabledSystemUIOverlays(SystemUiOverlay.values)
                 }
                 return new Scaffold(...
               }
     );
    

    希望有所帮助!

相关问题