I'm Working on Project Where there is A screen For viewing the user Profile and another screen for Editing Profile. I'm using onGenerateRoute
Method for Routing and know I can parse an argument and send it over.
How I can use Call Back Function ValueChange
with onGenerateRoute
Method?
CodePudding user response:
Navigate to the EditingProfile Page and pass the Function as an argument:
Navigator.pushNamed(context, "/editingprofile", arguments: () {
print("Function called");
});
In the onGenerateRoute pass the argument to the EditingProfile either as constructor param and call the variable directly
Route<dynamic>? generateRoute(RouteSettings settings) {
final args = settings.arguments;
switch (settings.name) {
case "/editingprofile":
return MaterialPageRoute(
builder: (context) => EditingPage(settings.arguments as Function));
}
}
class EditingPage extends StatelessWidget {
Function callback;
SecondPage(this.callback, {Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
body: OutlinedButton(
onPressed: (){
callback.call();
},
child: Text("Press me"),
),
);
}
}
or pass it inside the MaterialPageRoute as settings param and get the function with ModalRoute
Route<dynamic>? generateRoute(RouteSettings settings) {
final args = settings.arguments;
switch (settings.name) {
case "/editingprofile":
return MaterialPageRoute(
settings: settings,
builder: (context) => EditingProfile());
}
}
class EditingPage extends StatelessWidget {
EditingPage ({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
Function callback = ModalRoute.of(context)!.settings.arguments as Function;
return Scaffold(
body: OutlinedButton(
onPressed: (){
callback.call();
},
child: Text("Press me"),
),
);
}
}