What is the best way seperate the routing of different modules in my flutter app

Viewed 509

Right now Im using generated routing. I have a class that returns all my routes to the onGenerateRoute. I have several modules in my app, which i have seperated. Accessible modules depend on the type of user. Im trying to create a different routes file for each module and have access to them from the main routes file. I am using bloc state management. (my routes file is starting to get really cluttered.)

1 Answers

One way I've done it is via nested navigation and using multiple Navigator widgets, each of which manages a group of routes (this is if you're using Navigator 1.0).

For example; in the main routes object, you can have the routes to each module widget: User and Admin:

routes: {
   '/user': (context) => UserWidget(),
   '/admin': (context) => AdminWidget()
}

Then inside each of these widgets (which define your separate modules) you can have a nested Navigator widget that lays out the routes for each module (some "pseudo" code to get the idea):

class AdminWidget 
- Navigator
   - onGenerateRoute
     - '/admin_page1'
     - '/admin_page2'
     - '/admin_page3'
class UserWidget 
- Navigator
  - onGenerateRoute
    - '/user_page1'
    - '/user_page2'
    - '/user_page3'

That way you can group your routes and direct users to the corresponding routes based on their type or role. This is one way to do it.

Related