如何在Flutter测试中模拟onDoubleTap事件

4

我想编写一个Flutter测试并模拟双击,但是我无法找到方法。

目前为止,我已经做了以下工作:

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个回答

7
解决方法是在两次轻拍之间等待 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!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接