zubairehman / flutter_boilerplate_project

A boilerplate project created in flutter using MobX and Provider.
http://zubairehman.surge.sh/
MIT License
2.27k stars 910 forks source link

Router with dynamic param #27

Closed dohoangnam1103 closed 4 years ago

dohoangnam1103 commented 4 years ago

Can you give an example of sending param when switching router?

zubairehman commented 4 years ago

@dohoangnam1103 here you go:

Navigate to the widget

// A button that navigates to a named route. The named route
// extracts the arguments by itself.
RaisedButton(                                                   
  child: Text("Navigate to screen that extracts arguments"),    
  onPressed: () {                                               
    // When the user taps the button, navigate to a named route 
    // and provide the arguments as an optional parameter.      
    Navigator.pushNamed(                                        
      context,                                                  
      Routes.routeName,                         
      arguments: ScreenArguments(                               
        'Extract Arguments Screen',                              
        'This message is extracted in the build method.',       
      ),                                                                                                                 
    );                                                          
  },                                                            
),      

Extract the arguments

@override
  Widget build(BuildContext context) {
    // Extract the arguments from the current ModalRoute settings and cast
    // them as ScreenArguments.
    final ScreenArguments args = ModalRoute.of(context).settings.arguments;

    return Scaffold(
      appBar: AppBar(
        title: Text(args.title),
      ),
      body: Center(
        child: Text(args.message),
      ),
    );
  }

For more details, please head over to: https://flutter.dev/docs/cookbook/navigation/navigate-with-arguments