在构建方法中,
MediaQuery.of(context).orientation
等于Orientation.landscape
。如何将其变成portrait
.
测试小部件包裹在
MaterialApp
下。
包装查询方向的小部件
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;
}
默认应用程序处于纵向模式
要在测试中设置横向方向,您必须从测试中更新设备物理尺寸。
double landscapeHeight = any landscape height;
double landscapeWidth = any landscape width;
tester.view.physicalSize = Size(landscapeWidth, landscapeHeight);
在此之后,如果您想在纵向模式下进行测试,那么您的测试屏幕尺寸将是上面的尺寸,那么您必须重置它。
tester.view.resetPhysicalSize();