gpt4 book ai didi

testing - 获取小部件在 Flutter 测试中的位置?

转载 作者:IT王子 更新时间:2023-10-29 06:59:32 27 4
gpt4 key购买 nike

如何在 Flutter 测试中获取 Widget 的位置?理想情况下是小部件的中心,但左上角的坐标也可以。

这是我对一个功能的测试和尝试,该功能可在屏幕上查找小部件的位置。

  testWidgets('Pin is in center of screen', (WidgetTester tester) async {
await _setUp(tester); // Does setup
final findPin = find.byKey('pin');
final pinLocation = _getLocation(findPin.evaluate().first);
print(ui.window.physicalSize);
expect(pinLocation.dx, 1200.0);
expect(pinLocation.dy, 900.0);
});

Offset _getLocation(Element element) {
return (element.findRenderObject() as RenderBox).localToGlobal(Offset.zero);
}

ui.window.physicalSize 的打印(其中 uidart:ui 包)告诉我大小是 尺寸(2400.0, 1800.0)。但是,当我在测试中设置这些中点时,我的测试失败了,说 Size 实际上是 395.0 和 305.0。我的小部件只有 30.0 x 30.0 大小,即使是 395 +/- 30 也不等于 1200 范围内的任何东西。

在我的测试中,然后我将我的小部件包装在一个预定义宽度 (400) 和高度 (300) 的容器中,这不是它在真实应用程序中的样子,但出于测试目的,它更接近 - 给出195和155的“中心”,当它应该是200和150时。

我正在测试的小部件(在代码中,而不是在我的 testWidgets 中使用额外的容器)大约是:

return Center(
child: Container(
key: Key('pin'),
height: 30.0,
width: 30.0,
color: Colors.pink,
),
);

最佳答案

WidgetTester 有您正在寻找的方法,例如:

tester.getCenter(findPin);

关于testing - 获取小部件在 Flutter 测试中的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53531169/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com