import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:go_router/go_router.dart'; import 'package:riverpod_annotation/riverpod_annotation.dart'; import '../../features/admin/presentation/screens/admin_home_screen.dart'; import '../../features/admin/presentation/screens/system_settings_screen.dart'; import '../../features/admin/presentation/screens/user_management_screen.dart'; import '../../features/auth/presentation/screens/login_screen.dart'; import '../../features/auth/presentation/screens/register_screen.dart'; import '../../features/dashboard/presentation/screens/dashboard_screen.dart'; import '../../features/user/presentation/screens/user_home_screen.dart'; import '../../features/user/presentation/screens/user_profile_screen.dart'; import '../../shared/providers/auth_provider.dart'; import '../../shared/widgets/app_scaffold.dart'; import 'auth_guard.dart'; import 'route_names.dart'; part 'app_router.g.dart'; @Riverpod(keepAlive: true) GoRouter appRouter(Ref ref) { final authGuard = AuthGuard(ref); final authState = ref.watch(authStateProvider); return GoRouter( initialLocation: RoutePaths.login, debugLogDiagnostics: true, refreshListenable: _GoRouterRefreshStream(ref, authState), redirect: (context, state) { final location = state.uri.toString(); return authGuard.redirect(context, location); }, routes: [ // Auth Routes (비인증) GoRoute( name: RouteNames.login, path: RoutePaths.login, builder: (context, state) => const LoginScreen(), ), GoRoute( name: RouteNames.register, path: RoutePaths.register, builder: (context, state) => const RegisterScreen(), ), // User Shell Route ShellRoute( builder: (context, state, child) => AppScaffold( currentPath: state.uri.toString(), child: child, ), routes: [ GoRoute( name: RouteNames.userHome, path: RoutePaths.userHome, builder: (context, state) => const UserHomeScreen(), ), GoRoute( name: RouteNames.userProfile, path: RoutePaths.userProfile, builder: (context, state) => const UserProfileScreen(), ), GoRoute( name: RouteNames.userDashboard, path: RoutePaths.userDashboard, builder: (context, state) => const DashboardScreen(), ), ], ), // Admin Shell Route ShellRoute( builder: (context, state, child) => AppScaffold( currentPath: state.uri.toString(), isAdmin: true, child: child, ), routes: [ GoRoute( name: RouteNames.adminHome, path: RoutePaths.adminHome, builder: (context, state) => const AdminHomeScreen(), ), GoRoute( name: RouteNames.adminUsers, path: RoutePaths.adminUsers, builder: (context, state) => const UserManagementScreen(), ), GoRoute( name: RouteNames.adminDashboard, path: RoutePaths.adminDashboard, builder: (context, state) => const DashboardScreen(), ), GoRoute( name: RouteNames.adminSettings, path: RoutePaths.adminSettings, builder: (context, state) => const SystemSettingsScreen(), ), ], ), ], ); } /// GoRouter에서 Riverpod 상태 변경 시 리프레시하기 위한 Listenable 래퍼 class _GoRouterRefreshStream extends ChangeNotifier { _GoRouterRefreshStream(this.ref, dynamic _) { // authState 변경 시 GoRouter refresh 트리거 notifyListeners(); } final Ref ref; }