43 lines
1.3 KiB
Dart
43 lines
1.3 KiB
Dart
import 'package:flutter/material.dart';
|
|||
|
|
import 'package:go_router/go_router.dart';
|
||
|
|
import '../features/auth/pages/login_page.dart';
|
||
|
|
import '../features/auth/services/token_service.dart';
|
||
|
|
import '../features/main_shell/pages/main_shell_page.dart';
|
||
|
|
import '../features/revenue/pages/bill_list_page.dart';
|
||
|
|
|
||
|
|
/// 应用路由配置
|
||
|
|
class AppRoutes {
|
||
|
|
static const String login = '/login';
|
||
|
|
static const String main = '/main';
|
||
|
|
static const String bills = '/bills';
|
||
|
|
|
||
|
|
static GoRouter createRouter() {
|
||
|
|
return GoRouter(
|
||
|
|
initialLocation: login,
|
||
|
|
redirect: (BuildContext context, GoRouterState state) async {
|
||
|
|
final tokenService = TokenService();
|
||
|
|
final isLoggedIn = await tokenService.isLoggedIn();
|
||
|
|
final isGoingToLogin = state.matchedLocation == login;
|
||
|
|
|
||
|
|
if (!isLoggedIn && !isGoingToLogin) return login;
|
||
|
|
if (isLoggedIn && isGoingToLogin) return main;
|
||
|
|
return null;
|
||
|
|
},
|
||
|
|
routes: [
|
||
|
|
GoRoute(
|
||
|
|
path: login,
|
||
|
|
builder: (context, state) => const LoginPage(),
|
||
|
|
),
|
||
|
|
GoRoute(
|
||
|
|
path: main,
|
||
|
|
builder: (context, state) => const MainShellPage(),
|
||
|
|
),
|
||
|
|
GoRoute(
|
||
|
|
path: bills,
|
||
|
|
builder: (context, state) => const BillListPage(),
|
||
|
|
),
|
||
|
|
],
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|