games_app/prediqo_mobile/lib/app_router.dart
2026-06-29 15:01:23 +02:00

213 lines
7.9 KiB
Dart

/// Application navigation configuration using go_router.
///
/// Defines auth, wallet, account, and admin routes with shell-based dashboard layout.
library;
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:go_router/go_router.dart';
import 'data/repositories/admin_repository_impl.dart';
import 'features/admin/presentation/options_screen.dart';
import 'features/admin/presentation/settings_screen.dart';
import 'features/admin/presentation/user_detail_screen.dart';
import 'features/admin/presentation/user_management_screen.dart';
import 'features/auth/bloc/auth_bloc.dart';
import 'features/auth/presentation/login_screen.dart';
import 'features/auth/presentation/register_screen.dart';
import 'features/auth/presentation/totp_setup_screen.dart';
import 'features/wallet/presentation/dashboard_screen.dart';
import 'features/wallet/presentation/dashboard_shell.dart';
import 'features/wallet/presentation/deposit_asset_select_screen.dart';
import 'features/wallet/presentation/deposit_screen.dart';
import 'features/wallet/presentation/transaction_history_screen.dart';
import 'features/wallet/presentation/withdraw_screen.dart';
import 'features/wallet/presentation/transfer_screen.dart';
import 'features/wallet/presentation/account_screen.dart';
import 'features/wallet/presentation/profile_edit_screen.dart';
import 'features/wallet/presentation/change_password_screen.dart';
final _rootNavigatorKey = GlobalKey<NavigatorState>();
/// Builds the app's [GoRouter] with auth-gated and admin-protected routes.
GoRouter createAppRouter() {
return GoRouter(
navigatorKey: _rootNavigatorKey,
initialLocation: '/',
routes: [
GoRoute(path: '/', builder: (context, __) => const _SplashRedirect()),
GoRoute(path: '/login', builder: (_, __) => const LoginScreen()),
GoRoute(path: '/register', builder: (_, __) => const RegisterScreen()),
ShellRoute(
builder: (context, state, child) =>
DashboardShell(state: state, child: child),
routes: [
GoRoute(
path: '/dashboard',
builder: (_, state) {
final tab =
int.tryParse(state.uri.queryParameters['tab'] ?? '0') ?? 0;
return DashboardScreen(initialTab: tab);
},
),
GoRoute(
path: '/deposit',
builder: (_, __) => const DepositAssetSelectScreen(),
),
GoRoute(
path: '/deposit/:walletType',
builder: (c, state) {
final wt = state.pathParameters['walletType']!;
return DepositScreen(walletTypeCode: wt);
},
),
GoRoute(
path: '/withdraw',
builder: (_, __) => const WithdrawScreen(),
),
GoRoute(
path: '/transfer',
builder: (_, __) => const TransferScreen(),
),
GoRoute(
path: '/transactions',
builder: (_, __) => const TransactionHistoryScreen(),
),
GoRoute(
path: '/totp-setup',
builder: (_, __) => const TotpSetupScreen(),
),
GoRoute(path: '/account', builder: (_, __) => const AccountScreen()),
GoRoute(
path: '/account/profile',
builder: (_, __) => const ProfileEditScreen(),
),
GoRoute(
path: '/account/change-password',
builder: (_, __) => const ChangePasswordScreen(),
),
GoRoute(path: '/options', builder: (_, __) => const OptionsScreen()),
GoRoute(
path: '/options/settings',
builder: (context, __) {
final adminRepo = context.read<AdminRepositoryImpl>();
final isAdmin = context.read<AuthBloc>().state.isAdmin;
if (!isAdmin) {
return Scaffold(
appBar: AppBar(title: const Text('Settings')),
body: const Center(child: Text('Access denied. Admin only.')),
);
}
return SettingsScreen(adminRepo: adminRepo);
},
),
GoRoute(
path: '/admin/users',
builder: (context, __) {
final adminRepo = context.read<AdminRepositoryImpl>();
final isAdmin = context.read<AuthBloc>().state.isAdmin;
if (!isAdmin) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(
Icons.block_rounded,
size: 48,
color: Colors.grey,
),
const SizedBox(height: 16),
const Text('Access denied. Admin only.'),
const SizedBox(height: 16),
FilledButton(
onPressed: () => context.go('/dashboard'),
child: const Text('Back to Dashboard'),
),
],
),
),
);
}
return UserManagementScreen(adminRepo: adminRepo);
},
),
GoRoute(
path: '/admin/users/:userId',
builder: (context, state) {
final userId = state.pathParameters['userId']!;
final adminRepo = context.read<AdminRepositoryImpl>();
final isAdmin = context.read<AuthBloc>().state.isAdmin;
if (!isAdmin) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(
Icons.block_rounded,
size: 48,
color: Colors.grey,
),
const SizedBox(height: 16),
const Text('Access denied. Admin only.'),
const SizedBox(height: 16),
FilledButton(
onPressed: () => context.go('/dashboard'),
child: const Text('Back to Dashboard'),
),
],
),
),
);
}
return UserDetailScreen(userId: userId, adminRepo: adminRepo);
},
),
],
),
],
);
}
/// Initial route that redirects to login or dashboard based on auth state.
class _SplashRedirect extends StatefulWidget {
const _SplashRedirect();
@override
State<_SplashRedirect> createState() => _SplashRedirectState();
}
class _SplashRedirectState extends State<_SplashRedirect> {
@override
Widget build(BuildContext context) {
return BlocListener<AuthBloc, AuthState>(
listenWhen: (prev, curr) => prev.status != curr.status,
listener: (context, state) {
if (state.status == AuthStatus.loading) return;
if (!context.mounted) return;
if (state.status == AuthStatus.authenticated) {
context.go('/dashboard');
} else {
context.go('/login');
}
},
child: const Scaffold(body: Center(child: CircularProgressIndicator())),
);
}
/// Performs an immediate redirect when auth state is already resolved.
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) {
if (!mounted) return;
final state = context.read<AuthBloc>().state;
if (state.status == AuthStatus.authenticated) {
context.go('/dashboard');
} else if (state.status != AuthStatus.loading) {
context.go('/login');
}
});
}
}