Flutter开发: 路由封装与页面传值

众所周知Flutter乃是回调地狱(xD),所以路由一定要好好封装
var onGenerateRoute =  (RouteSettings settings){ 
        final String routerName = settings.name;  //获取路由名称
        final Function content = routers[routerName]; 
        if(content == null){ //检测路由是否存在
          return MaterialPageRoute(builder: (context) => routers['/error']());
        }
        if(settings.arguments == null){ //判断是否有传值
          return MaterialPageRoute(builder: (context) => content());
        }else{
          return MaterialPageRoute(builder: (context) => content(settings.arguments));
        }
};
下面路由指向基本用法
final routers = {
  "/": () => Home(), //默认主页
  "/error": () => Error(), //错误页面
  "/usersInfo": (arguments) => Users(usersInfo: arguments,) //传值案例
};

2 评论

  1. It agree, your idea simply excellent

留下评论