#first commit
This commit is contained in:
33
lib/core/router/app_router.dart
Normal file
33
lib/core/router/app_router.dart
Normal file
@@ -0,0 +1,33 @@
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:maibu_satabot_v2/core/router/route_paths.dart';
|
||||
|
||||
import '../../features/auth/presentation/bloc/auth_cubit.dart';
|
||||
import '../../features/auth/presentation/bloc/auth_state.dart';
|
||||
import '../../features/auth/presentation/pages/login_page.dart';
|
||||
import '../../features/home/presentation/pages/home_page.dart';
|
||||
import '../../features/register/presentation/pages/register_page.dart';
|
||||
import 'go_router_refresh_stream.dart';
|
||||
|
||||
GoRouter createRouter(AuthCubit authCubit) {
|
||||
return GoRouter(
|
||||
initialLocation: RoutePaths.login,
|
||||
refreshListenable: GoRouterRefreshStream(authCubit.stream),
|
||||
redirect: (context, state) {
|
||||
final loggedIn = authCubit.state is AuthAuthenticated;
|
||||
final loggingIn = state.matchedLocation == RoutePaths.login;
|
||||
|
||||
if (!loggedIn && !loggingIn) {
|
||||
return RoutePaths.login;
|
||||
}
|
||||
if (loggedIn && loggingIn) {
|
||||
return RoutePaths.home;
|
||||
}
|
||||
return null;
|
||||
},
|
||||
routes: [
|
||||
GoRoute(path: RoutePaths.login, builder: (_, __) => LoginPage()),
|
||||
GoRoute(path: RoutePaths.register, builder: (_, __) => RegisterPage()),
|
||||
GoRoute(path: RoutePaths.home, builder: (context, state) => HomePage()),
|
||||
],
|
||||
);
|
||||
}
|
||||
19
lib/core/router/go_router_refresh_stream.dart
Normal file
19
lib/core/router/go_router_refresh_stream.dart
Normal file
@@ -0,0 +1,19 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class GoRouterRefreshStream extends ChangeNotifier {
|
||||
late final StreamSubscription _subscription;
|
||||
|
||||
GoRouterRefreshStream(Stream<dynamic> stream) {
|
||||
_subscription = stream.listen((_) {
|
||||
notifyListeners();
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_subscription.cancel();
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
5
lib/core/router/route_paths.dart
Normal file
5
lib/core/router/route_paths.dart
Normal file
@@ -0,0 +1,5 @@
|
||||
class RoutePaths {
|
||||
static const login = '/auth';
|
||||
static const register = '/register';
|
||||
static const home = '/home';
|
||||
}
|
||||
Reference in New Issue
Block a user