import 'dart:async'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:go_router/go_router.dart'; import '../../core/di/injection.dart'; import '../../core/network/connectivity_controller.dart'; import '../../features/auth/bloc/auth_bloc.dart'; import '../../shared/widgets/offline_banner.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( builder: (context, auth) { final isAdmin = auth.isAdmin; final selected = _selectedIndex(auth); final connectivity = sl(); // No top SafeArea: tabs paint edge-to-edge (Home gradient / AppBar primary). // OfflineBanner owns status-bar padding when visible; child top inset is // cleared while offline so nested AppBars do not double-pad. return Scaffold( body: Column( children: [ OfflineBanner( controller: connectivity, includeStatusBarPadding: true, ), Expanded( child: ListenableBuilder( listenable: connectivity, builder: (context, _) { final media = MediaQuery.of(context); final top = connectivity.isOnline ? media.padding.top : 0.0; return MediaQuery( data: media.copyWith( padding: media.padding.copyWith(top: top), ), 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 stream) { _subscription = stream.listen((_) => notifyListeners()); } late final StreamSubscription _subscription; @override void dispose() { _subscription.cancel(); super.dispose(); } }