如何在颤振测试中模拟 onDoubleTap

时间:2021-04-29 05:42:26

标签: flutter dart flutter-test gesturedetector single-vs-double-tap

我正在尝试编写颤振测试并模拟双选项卡。但我找不到办法。

这是我现在所做的:

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


如何触发双击?

1 个答案:

答案 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!