Is there a way to show the BottomNavigationBar on every page? Currently the BottomNavigationBar disappears when pushing the button but I want the BottomNavigationBar to always be displayed even when routing to a new page. The following code shows my BottomNavigationBar and my detail page.
class BottomNavBar extends StatefulWidget {
const BottomNavBar({Key? key}) : super(key: key);
@override
State<BottomNavBar> createState() => _BottomNavBarState();
}
class _BottomNavBarState extends State<BottomNavBar> {
int currentIndex = 0;
final screens = const [Home(), Search()];
@override
Widget build(BuildContext context) {
return Scaffold(
body: screens[currentIndex],
bottomNavigationBar: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
backgroundColor: bottomNav,
unselectedItemColor: Colors.grey,
selectedFontSize: 12,
unselectedFontSize: 12,
currentIndex: currentIndex,
onTap: (index) => setState(() => currentIndex = index),
items: const [
BottomNavigationBarItem(
icon: Icon(
Icons.home_filled,
size: 28,
),
label: 'Home'),
BottomNavigationBarItem(
icon: Icon(
Icons.search_rounded,
size: 28,
),
label: 'Search'),
]));
}
}
My detail page:
class Home extends StatelessWidget {
const Home({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: IconButton(
icon: const Icon(Icons.abc),
onPressed: () {
Navigator.push(
context, MaterialPageRoute(builder: (context) => const Page1()));
},
)),
);
}
}
class Page1 extends StatelessWidget {
const Page1({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
color: Colors.red,
);
}
}
You can use a Navigator widget to achieve this. Check the following code: Each NavigatorView creates its own Navigator instance. This allows each tab to manage its navigation stack independently while keeping the BottomNavigationBar in place. Inside each PrimaryView, pages are pushed onto the stack of its corresponding Navigator. This means navigating to new pages doesn’t affect the visibility of the BottomNavigationBar.
Code example: