97 lines
3.0 KiB
Dart
97 lines
3.0 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
|
|
import '../auth/auth_provider.dart';
|
|
import '../../features/auth/presentation/login_screen.dart';
|
|
import '../../features/auth/presentation/register_screen.dart';
|
|
import '../../features/home/presentation/home_screen.dart';
|
|
import '../../features/establishments/presentation/establishment_detail_screen.dart';
|
|
import '../../features/wallet/presentation/wallet_screen.dart';
|
|
import '../../features/booking/presentation/bookings_screen.dart';
|
|
import '../../features/wash/presentation/wash_screen.dart';
|
|
import '../../features/profile/presentation/profile_screen.dart';
|
|
|
|
/// Routes nommées de l'application.
|
|
abstract final class AppRoutes {
|
|
static const login = '/login';
|
|
static const register = '/register';
|
|
static const home = '/';
|
|
static const wallet = '/wallet';
|
|
static const bookings = '/bookings';
|
|
static const washes = '/washes';
|
|
static const profile = '/profile';
|
|
static const establishment = '/establishments/:uuid';
|
|
}
|
|
|
|
/// Configuration GoRouter avec redirection selon l'état d'authentification.
|
|
final appRouterProvider = Provider<GoRouter>((ref) {
|
|
final authState = ref.watch(authProvider);
|
|
|
|
return GoRouter(
|
|
initialLocation: AppRoutes.home,
|
|
refreshListenable: GoRouterRefreshStream(ref),
|
|
redirect: (context, state) {
|
|
final isAuthenticated = authState.isAuthenticated;
|
|
final isAuthRoute = state.matchedLocation == AppRoutes.login ||
|
|
state.matchedLocation == AppRoutes.register;
|
|
|
|
if (!isAuthenticated && !isAuthRoute) {
|
|
return AppRoutes.login;
|
|
}
|
|
|
|
if (isAuthenticated && isAuthRoute) {
|
|
return AppRoutes.home;
|
|
}
|
|
|
|
return null;
|
|
},
|
|
routes: [
|
|
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 HomeScreen(),
|
|
),
|
|
GoRoute(
|
|
path: AppRoutes.establishment,
|
|
builder: (context, state) {
|
|
final uuid = state.pathParameters['uuid']!;
|
|
return EstablishmentDetailScreen(establishmentUuid: uuid);
|
|
},
|
|
),
|
|
GoRoute(
|
|
path: AppRoutes.wallet,
|
|
builder: (context, state) => const WalletScreen(),
|
|
),
|
|
GoRoute(
|
|
path: AppRoutes.bookings,
|
|
builder: (context, state) => const BookingsScreen(),
|
|
),
|
|
GoRoute(
|
|
path: AppRoutes.washes,
|
|
builder: (context, state) => const WashScreen(),
|
|
),
|
|
GoRoute(
|
|
path: AppRoutes.profile,
|
|
builder: (context, state) => const ProfileScreen(),
|
|
),
|
|
],
|
|
);
|
|
});
|
|
|
|
/// Écoute les changements Riverpod pour rafraîchir les redirections GoRouter.
|
|
class GoRouterRefreshStream extends ChangeNotifier {
|
|
GoRouterRefreshStream(this._ref) {
|
|
_ref.listen<AuthState>(authProvider, (_, __) => notifyListeners());
|
|
}
|
|
|
|
final Ref _ref;
|
|
}
|