我正在尝试编写颤振测试并模拟双选项卡。但我找不到办法。
这是我现在所做的:
void main() {
testWidgets('It should trigger onDoubleTap', (tester) async {
await tester.pumpWidget(MaterialApp(
home: GestureDetector(
child: const Text('button'),
onDoubleTap: () {
print('double tapped');
},
),
));
await tester.pumpAndSettle();
await tester.tap(find.text('button')); // <- Tried with tester.press too
await tester.tap(find.text('button')); // <- Tried with tester.press too
await tester.pumpAndSettle();
});
}
当我运行测试时,这是我得到的:
00:03 +1: All tests passed!
但我在控制台中没有看到任何 double tapped
。
如何触发双击?
答案 0 :(得分:2)
解决方案是在两次点击之间等待 kDoubleTapMinTime
。
void main() {
testWidgets('It should trigger onDoubleTap', (tester) async {
await tester.pumpWidget(MaterialApp(
home: GestureDetector(
child: const Text('button'),
onDoubleTap: () {
print('double tapped');
},
),
));
await tester.pumpAndSettle();
await tester.tap(find.text('button'));
await tester.pump(kDoubleTapMinTime); // <- Add this
await tester.tap(find.text('button'));
await tester.pumpAndSettle();
});
}
我在控制台中得到了 double tapped
:
double tapped
00:03 +1: All tests passed!