100 lines
3.0 KiB
Dart
100 lines
3.0 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'bottom_nav.dart';
|
|
import '../screens/library_screen.dart';
|
|
import '../screens/categories_screen.dart';
|
|
|
|
/// Shell widget with bottom navigation and nested navigators for each tab.
|
|
/// Uses IndexedStack to preserve navigation state when switching tabs.
|
|
class BottomNavShell extends StatefulWidget {
|
|
const BottomNavShell({super.key});
|
|
|
|
@override
|
|
State<BottomNavShell> createState() => _BottomNavShellState();
|
|
}
|
|
|
|
class _BottomNavShellState extends State<BottomNavShell> {
|
|
int _currentIndex = 0;
|
|
|
|
// Each tab gets its own navigator key to maintain independent navigation stacks
|
|
final _navigatorKeys = List.generate(4, (_) => GlobalKey<NavigatorState>());
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return PopScope(
|
|
canPop: false,
|
|
onPopInvokedWithResult: (didPop, result) async {
|
|
if (didPop) return;
|
|
final shouldPop = await _onWillPop();
|
|
if (shouldPop && context.mounted) {
|
|
Navigator.of(context).pop();
|
|
}
|
|
},
|
|
child: Scaffold(
|
|
body: IndexedStack(
|
|
index: _currentIndex,
|
|
children: [
|
|
_buildNavigator(0, (_) => const LibraryScreen()),
|
|
_buildNavigator(1, (_) => const CategoriesScreen()),
|
|
_buildNavigator(2, (_) => _buildPlaceholder('Избранное')),
|
|
_buildNavigator(3, (_) => _buildPlaceholder('Настройки')),
|
|
],
|
|
),
|
|
bottomNavigationBar: BottomNav(
|
|
currentIndex: _currentIndex,
|
|
onTap: _onTabTapped,
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
/// Builds a nested navigator for a tab
|
|
Widget _buildNavigator(int index, WidgetBuilder builder) {
|
|
return Navigator(
|
|
key: _navigatorKeys[index],
|
|
onGenerateRoute: (settings) {
|
|
return MaterialPageRoute(builder: builder, settings: settings);
|
|
},
|
|
);
|
|
}
|
|
|
|
/// Placeholder screen for tabs not yet implemented
|
|
Widget _buildPlaceholder(String title) {
|
|
return Scaffold(
|
|
appBar: AppBar(title: Text(title), automaticallyImplyLeading: false),
|
|
body: Center(
|
|
child: Text(title, style: Theme.of(context).textTheme.headlineMedium),
|
|
),
|
|
);
|
|
}
|
|
|
|
/// Handle tab selection
|
|
void _onTabTapped(int index) {
|
|
if (_currentIndex == index) {
|
|
// If tapping the current tab, pop to root of that tab's navigator
|
|
final navigator = _navigatorKeys[index].currentState;
|
|
if (navigator != null && navigator.canPop()) {
|
|
navigator.popUntil((route) => route.isFirst);
|
|
}
|
|
} else {
|
|
// Switch to the selected tab
|
|
setState(() {
|
|
_currentIndex = index;
|
|
});
|
|
}
|
|
}
|
|
|
|
/// Handle system back button
|
|
Future<bool> _onWillPop() async {
|
|
final navigator = _navigatorKeys[_currentIndex].currentState;
|
|
|
|
// If the current tab's navigator can pop, pop it
|
|
if (navigator != null && navigator.canPop()) {
|
|
navigator.pop();
|
|
return false; // Don't exit app
|
|
}
|
|
|
|
// If on root of current tab, allow app to exit
|
|
return true;
|
|
}
|
|
}
|