简体   繁体   English

如何使用 onGenerateRoute 回调 function

[英]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.

相关问题 如何在 onGenerateRoute 中使用 provider? - How use provider in onGenerateRoute? 调用生成路由 function 没有 arguments - call onGenerateRoute function without arguments 在构建函数运行之前,如何使用 onGenerateRoute 在 Dart 中接收数据? - How can I receive data in Dart with onGenerateRoute bevor the build function runs? 为什么在 Flutter 中使用 onGenerateRoute() 和 ModalRoute.of()? - Why use onGenerateRoute() vs ModalRoute.of() in Flutter? 如何使用 onGenerateRoute 将 BottomNavigationBar 持久化到屏幕上? - How to persist a BottomNavigationBar across to screens using onGenerateRoute? 使用带有Flutter后退按钮的onGenerateRoute时失败的断言 - Failed assertion when using onGenerateRoute with Flutter back button 使用`Get.back()`返回视图/控制器时如何调用函数? - How to call a function when returning to view/controller with `Get.back()`? 如何使用 onGenerateRoute 在 URL 中显示 Flutter Web 路由名称? - How to show Flutter Web route name in the URL with onGenerateRoute? 如何使用Function在Button内部调用 - How to Use Function to Call it inside the Button Flutter Web-正确的方式使用 onGenerateRoute 和 routes 作为 MaterialApp 的属性 - Flutter Web-Proper way to use both of onGenerateRoute and routes as properties of MaterialApp
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM