flutterui-testing

In Flutter Widget testing, how to make media.orientation to portrait?


In build method, MediaQuery.of(context).orientation equals Orientation.landscape. How to make it into portrait.

The test widget is wrap under MaterialApp.


Solution

  • Wrapping the widgets that query orientation in

      MediaQuery(
        data: MediaQueryData
            .fromWindow(ui.window)
            .copyWith(size: const Size(600.0, 800.0)),
        child: widgetToTest,
      )
    

    worked for me.

    MediaQuery.orientation just checks what dimension is bigger

      Orientation get orientation {
        return size.width > size.height ? Orientation.landscape : Orientation.portrait;
      }