首页 > 解决方案 > 如何在颤振测试中模拟 onDoubleTap

问题描述

我正在尝试编写颤振测试并模拟双标签。但我无法设法找到方法。

这是我现在所做的:

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在控制台中看不到任何内容。


如何触发双击?

标签: flutterdartflutter-testgesturedetectorsingle-vs-double-tap

解决方案


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!

推荐阅读