Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to simulate onDoubleTap in flutter test

I'm trying to write a flutter test and simulate a double tab. But I cannot manage to find a way.

Here is what I have done for now:

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();
  });
}

When I run the test, this is was I get:

00:03 +1: All tests passed!                                                                                              

But I don't see any double tapped in the console.


How can I trigger the double-tap?

like image 987
Valentin Vignal Avatar asked Oct 20 '25 17:10

Valentin Vignal


1 Answers

The solution is to wait kDoubleTapMinTime between both taps.

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();
  });
}

I get the double tapped in the console:

double tapped
00:03 +1: All tests passed!
like image 192
Valentin Vignal Avatar answered Oct 23 '25 08:10

Valentin Vignal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!