[英]how to use call back function with onGenerateRoute
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.我正在使用
onGenerateRoute
方法进行路由,并且知道我可以解析参数并将其发送过来。
How I can use Call Back Function ValueChange
with onGenerateRoute
Method?如何通过
onGenerateRoute
方法使用 Call Back Function ValueChange
?
Navigate to the EditingProfile Page and pass the Function as an argument:导航到 EditingProfile 页面并将 Function 作为参数传递:
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在 onGenerateRoute 中将参数作为构造函数参数传递给 EditingProfile 并直接调用变量
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或将其作为设置参数传递到 MaterialPageRoute 中,并使用 ModalRoute 获取 function
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"),
),
);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.