androidiosflutterwidgetflutter-go-router

Flutter GoRouter PopScope Widget Not Working


I'm working on a Flutter application using go_router for navigation and PopScope to handle back navigation. I'm experiencing issues with PopScope not behaving as expected.

Context:

Code Example:

Here is the relevant code snippet where I configure PopScope and GoRouter:

import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

class AppNavigation {
  AppNavigation._();

  static final rootNavigatorKey = GlobalKey<NavigatorState>();

  static List<RouteBase> routes = [
    GoRoute(
      path: "/home",
      name: "Home",
      builder: (BuildContext context, GoRouterState state) {
        return HomePage();
      },
    ),
    // Other routes...
  ];

  static final GoRouter router = GoRouter(
    navigatorKey: rootNavigatorKey,
    routes: routes,
  );
}

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Home')),
      body: PopScope(
        onPopInvoked: () async {
          final shouldExit = await showDialog(
            context: context,
            builder: (context) => AlertDialog(
              title: Text('Confirmation'),
              content: Text('Do you want to exit?'),
              actions: [
                TextButton(
                  onPressed: () => Navigator.of(context).pop(false),
                  child: Text('No'),
                ),
                TextButton(
                  onPressed: () => Navigator.of(context).pop(true),
                  child: Text('Yes'),
                ),
              ],
            ),
          );
          return shouldExit;
        },
        child: Center(child: Text('Home Page Content')),
      ),
    );
  }
}

Issue:

What I’ve Tried:

Question:

How can I make sure that PopScope works properly with go_router? Is there a known issue or workaround for using PopScope with go_router to control back navigation?

Thank you for any help or insights!

What I Tried:

What I Expected:

What Actually Happened:


Solution

  •  void _onBackPressed({required BuildContext context}) {
          WidgetsBinding.instance.addPostFrameCallback((_) {
            if (mounted) {
              showDialog(
                context: context,
                builder: (context) {
                  return AlertDialog(
                    title: const Text('Are you sure?'),
                    content: const Text('Do you want to end the call?'),
                    actions: [
                      TextButton(
                        onPressed: () {
                          Navigator.pop(context);
                        },
                        child: const Text('No'),
                      ),
                      TextButton(
                        onPressed: () {
                          Navigator.pop(context);
                          Navigator.pop(context);
                          openCallEndReviewBottomSheet(context, navigationProvider);
                        },
                        child: const Text('Yes'),
                      ),
                    ],
                  );
                },
              );
            }
          });
        }
    
        return BackButtonListener(
          onBackButtonPressed: () async {
            _onBackPressed(context: context);
            return true;
          },
    /// ...  My Solution