0

In Related question, app can hide the status bar with

  SystemChrome.setEnabledSystemUIOverlays([]);

But if I surround that with a media query to detect orientation in the main app, it causes the app to error because there is no material ancestor widget.

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

Is there a way to conditionally hide the status bar in Flutter for the entire app, if the device rotates to landscape mode?

Deborah
  • 4,316
  • 8
  • 31
  • 45

1 Answers1

0

MediaQuery widget can be accessed only when the ancestor of the widget that you access MediaQuery from is a material widget.

A more generic solution without MediaQuery would be like:

First

import "dart:ui" as ui;

then

Size s = ui.window.physicalSize/ui.window.devicePixelRatio;
bool landscape = s.width>s.height;

if (landscape) {
  SystemChrome.setEnabledSystemUIOverlays([]);
}

Or


Using Orientation builder as root. Inside the build function,

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

Hope that helped!

Hemanth Raj
  • 32,555
  • 10
  • 92
  • 82
  • This did not return the status bar on rotate back to portrait. It seems to be called only once. Worse, apparently "SystemChrome.setEnabledSystemUIOverlays([]);" also removes the Android bottom button bar, which also stay removed. – Deborah Feb 02 '18 at 21:28
  • Yes, call these statements inside a function that listens to orientation changes – Hemanth Raj Feb 03 '18 at 02:53
  • Or consider using [`OrientationBuilder`](https://docs.flutter.io/flutter/widgets/OrientationBuilder-class.html) as root. I've changed my answer to implement the same. – Hemanth Raj Feb 03 '18 at 03:06
  • @Hemanth Why does the app not able to take up the entire screen after hiding the system bars? – CopsOnRoad Oct 28 '18 at 14:40