SuperAPI AI 工具集

flutter-go-router-navigation

编程与开发

查看项目

更新于 10-15 22:13

技能介绍

用途:在 Flutter 项目中实现类型化路由、重定向与导航守卫,适用于修改 go_router 导航相关代码的场景。

主要能力与约定: 结构上建议将路由放在 core/router/ 下,app_router.dart 存放路由配置,routes.dart 存放类型化路由定义(GoRouteData)。 类型化路由:始终使用 go_router_builder 提供的 GoRouteData 与 @TypedGoRoute,不使用原始路径字符串。 参数:在路由类中定义强类型参数,例如 class OrderDetailRoute extends GoRouteData { final String id; },路径形如 '/orders/:id'。 根路由:在依赖注入中注册唯一的全局 GoRouter 实例。 子路由:通过 TypedGoRoute 与 children 列表嵌套相关路由。 重定向:在 GoRouter 配置的 redirect 回调中处理登录校验,例如 redirect: (context, state) => isLoggedIn ? null : '/login',不要在页面组件内检查鉴权。 标签页:使用 StatefulShellRoute 与 branches 实现底部标签栏(如 Home、Orders、Profile),使每个标签保持独立导航栈。 转场:在 buildPage 中定义统一转场(Fade、Slide)。 导航:使用 MyRoute().go(context) 或 MyRoute().push(context),例如 OrderDetailRoute(id: id).go(context)。

反模式:不使用原始字符串路径(如 context.go('/orders/123'));不在 UI 的 build() 中写内联鉴权;不注册多个 Router;构建路由前始终在 redirect 中校验参数是否存在。

相关主题:layer-based-clean-architecture、auto-route-navigation、security。

限制:资料未提供具体安装命令、版本要求、完整代码示例与测试细节,相关实现可参考 references/typed-routes.md。