import 'package:flutter/material.dart'; import 'package:go_router/go_router.dart'; import 'package:hooks_riverpod/hooks_riverpod.dart'; import '../../features/assets/presentation/assets_page.dart'; import '../../features/auth/presentation/delete_account_page.dart'; import '../../features/auth/presentation/login_page.dart'; import '../../features/debug/presentation/debug_info_page.dart'; import '../../features/debug/presentation/debug_page.dart'; import '../../features/debug/presentation/debug_shorebird_page.dart'; import '../../features/debug/presentation/fancy_dio_inspector_page.dart'; import '../../features/debug/presentation/typography_test_page.dart'; import '../../features/market/presentation/market_page.dart'; import '../../features/news/presentation/news_page.dart'; import '../../features/profile/presentation/profile_page.dart'; import '../../features/settings/presentation/settings_page.dart'; import '../../features/settings/presentation/privacy_consent_page.dart'; import '../../features/web/presentation/web_page.dart'; import '../services/device_header_service.dart'; import 'tab_shell.dart'; import 'route_error_page.dart'; abstract final class AppRoutes { static const assets = '/assets'; static const market = '/market'; static const news = '/news'; static const profile = '/profile'; static const login = '/login'; static const settings = '/settings'; static const deleteAccount = '/delete-account'; static const deleteAccountCompleted = '/delete-account-completed'; static const debug = '/debug'; static const debugInfo = '/debug/info'; static const debugShorebird = '/debug/shorebird'; static const debugTypography = '/debug/typography'; static const fancyDioInspector = '/debug/fancy_dio_inspector'; static const privacyConsent = '/privacy-consent'; static const web = '/web'; } final rootNavigatorKey = GlobalKey(); final routerProvider = Provider((ref) { final startupListenable = ValueNotifier(0); return GoRouter( navigatorKey: rootNavigatorKey, initialLocation: AppRoutes.assets, refreshListenable: startupListenable, errorBuilder: (_, state) => RouteErrorPage(error: state.error), redirect: (context, state) { final loc = state.matchedLocation; final onConsent = loc == AppRoutes.privacyConsent; final showStartup = DeviceHeaderService.to.privacyConsentRequired || DeviceHeaderService.to.iosStartupRequired; if (showStartup) { if (!onConsent) return AppRoutes.privacyConsent; } else if (onConsent) { return AppRoutes.assets; } return null; }, routes: [ StatefulShellRoute.indexedStack( builder: (context, state, navigationShell) => TabShell( location: state.matchedLocation, navigationShell: navigationShell, ), branches: [ StatefulShellBranch( routes: [ GoRoute( path: AppRoutes.assets, builder: (ctx, s) => const AssetsPage(), ), ], ), StatefulShellBranch( routes: [ GoRoute( path: AppRoutes.market, builder: (ctx, s) => const MarketPage(), ), ], ), StatefulShellBranch( routes: [ GoRoute( path: AppRoutes.news, builder: (ctx, s) => const NewsPage(), ), ], ), StatefulShellBranch( routes: [ GoRoute( path: AppRoutes.profile, builder: (ctx, s) => const ProfilePage(), ), ], ), ], ), GoRoute(path: AppRoutes.debug, builder: (_, s) => const DebugPage()), GoRoute( path: AppRoutes.login, builder: (_, s) => LoginPage(next: s.uri.queryParameters['next']), ), GoRoute( path: AppRoutes.settings, builder: (_, s) => const SettingsPage(), ), GoRoute( path: AppRoutes.deleteAccount, builder: (_, s) => const DeleteAccountPage(), ), GoRoute( path: AppRoutes.deleteAccountCompleted, builder: (_, s) => const DeleteAccountSuccessPage(), ), GoRoute( path: AppRoutes.debugInfo, builder: (_, s) => const DebugInfoPage(), ), GoRoute( path: AppRoutes.debugShorebird, builder: (_, s) => const DebugShorebirdPage(), ), GoRoute( path: AppRoutes.debugTypography, builder: (_, s) => const TypographyTestPage(), ), GoRoute( path: AppRoutes.fancyDioInspector, builder: (_, s) => const FancyDioInspectorPage(), ), GoRoute( path: AppRoutes.privacyConsent, builder: (_, s) => const PrivacyConsentPage(), ), GoRoute( path: AppRoutes.web, builder: (_, state) { final uri = parseSafeWebUri(state.uri.queryParameters['url']); if (uri == null) { return const Scaffold(body: Center(child: Text('页面地址不可用'))); } return AppWebPage( initialUri: uri, hideTitle: state.uri.queryParameters['hideTitle'] == '1', ); }, ), ], ); });