Flutter testing WillPopScope with back button


On my Home widget, when user taps system back button, It shows by a WillPopScope a confirmation dialog widget.
I want to test this dialog but I cant figure it out how to press the back button on a test file.


I had the same problem but i had no back button in my app. I wanted to test the android system back button. This is how I did it. Maybe it’s helpful for people who run into the same problem as me.

testWidgets("test onWillPop",(WidgetTester tester) async {
    bool willPopCalled = false;
    await tester.pumpWidget(
        home: Scaffold(
          body: WillPopScope(
            onWillPop: () async {
              willPopCalled = true;
              return false;
            child: Container(),

    final dynamic widgetsAppState = tester.state(find.byType(WidgetsApp));
    await widgetsAppState.didPopRoute();
    await tester.pump();

    expect(willPopCalled, true);

Inspired by: https://github.com/flutter/flutter/blob/master/packages/flutter/test/material/will_pop_test.dart

Answered By – Stan

Answer Checked By – Terry (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *