如何在flutter测试中模拟onDoubleTap
How to simulate onDoubleTap in flutter test
我正在尝试编写颤动测试并模拟双标签。但是我找不到办法。
这是我目前所做的:
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
。
如何触发双击?
解决方案是在两次点击之间等待 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!
我正在尝试编写颤动测试并模拟双标签。但是我找不到办法。
这是我目前所做的:
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
。
如何触发双击?
解决方案是在两次点击之间等待 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!