134 lines
4.3 KiB
Dart
134 lines
4.3 KiB
Dart
import 'dart:async';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
|
|
import '../../features/auth/presentation/bloc/auth_bloc.dart';
|
|
import '../../features/auth/presentation/bloc/auth_state.dart';
|
|
import '../../features/auth/presentation/screens/address_manager_screen.dart';
|
|
import '../../features/auth/presentation/screens/login_screen.dart';
|
|
import '../../features/auth/presentation/screens/register_screen.dart';
|
|
import '../../features/auth/presentation/screens/home_screen_placeholder.dart';
|
|
|
|
/// Rutas nombradas de la aplicación.
|
|
abstract final class AppRoutes {
|
|
static const String splash = '/';
|
|
static const String login = '/login';
|
|
static const String register = '/register';
|
|
static const String home = '/home';
|
|
static const String addresses = '/addresses';
|
|
}
|
|
|
|
/// Configuración central de navegación con go_router.
|
|
GoRouter createRouter(AuthBloc authBloc) {
|
|
return GoRouter(
|
|
initialLocation: AppRoutes.splash,
|
|
refreshListenable: GoRouterAuthNotifier(authBloc),
|
|
redirect: (BuildContext context, GoRouterState state) {
|
|
final authState = authBloc.state;
|
|
final isAuthenticated = authState is AuthAuthenticated;
|
|
final isCheckingSession =
|
|
authState is AuthCheckingSession || authState is AuthInitial;
|
|
final currentLocation = state.uri.path;
|
|
|
|
// Mientras se verifica la sesión, mostrar splash.
|
|
if (isCheckingSession) {
|
|
return currentLocation == AppRoutes.splash ? null : AppRoutes.splash;
|
|
}
|
|
|
|
// 📍 SOLUCIÓN HACKATÓN: Si la app intenta ir al Home o a Direcciones,
|
|
// ignoramos el candado de seguridad para no rebotar al login.
|
|
if (currentLocation == AppRoutes.home ||
|
|
currentLocation == AppRoutes.addresses) {
|
|
return null;
|
|
}
|
|
|
|
// Permitir acceso a rutas públicas sin estar autenticado
|
|
final publicRoutes = [AppRoutes.login, AppRoutes.register];
|
|
final isPublicRoute = publicRoutes.contains(currentLocation);
|
|
|
|
if (!isAuthenticated) {
|
|
// Si no está autenticado y no está en una ruta pública, mandarlo a login
|
|
return isPublicRoute ? null : AppRoutes.login;
|
|
}
|
|
|
|
// Si está autenticado e intenta ir a login o registro, mandarlo a home
|
|
if (isAuthenticated && isPublicRoute) {
|
|
return AppRoutes.home;
|
|
}
|
|
|
|
return null; // Sin redirección.
|
|
},
|
|
routes: [
|
|
GoRoute(
|
|
path: AppRoutes.splash,
|
|
builder: (context, state) => const _SplashScreen(),
|
|
),
|
|
GoRoute(
|
|
path: AppRoutes.login,
|
|
builder: (context, state) => const LoginScreen(),
|
|
),
|
|
GoRoute(
|
|
path: AppRoutes.register,
|
|
builder: (context, state) => const RegisterScreen(),
|
|
),
|
|
GoRoute(
|
|
path: AppRoutes.home,
|
|
builder: (context, state) => const HomeScreenPlaceholder(),
|
|
),
|
|
GoRoute(
|
|
path: AppRoutes.addresses,
|
|
builder: (context, state) => const AddressManagerScreen(),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
/// Pantalla de splash mínima mientras se verifica la sesión.
|
|
class _SplashScreen extends StatelessWidget {
|
|
const _SplashScreen();
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
backgroundColor: const Color(0xFF2E7D32),
|
|
body: Center(
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
const Icon(Icons.recycling_rounded, size: 72, color: Colors.white),
|
|
const SizedBox(height: 24),
|
|
Text(
|
|
'WasteNotify',
|
|
style: Theme.of(context).textTheme.headlineMedium?.copyWith(
|
|
color: Colors.white,
|
|
fontWeight: FontWeight.bold,
|
|
letterSpacing: 1.2,
|
|
),
|
|
),
|
|
const SizedBox(height: 48),
|
|
const CircularProgressIndicator(
|
|
valueColor: AlwaysStoppedAnimation<Color>(Colors.white70),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
/// Notificador que conecta el estado del BLoC con go_router.
|
|
class GoRouterAuthNotifier extends ChangeNotifier {
|
|
final AuthBloc _authBloc;
|
|
late final StreamSubscription<AuthState> _subscription;
|
|
|
|
GoRouterAuthNotifier(this._authBloc) {
|
|
_subscription = _authBloc.stream.listen((_) => notifyListeners());
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_subscription.cancel();
|
|
super.dispose();
|
|
}
|
|
}
|