skip to Main Content

Is there a way to have several paths pointing to the same page?

To give some context: I have a flutter app that handles screens via Bloc events. The Home HomeScreen can have multiple events, each showing a specific page. Hence the need to have multiple paths pointing to the same screen.

The following is an example of how I have tried to set up routes. Build it doesn’t work.

@MaterialAutoRouter(
  routes: <AutoRoute>[
    AutoRoute(path: '/routeA', page: HomeScreen),
    AutoRoute(path: '/routeB', page: HomeScreen),
    AutoRoute(path: '/routeC', page: HomeScreen),
    AutoRoute(path: '/routeD', page: HomeScreen),
  ]
)

2

Answers


  1. HomeScreen can have multiple events, each showing a specific page. Hence the need to have multiple paths pointing to the same screen.

    If homepage has multiple events, handle what the users sees in the home page

    eg

    event 1
      show user picture of a cat
    event 2
      show user picture of a dog.
    
    Login or Signup to reply.
  2. My solution to this is creating different classes that point to the same widget.

    So in your case, if you have the class 'HomeScreen' you, want to create classes that point to it.

     class HomeScreenRouteTwo extends StatelessWidget {
      const HomeScreenRouteTwo({
        Key? key,
      }) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return HomeScreen('specificData');
      }
    }
    
     class HomeScreenRouteThree extends StatelessWidget {
      const HomeScreenRouteThree({
        Key? key,
      }) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return HomeScreen('specificData');
      }
    }
    

    And in your router

    @MaterialAutoRouter(
     routes: <AutoRoute>[
      AutoRoute(path: 'routeA', page: HomeScreenRouteTwo),
      AutoRoute(path: 'routeB', page: HomeScreenRouteThree),
       ... etc
      ]
    )
    

    This will do the same effect with different paths that point to the same widget you’ve built, but makes you able to send in different information based on the path you send the user to.

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