diff --git a/lib/screens/pages_layout.dart b/lib/screens/pages_layout.dart index a667d1f..6b3e096 100644 --- a/lib/screens/pages_layout.dart +++ b/lib/screens/pages_layout.dart @@ -5,38 +5,47 @@ import 'package:reciper/screens/settings.dart'; import 'package:reciper/widgets/bottom_nav_bar.dart'; class PagesLayout extends StatefulWidget { - final Widget? child; + final Widget child; final bool displayBottomNavBar; - final int currentSection; + final int? currentSection; - const PagesLayout( - {super.key, - this.child, - this.displayBottomNavBar = true, - this.currentSection = 0}); + const PagesLayout({ + Key? key, + required this.child, + this.displayBottomNavBar = true, + this.currentSection, + }) : super(key: key); @override State createState() => _PagesLayoutState(); } class _PagesLayoutState extends State { + late int currentPageIndex; + late Widget currentChild; + List pages = [ + const HomePage(), + const RecipeEditorPage(), + const Settings() + ]; + + @override + void initState() { + super.initState(); + currentPageIndex = widget.currentSection ?? 0; + currentChild = widget.child; + } + @override Widget build(BuildContext context) { return Scaffold( - body: widget.child, + body: currentChild, bottomNavigationBar: widget.displayBottomNavBar ? BottomNavBar( - selectedIndex: widget.currentSection, + selectedIndex: currentPageIndex, onDestinationSelected: (index) => setState(() { - Navigator.of(context).push(MaterialPageRoute( - builder: (context) => PagesLayout( - currentSection: index, - child: [ - const HomePage(), - const RecipeEditorPage(), - const Settings(), - ][index]), - )); + currentPageIndex = index; + currentChild = pages[currentPageIndex]; }), ) : null,