Difference between onGenerateRoute and routes in Flutter

后端 未结 3 1978
太阳男子
太阳男子 2021-02-18 22:27

What is the benefit or use case of onGenerateRoute and routes in Flutter.

In my application in first page inside MaterialApp we can define rout

3条回答
  •  借酒劲吻你
    2021-02-18 22:50

    My solution is to re-assign the settings of MaterialPageRoute.

    eg: //配置路由

    final routes = {
      '/': (context) => MainPage(),
    };
    

    // 统一处理

    var onGenerateRoute = (RouteSettings settings) {
        final String name = settings.name;
          final Function pageContentBuilder = routes[name];
          if (pageContentBuilder != null) {
            if (settings.arguments != null) {
              final Route route = MaterialPageRoute(
                  settings: settings,
                  builder: (context) => pageContentBuilder(context, arguments: settings.arguments));
              return route;
            } else {
              final Route route =
                  MaterialPageRoute(settings: settings,
                      builder: (context) => pageContentBuilder(context));
              return route;
            }
          }
        };
    
    
    
       
    

提交回复
热议问题