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:

  • I’m using go_router for routing in my Flutter app.

  • I need to show an AlertDialog when the user tries to navigate back from a specific route, but PopScope doesn’t seem to work as intended.

  • I want to control the behavior when the back button is pressed and either allow or prevent the navigation based on user confirmation.

Code Example:

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

dart

Kodu kopyala

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:

  • The PopScope widget is supposed to intercept the back navigation and display an AlertDialog, but it doesn’t seem to work as expected.

  • The AlertDialog does not appear when the back button is pressed, and the navigation behavior is not controlled.

What I’ve Tried:

  • Ensured that PopScope is correctly used within the Navigator widget.

  • Verified that GoRouter is properly set up and that the route configuration is correct.

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:

  • Implemented PopScope to handle back navigation within the GoRouter setup.

  • Used PopScope to show an AlertDialog asking the user for confirmation when they attempt to navigate back.

  • Configured the onPopInvoked callback in PopScope to display the AlertDialog and determine if navigation should proceed.

What I Expected:

  • When the back button is pressed, PopScope should intercept the event and show the AlertDialog.

  • The AlertDialog should give the user the option to either confirm or cancel the navigation.

  • If the user confirms, navigation should proceed. If the user cancels, the navigation should be blocked.

What Actually Happened:

  • The AlertDialog does not appear when the back button is pressed.

  • Navigation occurs immediately without showing the dialog, ignoring the PopScope configuration.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật