Flutter Widget测试中,如何将media.orientation设置为纵向?

问题描述 投票:0回答:2

在构建方法中,

MediaQuery.of(context).orientation
等于
Orientation.landscape
。如何将其变成
portrait
.

测试小部件包裹在

MaterialApp
下。

flutter ui-testing
2个回答
4
投票

包装查询方向的小部件

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

为我工作。

MediaQuery.orientation
只是检查哪个尺寸更大

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

0
投票

默认应用程序处于纵向模式

要在测试中设置横向方向,您必须从测试中更新设备物理尺寸。

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

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

在此之后,如果您想在纵向模式下进行测试,那么您的测试屏幕尺寸将是上面的尺寸,那么您必须重置它。

  tester.view.resetPhysicalSize();
© www.soinside.com 2019 - 2024. All rights reserved.