首页 > 解决方案 > 在 Flutter Widget 测试中,如何将 media.orientation 设置为纵向?

问题描述

在构建方法中,MediaQuery.of(context).orientation等于Orientation.landscape。怎么弄成portrait

测试小部件包裹在MaterialApp.

标签: flutterui-testing

解决方案


包装查询方向的小部件

  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;
  }

推荐阅读