feat(apps/router): 重构路由系统使用 AppRoutes 常量
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
final RouteObserver<ModalRoute<void>> appRouteObserver =
|
||||
RouteObserver<ModalRoute<void>>();
|
||||
@@ -1,6 +1,8 @@
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'app_route_observer.dart';
|
||||
import '../../features/auth/presentation/bloc/auth_bloc.dart';
|
||||
import '../../features/auth/presentation/bloc/auth_state.dart';
|
||||
import 'app_routes.dart';
|
||||
import 'go_router_refresh_stream.dart';
|
||||
import '../../features/auth/ui/screens/login_screen.dart';
|
||||
import '../../features/auth/ui/screens/auth_boot_screen.dart';
|
||||
@@ -15,9 +17,13 @@ import '../../features/contacts/ui/screens/add_contact_screen.dart';
|
||||
import '../../features/calendar/ui/screens/calendar_dayweek_screen.dart';
|
||||
import '../../features/calendar/ui/screens/calendar_month_screen.dart';
|
||||
import '../../features/calendar/ui/screens/calendar_event_detail_screen.dart';
|
||||
import '../../features/calendar/ui/screens/calendar_event_create_screen.dart';
|
||||
import '../../features/calendar/ui/screens/calendar_event_edit_screen.dart';
|
||||
import '../../features/calendar/ui/screens/calendar_event_share_screen.dart';
|
||||
import '../../features/calendar/ui/calendar_time_utils.dart';
|
||||
import '../../features/todo/ui/screens/todo_quadrants_screen.dart';
|
||||
import '../../features/todo/ui/screens/todo_detail_screen.dart';
|
||||
import '../../features/todo/ui/screens/todo_edit_screen.dart';
|
||||
import '../../features/settings/ui/screens/settings_screen.dart';
|
||||
import '../../features/settings/ui/screens/features_screen.dart';
|
||||
import '../../features/settings/ui/screens/memory_screen.dart';
|
||||
@@ -26,34 +32,35 @@ import '../../features/settings/ui/screens/change_password_screen.dart';
|
||||
import '../../features/settings/ui/screens/edit_profile_screen.dart';
|
||||
|
||||
final _protectedRoutes = [
|
||||
'/home',
|
||||
'/contacts',
|
||||
'/contacts/add',
|
||||
'/calendar/dayweek',
|
||||
'/calendar/month',
|
||||
AppRoutes.homeMain,
|
||||
AppRoutes.contactsList,
|
||||
AppRoutes.contactsAdd,
|
||||
AppRoutes.calendarDayWeek,
|
||||
AppRoutes.calendarMonth,
|
||||
'/calendar/events',
|
||||
'/todo',
|
||||
'/settings',
|
||||
'/settings/features',
|
||||
'/settings/memory',
|
||||
'/settings/account',
|
||||
'/change-password',
|
||||
'/edit-profile',
|
||||
'/messages/invites',
|
||||
AppRoutes.todoList,
|
||||
AppRoutes.settingsMain,
|
||||
AppRoutes.settingsFeatures,
|
||||
AppRoutes.settingsMemory,
|
||||
AppRoutes.settingsAccount,
|
||||
AppRoutes.settingsChangePassword,
|
||||
AppRoutes.settingsEditProfile,
|
||||
AppRoutes.messageInviteList,
|
||||
];
|
||||
|
||||
GoRouter createAppRouter(AuthBloc authBloc) {
|
||||
return GoRouter(
|
||||
initialLocation: '/boot',
|
||||
initialLocation: AppRoutes.authBoot,
|
||||
observers: [appRouteObserver],
|
||||
refreshListenable: GoRouterRefreshStream(authBloc.stream),
|
||||
redirect: (context, state) {
|
||||
final authState = authBloc.state;
|
||||
final isAuthenticated = authState is AuthAuthenticated;
|
||||
final isAuthChecking =
|
||||
authState is AuthInitial || authState is AuthLoading;
|
||||
final isBootRoute = state.matchedLocation == '/boot';
|
||||
final isBootRoute = state.matchedLocation == AppRoutes.authBoot;
|
||||
final isAuthRoute =
|
||||
state.matchedLocation == '/' ||
|
||||
state.matchedLocation == AppRoutes.authLogin ||
|
||||
state.matchedLocation.startsWith('/login') ||
|
||||
state.matchedLocation.startsWith('/register');
|
||||
final isProtected = _protectedRoutes.any(
|
||||
@@ -61,61 +68,84 @@ GoRouter createAppRouter(AuthBloc authBloc) {
|
||||
);
|
||||
|
||||
if (isAuthChecking && !isBootRoute) {
|
||||
return '/boot';
|
||||
return AppRoutes.authBoot;
|
||||
}
|
||||
if (!isAuthChecking && isBootRoute) {
|
||||
return isAuthenticated ? '/home' : '/';
|
||||
return isAuthenticated ? AppRoutes.homeMain : AppRoutes.authLogin;
|
||||
}
|
||||
if (!isAuthenticated && isProtected) {
|
||||
return '/';
|
||||
return AppRoutes.authLogin;
|
||||
}
|
||||
if (isAuthenticated && isAuthRoute) {
|
||||
return '/home';
|
||||
return AppRoutes.homeMain;
|
||||
}
|
||||
return null;
|
||||
},
|
||||
routes: [
|
||||
GoRoute(
|
||||
path: '/boot',
|
||||
path: AppRoutes.authBoot,
|
||||
builder: (context, state) => const AuthBootScreen(),
|
||||
),
|
||||
GoRoute(path: '/', builder: (context, state) => const LoginScreen()),
|
||||
GoRoute(
|
||||
path: AppRoutes.authLogin,
|
||||
builder: (context, state) => const LoginScreen(),
|
||||
),
|
||||
GoRoute(
|
||||
path: AppRoutes.calendarEventCreate,
|
||||
builder: (context, state) => CalendarEventCreateScreen(
|
||||
initialDate: parseYmd(state.uri.queryParameters['date']),
|
||||
),
|
||||
),
|
||||
GoRoute(
|
||||
path: '/calendar/events/:id',
|
||||
builder: (context, state) =>
|
||||
CalendarEventDetailScreen(eventId: state.pathParameters['id']!),
|
||||
),
|
||||
GoRoute(
|
||||
path: '/register',
|
||||
path: '/calendar/events/:id/edit',
|
||||
builder: (context, state) =>
|
||||
CalendarEventEditScreen(eventId: state.pathParameters['id']!),
|
||||
),
|
||||
GoRoute(
|
||||
path: '/calendar/events/:id/share',
|
||||
builder: (context, state) =>
|
||||
CalendarEventShareScreen(eventId: state.pathParameters['id']!),
|
||||
),
|
||||
GoRoute(
|
||||
path: AppRoutes.authRegister,
|
||||
builder: (context, state) => const RegisterScreen(),
|
||||
),
|
||||
GoRoute(
|
||||
path: '/register/verification',
|
||||
path: AppRoutes.authRegisterVerification,
|
||||
builder: (context, state) => const RegisterVerificationScreen(),
|
||||
),
|
||||
GoRoute(
|
||||
path: '/reset-password',
|
||||
path: AppRoutes.authResetPassword,
|
||||
builder: (context, state) => const ResetPasswordScreen(),
|
||||
),
|
||||
GoRoute(path: '/home', builder: (context, state) => const HomeScreen()),
|
||||
GoRoute(
|
||||
path: '/messages/invites',
|
||||
path: AppRoutes.homeMain,
|
||||
builder: (context, state) => const HomeScreen(),
|
||||
),
|
||||
GoRoute(
|
||||
path: AppRoutes.messageInviteList,
|
||||
builder: (context, state) => const MessageInviteListScreen(),
|
||||
),
|
||||
GoRoute(
|
||||
path: '/messages/invites/:id',
|
||||
builder: (context, state) => const MessageInviteDetailScreen(),
|
||||
builder: (context, state) =>
|
||||
MessageInviteDetailScreen(inviteId: state.pathParameters['id']!),
|
||||
),
|
||||
GoRoute(
|
||||
path: '/contacts',
|
||||
path: AppRoutes.contactsList,
|
||||
builder: (context, state) => const ContactsScreen(),
|
||||
),
|
||||
GoRoute(
|
||||
path: '/contacts/add',
|
||||
path: AppRoutes.contactsAdd,
|
||||
builder: (context, state) => const AddContactScreen(),
|
||||
),
|
||||
GoRoute(
|
||||
path: '/calendar/dayweek',
|
||||
path: AppRoutes.calendarDayWeek,
|
||||
builder: (context, state) {
|
||||
final fromHome = state.uri.queryParameters['from'] == 'home';
|
||||
final initialDate = parseYmd(state.uri.queryParameters['date']);
|
||||
@@ -126,43 +156,52 @@ GoRouter createAppRouter(AuthBloc authBloc) {
|
||||
},
|
||||
),
|
||||
GoRoute(
|
||||
path: '/calendar/month',
|
||||
path: AppRoutes.calendarMonth,
|
||||
builder: (context, state) {
|
||||
final fromHome = state.uri.queryParameters['from'] == 'home';
|
||||
return CalendarMonthScreen(resetToToday: fromHome);
|
||||
},
|
||||
),
|
||||
GoRoute(
|
||||
path: '/todo',
|
||||
path: AppRoutes.todoList,
|
||||
builder: (context, state) => const TodoQuadrantsScreen(),
|
||||
),
|
||||
GoRoute(
|
||||
path: AppRoutes.todoCreate,
|
||||
builder: (context, state) => const TodoEditScreen.create(),
|
||||
),
|
||||
GoRoute(
|
||||
path: '/todo/:id',
|
||||
builder: (context, state) =>
|
||||
TodoDetailScreen(todoId: state.pathParameters['id']!),
|
||||
),
|
||||
GoRoute(
|
||||
path: '/settings',
|
||||
path: '/todo/:id/edit',
|
||||
builder: (context, state) =>
|
||||
TodoEditScreen(todoId: state.pathParameters['id']!),
|
||||
),
|
||||
GoRoute(
|
||||
path: AppRoutes.settingsMain,
|
||||
builder: (context, state) => const SettingsScreen(),
|
||||
),
|
||||
GoRoute(
|
||||
path: '/settings/features',
|
||||
path: AppRoutes.settingsFeatures,
|
||||
builder: (context, state) => const FeaturesScreen(),
|
||||
),
|
||||
GoRoute(
|
||||
path: '/settings/memory',
|
||||
path: AppRoutes.settingsMemory,
|
||||
builder: (context, state) => const MemoryScreen(),
|
||||
),
|
||||
GoRoute(
|
||||
path: '/settings/account',
|
||||
path: AppRoutes.settingsAccount,
|
||||
builder: (context, state) => const AccountScreen(),
|
||||
),
|
||||
GoRoute(
|
||||
path: '/change-password',
|
||||
path: AppRoutes.settingsChangePassword,
|
||||
builder: (context, state) => const ChangePasswordScreen(),
|
||||
),
|
||||
GoRoute(
|
||||
path: '/edit-profile',
|
||||
path: AppRoutes.settingsEditProfile,
|
||||
builder: (context, state) => const EditProfileScreen(),
|
||||
),
|
||||
],
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
class AppRoutes {
|
||||
AppRoutes._();
|
||||
|
||||
static const authBoot = '/boot';
|
||||
static const authLogin = '/';
|
||||
static const authRegister = '/register';
|
||||
static const authRegisterVerification = '/register/verification';
|
||||
static const authResetPassword = '/reset-password';
|
||||
|
||||
static const homeMain = '/home';
|
||||
|
||||
static const messageInviteList = '/messages/invites';
|
||||
static String messageInviteDetail(String id) => '/messages/invites/$id';
|
||||
|
||||
static const contactsList = '/contacts';
|
||||
static const contactsAdd = '/contacts/add';
|
||||
|
||||
static const calendarDayWeek = '/calendar/dayweek';
|
||||
static const calendarMonth = '/calendar/month';
|
||||
static String calendarEventDetail(String id) => '/calendar/events/$id';
|
||||
static const calendarEventCreate = '/calendar/events/new';
|
||||
static String calendarEventEdit(String id) => '/calendar/events/$id/edit';
|
||||
static String calendarEventShare(String id) => '/calendar/events/$id/share';
|
||||
|
||||
static const todoList = '/todo';
|
||||
static String todoDetail(String id) => '/todo/$id';
|
||||
static const todoCreate = '/todo/new';
|
||||
static String todoEdit(String id) => '/todo/$id/edit';
|
||||
|
||||
static const settingsMain = '/settings';
|
||||
static const settingsFeatures = '/settings/features';
|
||||
static const settingsMemory = '/settings/memory';
|
||||
static const settingsAccount = '/settings/account';
|
||||
static const settingsChangePassword = '/change-password';
|
||||
static const settingsEditProfile = '/edit-profile';
|
||||
}
|
||||
Reference in New Issue
Block a user