Milad-Akarie / auto_route_library

Flutter route generator
MIT License
1.59k stars 405 forks source link

Passing argument to page nested #719

Closed magician20 closed 2 years ago

magician20 commented 3 years ago

My question how to pass an argument (not paramter that will appear on the url ) from HomePage Class that use AutoTabsScaffold to the BooksPage Class.

@MaterialAutoRouter(
    replaceInRouteName: 'Page,Route',
    AutoRoute(
     path: "/home",
     page: HomePage, 
     children: [
      RedirectRoute(path: '', redirectTo: 'books'),
       AutoRoute(
            path: "/books",
            name: "BooksRouter",
            page: EmptyRouterPage,  // <----  chanage to Wrapping Routes Class ??
            children: [
                AutoRoute(path: '', page: BooksPage),
                AutoRoute(path: ':bookId', page: BookDetailsPage),
                RedirectRoute(path: '*', redirectTo: ''),
            ],
        ),
  AutoRoute(path: 'settings', page: SettingsPage),
],
)
class $AppRouter {}
Milad-Akarie commented 3 years ago

@magician20 yes, change EmptyRouterPage to your own widget that accepts the needed args.

magician20 commented 3 years ago

@Milad-Akarie Sorry for my bad question but what about accessing BookDetailsPage? As you can see below the Wrapping Class code, I can open BooksPage but then I can't open BookDetailsPage. I don't know how to make AutoRouter differentiate the two routes/paths.

///I'm trying to pass scaffoldKey to BooksPage
  @override
  Widget build(BuildContext context) {
    // AutoRouter is required to render sub-routes
    return AutoRouter(builder: (context, content) => BooksPage(scaffoldKey: scaffoldKey),); 
  }

Thanks for reply.

Milad-Akarie commented 3 years ago

@magician20 the content coming from router is the rendered stack and you're replacing it with just one page. just remove the builder from AutoRouter and just return the AutoRouter() widget as is

magician20 commented 3 years ago

@Milad-Akarie Thanks for reply I did as you mention and it work (can access detail page for each book) but, I got two problem

1-scaffoldKey object never passed down to BooksPage so I wasn't able to open or close drawer. 2-Mobile app lose BooksPage state when I navigate to different page by drawer ,despite work fine on web, so how can WrapperRouterPage can pass the argument to BooksPage ?

class WrapperRouterPage extends StatelessWidget { ///this key handle drawer state open/close final GlobalKey? scaffoldKey; const NotesWrapperPage({Key? key, this.scaffoldKey}): super(key: key);

@override Widget build(BuildContext context) { // AutoRouter is required to render sub-routes return AutoRouter(/builder: (context, content) => BooksPage(scaffoldKey: scaffoldKey),/); } }

AutoTabsScaffold( //scaffoldKey: _scaffoldKey,//not working routes: [ BooksRouter(scaffoldKey: _scaffoldKey), ArchiveRoute(), SettingsRoute(), AboutRoute() ],

wferem1 commented 3 years ago

Hi, I think you have to push a BooksRouter with a BooksRoute as its child.

context.push(
  BooksRouter(
    children:[
      BooksRoute(
        scaffoldKey: scaffoldKey,
      )
    ]
  )
);
magician20 commented 2 years ago

@wferem1 I don't think I can use that with AutoTabsScaffold

magician20 commented 2 years ago

@Milad-Akarie @wferem1 Any help here with AutoTabsScaffold & Drawer open & Close. Problem how sub tree widget like BooksPage can accesses the drawer global key of HomePage top widget ?

magician20 commented 2 years ago

@Milad-Akarie I solved By using state management (ex: Cubit) so i can be able to pass scaffoldKey to sub widget inside the nested route of Books. (is that good way ? or maybe if we have a away to be able to pass value to constructor of EmptyRouterPage and below widget can access them)