Files

126 lines
4.0 KiB
Dart
Raw Permalink Normal View History

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 'dependencies.dart';
GoRouter createRouter(AppDependencies dependencies) => GoRouter(
initialLocation: '/home',
refreshListenable: dependencies.session,
redirect: (context, state) {
final authRoute = state.matchedLocation == '/login' || state.matchedLocation == '/register';
if (!dependencies.session.isAuthenticated && !authRoute) return '/login';
if (dependencies.session.isAuthenticated && state.matchedLocation == '/login') return '/home';
return null;
},
routes: [
GoRoute(
path: '/login',
builder: (context, state) => LoginPage(session: dependencies.session),
),
GoRoute(
path: '/register',
builder: (context, state) => RegisterPage(session: dependencies.session),
),
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: '我的',
),
],
),
);
}