如何用go_router_build在Flutter中实现Web与移动端差异化导航
基于go_router_build实现跨平台动态导航适配
核心思路是通过**平台判断(kIsWeb)**结合go_router_build的类型安全路由生成能力,分别构建Web端的shell路由结构和移动端的独立导航路由结构,同时处理导航时的UI差异(固定AppBar/隐藏底部导航)。
1. 拆分平台专属路由配置
在代码中分别为Web和移动端定义不同的路由结构,利用go_router_build的注解生成类型安全的路由代码。
Web端:Shell路由结构
用TypedShellRoute包裹需要固定AppBar的页面,确保内容切换时顶部导航保持不变:
// web_routes.dart part 'web_routes.g.dart'; @TypedShellRoute<WebMainShell>( routes: [ TypedGoRoute<HomeRoute>(path: '/'), TypedGoRoute<SettingsRoute>(path: '/settings'), TypedGoRoute<DetailRoute>(path: '/detail/:id'), // 纳入shell,共享顶部导航 ], ) class WebMainShell extends ShellRouteData { const WebMainShell(); @override Widget builder(BuildContext context, GoRouterState state, Widget child) { return Scaffold( appBar: AppBar(title: const Text('Web固定导航')), body: child, bottomNavigationBar: BottomNavigationBar( items: const [ BottomNavigationBarItem(icon: Icon(Icons.home), label: '首页'), BottomNavigationBarItem(icon: Icon(Icons.settings), label: '设置'), ], currentIndex: state.location == '/' ? 0 : 1, onTap: (index) => context.go(index == 0 ? const HomeRoute().location : const SettingsRoute().location), ), ); } } // 页面路由类 class HomeRoute extends GoRouteData { const HomeRoute(); @override Widget build(BuildContext context, GoRouterState state) => const HomePage(); } class SettingsRoute extends GoRouteData { const SettingsRoute(); @override Widget build(BuildContext context, GoRouterState state) => const SettingsPage(); } class DetailRoute extends GoRouteData { const DetailRoute({required this.id}); final String id; @override Widget build(BuildContext context, GoRouterState state) => DetailPage(id: id); }
移动端:独立导航路由结构
将需要独立导航的页面(如详情页)从shell中抽离,作为单独路由,跳转时自动替换整个页面(包括自定义AppBar),同时通过路由状态判断隐藏底部导航:
// mobile_routes.dart part 'mobile_routes.g.dart'; @TypedShellRoute<MobileMainShell>( routes: [ TypedGoRoute<HomeRoute>(path: '/'), TypedGoRoute<SettingsRoute>(path: '/settings'), // 详情页不纳入shell,作为独立路由 ], ) class MobileMainShell extends ShellRouteData { const MobileMainShell(); @override Widget builder(BuildContext context, GoRouterState state, Widget child) { // 判断当前是否为独立页面,隐藏底部导航 final isIndependentPage = state.location.startsWith('/detail/'); return Scaffold( appBar: AppBar(title: const Text('移动端主导航')), body: child, bottomNavigationBar: isIndependentPage ? null : BottomNavigationBar( items: const [ BottomNavigationBarItem(icon: Icon(Icons.home), label: '首页'), BottomNavigationBarItem(icon: Icon(Icons.settings), label: '设置'), ], currentIndex: state.location == '/' ? 0 : 1, onTap: (index) => context.go(index == 0 ? const HomeRoute().location : const SettingsRoute().location), ), ); } } // 独立详情页路由 @TypedGoRoute<DetailRoute>(path: '/detail/:id') class DetailRoute extends GoRouteData { const DetailRoute({required this.id}); final String id; @override Widget build(BuildContext context, GoRouterState state) { return Scaffold( appBar: AppBar(title: Text('详情页 $id')), body: DetailContent(id: id), ); } }
2. 动态初始化GoRouter
在main.dart中根据平台加载对应路由生成代码:
import 'package:flutter/foundation.dart'; import 'web_routes.g.dart' as web_routes; import 'mobile_routes.g.dart' as mobile_routes; final GoRouter router = GoRouter( routes: kIsWeb ? web_routes.$routes : mobile_routes.$routes, );
3. 统一路由跳转调用
借助go_router_build的类型安全特性,跳转时直接调用路由类的go/push方法,无需关心平台差异:
// 从首页跳详情页,自动适配平台路由逻辑 context.push(const DetailRoute(id: '123').location);
关键注意事项
- 确保Web和移动端的路由路径完全一致,避免跳转时出现路径不匹配问题。
- 移动端shell的builder中,也可以通过
state.routeData.runtimeType判断当前路由类型,替代路径字符串判断,更可靠。 - 运行代码生成命令时,确保两个路由文件都被扫描:
flutter pub run build_runner build
内容的提问来源于stack exchange,提问作者devblock
相关产品推荐
相关产品推荐

