如何在颤振测试中获得小部件的位置?理想情况下是小部件的中心,但左上角的坐标也可以。
这是我对一个函数的测试和尝试,该函数可以在屏幕上找到小部件的位置。
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
(在哪里
ui
是吗
dart:ui
包装)告诉我尺寸是
Size(2400.0, 1800.0)
.然而,当我在测试中设置这些中点时,我的测试失败了,说大小实际上是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,
),
);