90 lines
2.4 KiB
Dart
90 lines
2.4 KiB
Dart
import 'dart:async';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
import '../../features/auth/bloc/auth_bloc.dart';
|
|
import '../../shared/widgets/pred_bottom_nav.dart';
|
|
|
|
/// Main authenticated shell with bottom navigation.
|
|
class AppShell extends StatelessWidget {
|
|
const AppShell({super.key, required this.state, required this.child});
|
|
|
|
final GoRouterState state;
|
|
final Widget child;
|
|
|
|
int _selectedIndex(AuthState auth) {
|
|
final path = state.uri.path;
|
|
if (path == '/predict' || path.startsWith('/predict/')) return 1;
|
|
if (path.startsWith('/deposit') ||
|
|
path == '/withdraw' ||
|
|
path == '/transfer' ||
|
|
path == '/transactions' ||
|
|
path == '/wallet') {
|
|
return 2;
|
|
}
|
|
if ( path == '/account' ||
|
|
path == '/totp-setup' ||
|
|
path.startsWith('/referral')) {
|
|
return 3;
|
|
}
|
|
if (path == '/notifications') return 0;
|
|
if (path.startsWith('/options') || path.startsWith('/admin/')) {
|
|
return auth.isAdmin ? 4 : 0;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
void _onTap(BuildContext context, int index, bool isAdmin) {
|
|
switch (index) {
|
|
case 0:
|
|
context.go('/home');
|
|
case 1:
|
|
context.go('/predict');
|
|
case 2:
|
|
context.go('/wallet');
|
|
case 3:
|
|
context.go('/account');
|
|
case 4:
|
|
if (isAdmin) context.go('/options');
|
|
default:
|
|
context.go('/home');
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return BlocBuilder<AuthBloc, AuthState>(
|
|
builder: (context, auth) {
|
|
final isAdmin = auth.isAdmin;
|
|
final selected = _selectedIndex(auth);
|
|
return Scaffold(
|
|
body: SafeArea(
|
|
bottom: false,
|
|
child: child,
|
|
),
|
|
bottomNavigationBar: PredBottomNav(
|
|
selectedIndex: isAdmin ? selected : selected.clamp(0, 3),
|
|
showAdmin: isAdmin,
|
|
onTap: (i) => _onTap(context, i, isAdmin),
|
|
),
|
|
);
|
|
},
|
|
);
|
|
}
|
|
}
|
|
|
|
/// Listens to [AuthBloc] stream to refresh go_router redirects.
|
|
class GoRouterRefreshStream extends ChangeNotifier {
|
|
GoRouterRefreshStream(Stream<dynamic> stream) {
|
|
_subscription = stream.listen((_) => notifyListeners());
|
|
}
|
|
|
|
late final StreamSubscription<dynamic> _subscription;
|
|
|
|
@override
|
|
void dispose() {
|
|
_subscription.cancel();
|
|
super.dispose();
|
|
}
|
|
}
|