用途:为 Flutter 应用配置声明式路由,使用 go_router 包实现基于 URL 的导航、深度链接与嵌套导航,适合 Web 应用或需要复杂路由的移动应用。
主要能力:GoRouter 定义路由树;GoRoute 将 URL 路径映射到页面;ShellRoute / StatefulShellRoute 提供持久化 UI 外壳(如底部导航栏)并保持各分支状态;Path URL Strategy 去除 Web URL 中的 # 片段,便于跨平台深度链接;支持 redirect 处理鉴权或状态路由,errorBuilder 处理错误页。
使用方式:先创建应用并添加依赖:flutter create <app-name>、cd <app-name>、flutter pub add go_router。在 main 中调用 usePathUrlStrategy(),定义顶层 GoRouter 实例并绑定到 MaterialApp.router 的 routerConfig。导航可用 context.go()、context.push()、context.goNamed()、context.pop()。嵌套导航使用 StatefulShellRoute.indexedStack 与 StatefulShellBranch,外壳组件通过 StatefulNavigationShell 切换分支。
深度链接:Android 需在 AndroidManifest.xml 的 MainActivity 添加 intent-filter,并在域名下托管 .well-known/assetlinks.json;iOS 需在 Info.plist 设置 FlutterDeepLinkingEnabled,在 Runner.entitlements 添加关联域名,并托管 apple-app-site-association。测试可用 adb shell 或 xcrun simctl openurl。
限制:资料未提供具体版本要求、性能数据或收费信息;若使用第三方深度链接插件(如 app_links),需将 FlutterDeepLinkingEnabled 设为 NO 以避免冲突。