import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:go_router/go_router.dart'; import 'package:worldhopper/providers/navigation_provider.dart'; /// Shell widget that provides the bottom navigation bar /// and handles tab switching for the main app sections class MainShellScreen extends ConsumerStatefulWidget { final StatefulNavigationShell navigationShell; const MainShellScreen({ super.key, required this.navigationShell, }); @override ConsumerState createState() => _MainShellScreenState(); } class _MainShellScreenState extends ConsumerState { bool _initialized = false; @override void didChangeDependencies() { super.didChangeDependencies(); if (!_initialized) { _initialized = true; // Load saved tab index and navigate to it final savedIndex = ref.read(navigationStateProvider); if (savedIndex != widget.navigationShell.currentIndex) { // Delay navigation to allow widget tree to build WidgetsBinding.instance.addPostFrameCallback((_) { widget.navigationShell.goBranch(savedIndex, initialLocation: true); }); } } } void _onDestinationSelected(int index) { // Persist the tab change ref.read(navigationStateProvider.notifier).setTabIndex(index); // Navigate to the selected tab widget.navigationShell.goBranch( index, initialLocation: index == widget.navigationShell.currentIndex, ); } @override Widget build(BuildContext context) { return Scaffold( body: widget.navigationShell, bottomNavigationBar: NavigationBar( selectedIndex: widget.navigationShell.currentIndex, onDestinationSelected: _onDestinationSelected, destinations: const [ NavigationDestination( icon: Icon(Icons.auto_stories_outlined), selectedIcon: Icon(Icons.auto_stories), label: 'Library', ), NavigationDestination( icon: Icon(Icons.dns_outlined), selectedIcon: Icon(Icons.dns), label: 'Servers', ), NavigationDestination( icon: Icon(Icons.settings_outlined), selectedIcon: Icon(Icons.settings), label: 'Settings', ), ], ), ); } }