Files
platforms/apps/user_app/lib/app/router.dart
2026-09-04 21:54:35 +08:00

148 lines
4.7 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 功能描述:配置用户端路由、鉴权守卫及登录后安全回跳。
// 版本1.1.0
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import '../ui/features/auth/login_page.dart';
import '../ui/features/auth/register_page.dart';
import '../ui/features/home/home_page.dart';
import '../ui/features/orders/orders_page.dart';
import '../ui/features/profile/profile_page.dart';
import '../ui/features/shared/record_list_page.dart';
import '../ui/features/shared/record_list_view_model.dart';
import '../ui/features/shop/shop_page.dart';
import 'auth_navigation.dart';
import 'dependencies.dart';
GoRouter createRouter(
AppDependencies dependencies, {
String initialLocation = '/home',
}) => GoRouter(
initialLocation: initialLocation,
refreshListenable: dependencies.session,
redirect: (context, state) {
final authRoute = state.matchedLocation == '/login' || state.matchedLocation == '/register';
if (!dependencies.session.isAuthenticated && !authRoute) {
return buildAuthLocation(
'/login',
redirectTarget: state.uri.toString(),
sessionExpired: dependencies.session.hasExpired,
);
}
if (dependencies.session.isAuthenticated && state.matchedLocation == '/login') {
return sanitizeRedirectTarget(state.uri.queryParameters['redirect']) ?? '/home';
}
return null;
},
routes: [
GoRoute(
path: '/login',
builder: (context, state) => LoginPage(
session: dependencies.session,
redirectTarget: sanitizeRedirectTarget(state.uri.queryParameters['redirect']),
showSessionExpiredMessage: state.uri.queryParameters['reason'] == 'expired',
),
),
GoRoute(
path: '/register',
builder: (context, state) => RegisterPage(
session: dependencies.session,
redirectTarget: sanitizeRedirectTarget(state.uri.queryParameters['redirect']),
showSessionExpiredMessage: state.uri.queryParameters['reason'] == 'expired',
),
),
GoRoute(
path: '/records/contracts',
builder: (context, state) => RecordListPage(
title: '供气合同',
eyebrow: '可信履约',
viewModel: RecordListViewModel(dependencies.repository.contracts),
),
),
GoRoute(
path: '/records/wallet',
builder: (context, state) => RecordListPage(
title: '钱包流水',
eyebrow: '资金记录',
viewModel: RecordListViewModel(dependencies.repository.walletRecords),
),
),
StatefulShellRoute.indexedStack(
builder: (context, state, navigationShell) => _UserShell(navigationShell: navigationShell),
branches: [
StatefulShellBranch(
routes: [
GoRoute(
path: '/home',
builder: (context, state) => HomePage(repository: dependencies.repository),
),
],
),
StatefulShellBranch(
routes: [
GoRoute(
path: '/shop',
builder: (context, state) => ShopPage(repository: dependencies.repository),
),
],
),
StatefulShellBranch(
routes: [
GoRoute(
path: '/orders',
builder: (context, state) => OrdersPage(repository: dependencies.repository),
),
],
),
StatefulShellBranch(
routes: [
GoRoute(
path: '/me',
builder: (context, state) =>
ProfilePage(session: dependencies.session, repository: dependencies.repository),
),
],
),
],
),
],
);
class _UserShell extends StatelessWidget {
const _UserShell({required this.navigationShell});
final StatefulNavigationShell navigationShell;
@override
Widget build(BuildContext context) => Scaffold(
body: navigationShell,
bottomNavigationBar: NavigationBar(
selectedIndex: navigationShell.currentIndex,
onDestinationSelected: (index) =>
navigationShell.goBranch(index, initialLocation: index == navigationShell.currentIndex),
destinations: const [
NavigationDestination(
icon: Icon(Icons.home_outlined),
selectedIcon: Icon(Icons.home),
label: '首页',
),
NavigationDestination(
icon: Icon(Icons.shopping_bag_outlined),
selectedIcon: Icon(Icons.shopping_bag),
label: '商城',
),
NavigationDestination(
icon: Icon(Icons.receipt_long_outlined),
selectedIcon: Icon(Icons.receipt_long),
label: '订单',
),
NavigationDestination(
icon: Icon(Icons.person_outline),
selectedIcon: Icon(Icons.person),
label: '我的',
),
],
),
);
}