Search code examples
flutterdartflutter-testgesturedetectorsingle-vs-double-tap

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?


Solution

  • 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!