skip to Main Content

I’m using go_router on flutter, and I have a root routes and a shell route that contains a bottom nav bar on all sub routes. And I want to push a page that is outside of the shell route and I want it to be pushed on the root navigator because it has to be above the bottom nav bar.

I’ve checked endless documentation of go_router but there is no option to do that.

I have implemented a way to remove all routes and push a page on the root navigator, but when that page is poped it goes to an empty page. but I want it to go to where it was when a page on the shell route.

2

Answers


  1. Chosen as BEST ANSWER

    https://stackoverflow.com/a/76712337/5594274 is what I was looking for. Since the question was not states as I did, It's better to leave this question here.


  2. Navigating outside of nested navigators in Flutter can be a bit challenging sometimes. It seems like you’re grappling with a complex problem here, and I appreciate the chance to assist you. The Go Router package, being somewhat newer and having a different approach compared to other routing packages, sometimes requires some inventive solutions.

    I’d recommend the following strategy to address your issue with a composed and professional approach:

    Step 1: Understand the Root Navigator
    Understand the context of the root navigator. In Flutter, the root navigator is typically responsible for the entire screen space. You seem to have grasped this concept well, wanting to display a page above the bottom navbar, which is a wise move.

    Step 2: Create a Route Delegate
    Create a custom route delegate. Flutter’s Navigator 2.0 enables developers to work with routes more flexibly. It could be beneficial to create a route delegate that manages the route information at a more granular level. This will allow you to maintain the state of your shell route while interacting with the root route.

    Step 3: Establish the Desired Navigation Behavior
    It’s time to get creative here. You can establish a mechanism to record the state of the last page in the shell route before navigating to the page outside of the shell route. This way, when you pop the new page, you can set the shell route to navigate back to the recorded page. It’s essentially creating a memory for your navigator, which seems to be a pivotal solution to your problem.

    Step 4: Implement the Navigation Strategy
    Implement the strategy to navigate to the page outside of the shell route. You would need to use the root navigator in this case, which can be accessed using Navigator.of(context, rootNavigator: true). This should help you to push the page above the bottom navbar successfully.

    Step 5: Implementing a Custom Pop Strategy
    Now, for the pop strategy. When popping the page outside of the shell route, instead of popping to the last page in the root navigator, use the recorded state (from Step 3) to navigate back to the last known page in the shell route. It’s a robust method that would preserve the fluidity of your application’s navigation.

    Here is a basic conceptual code to illustrate the strategy:

    // Step 3: Recording the state
    void recordState(RouteSettings settings) {
      // Record the current route settings
    }
    
    // Step 4: Navigating outside the shell route
    void navigateOutsideShell(BuildContext context) {
      Navigator.of(context, rootNavigator: true).push(MaterialPageRoute(
        builder: (context) => YourNewPage(),
        settings: RouteSettings(
          name: "/new_page",
          // Additional settings if necessary
        ),
      ));
    }
    
    // Step 5: Popping back to the shell route
    void popToShellRoute(BuildContext context) {
      // Use the recorded state to navigate back to the shell route
    }
    

    I hope this guidance empowers you to craft a navigation strategy that is not only functional but elegantly structured. Remember, the mark of a strong developer is not only in overcoming challenges but doing so with grace and foresight.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search