Created
March 22, 2022 22:05
-
-
Save lucas404x/9f85e44c96db2bfd34675a1ac6c567ca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import '../home/home_page.dart'; | |
import 'widgets/bottom_navigator.dart'; | |
class RootPage extends StatefulWidget { | |
const RootPage({Key? key}) : super(key: key); | |
@override | |
State<RootPage> createState() => _RootPageState(); | |
} | |
class _RootPageState extends State<RootPage> { | |
late final GlobalKey<NavigatorState> _pageNavigatorKey; | |
late final List<String> _pages; | |
late int _currentIndex; | |
@override | |
void initState() { | |
_pageNavigatorKey = GlobalKey(); | |
_pages = [ | |
HomePage.router, | |
'', | |
'', | |
'', | |
]; | |
_currentIndex = 0; | |
super.initState(); | |
} | |
@override | |
void dispose() { | |
_pageNavigatorKey.currentState?.dispose(); | |
_pages.clear(); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
body: Navigator( | |
initialRoute: HomePage.router, | |
key: _pageNavigatorKey, | |
onGenerateRoute: (settings) { | |
int pageIndex = _pages.indexOf(settings.name ?? ''); | |
if (pageIndex > -1 && pageIndex != _currentIndex) { | |
setState(() { | |
_currentIndex = pageIndex; | |
}); | |
} | |
late Widget _currentPage; | |
switch (settings.name) { | |
case HomePage.router: | |
_currentPage = const HomePage(); | |
break; | |
default: | |
_currentPage = Container(); | |
} | |
return MaterialPageRoute(builder: (context) => _currentPage); | |
}, | |
), | |
bottomNavigationBar: BottomNavigator( | |
currentIndex: _currentIndex, | |
onItemPressed: (index) { | |
setState(() { | |
_currentIndex = index; | |
_pageNavigatorKey.currentState?.pushNamed(_pages[index]); | |
}); | |
}, | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment