Search code examples
flutterrouternavigator

How to close the specified page in flutter


Open the app, the page order is A->B->C, I want to close the B page on the C page, and the page order is A->C. As far as I know, Navigator.removeRoute(context, routerC) can be used, but I don't know how to get routerC.

Can you tell me how to do it? thanks


Solution

  • route list class

      class AppRoutes {
      static String get A=> "/a";
      static String get B=> "/b";
      static String get C=> "/c";
      static String get K=> "/k";
    

    write remove route function as below

    clearRouteAndOpen(BuildContext context, String name,
        {Object? arguments, String? removeUntil}) {
      Navigator.pushNamedAndRemoveUntil(
          context, name, ModalRoute.withName(removeUntil ?? name),
          arguments: arguments);
    }
    

    call removeRoute function as below when back press in toolbar or any action you can remove B easily from C. call below function from C screen. Suppose A start from K screen. just remove until K.

     clearRouteAndOpen(
                              context,
                              AppRoutes.A,
                              removeUntil: AppRoutes.K,
                            );