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

2

6

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

The test widget is wrap under MaterialApp.

Obsessive answered 3/5, 2018 at 15:8 Comment(1)
Related issue github.com/flutter/flutter/issues/10307De
C
4

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;
  }
Crispen answered 27/6, 2018 at 18:19 Comment(1)
See also #53707069De
V
0

By default app is in portrait mode

To set landscape orientation in your test you have to update you device physical size from the test.

  double landscapeHeight = any landscape height;
  double landscapeWidth = any landscape width;

  tester.view.physicalSize = Size(landscapeWidth, landscapeHeight);

After this your testing screen size would be the above one if you want to test in portrait mode then you have to reset this.

  tester.view.resetPhysicalSize();
Volteface answered 25/9 at 13:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.