I want the BottomNavigationBar to persist and the user to be sent to a different screen depending on which index that they have selected and that index highlighted on the BottomNavigationBar.
The way I'm attempting to do this is to change the body value of the Scaffold
according to the currently selected index of the BottomNavigationBar()
. I am able to highlight the selected index of the BottomNavigationBar()
once it is selected but the body is not routing to the appropriate widget.
The current method referenced for onTap:
in the BottomNavBar
correctly highlights the selected index but does not route to the correct widget.
There is an unused method (_switchScreen
) in my BottomNavBar
. Inserting this as the value to onTap:
for the BottomNavBar()
appropriately routes to the correct screen but then I have to have the BottomNavBar
rebuilt on every view and is inaccessible behind the BottomNavBar
which is referenced in the NavModel
(Navigation Model).
Here is my BottomNavBar()
:
import 'package:flutter/material.dart';
import '../screens/Add_Media_Screen.dart';
import '../screens/Profile_Screen.dart';
import '../screens/Venture_Feed_Screen.dart';
class BottomNavBar extends StatefulWidget {
@override
_BottomNavBarState createState() => _BottomNavBarState();
}
class _BottomNavBarState extends State<BottomNavBar> {
int _selectedIndex = 0;
void _onItemTapped(int index) {
setState(() {
_selectedIndex = index;
});
}
void _switchScreen(index) {
switch (index) {
case 0:
Navigator.pushNamed(context, VentureFeedScreen.routeName);
break;
case 1:
Navigator.pushNamed(context, ProfileScreen.routeName);
break;
case 2:
Navigator.pushNamed(context, AddMediaScreen.routeName);
break;
}
}
@override
Widget build(BuildContext context) {
return Theme(
data: ThemeData(
primaryColor: Color(0xff84d1da),
accentColor: Color(0xff62c2a2),
),
child: BottomNavigationBar(
selectedItemColor: Color(0xff84d1da),
unselectedItemColor: Colors.grey,
type: BottomNavigationBarType.fixed,
items: const <BottomNavigationBarItem>[
//Index 0
BottomNavigationBarItem(
icon: Icon(
Icons.explore,
),
label: 'Discover',
),
//Index 1
BottomNavigationBarItem(
icon: Icon(
Icons.account_circle,
// color: Color(0xff84d1da),
),
label: 'Profile',
// backgroundColor: Color(0xff84d1da),
),
//Index 2
BottomNavigationBarItem(
icon: Icon(
Icons.add_circle_outline,
),
label: 'Add Media',
),
],
currentIndex: _selectedIndex,
onTap: _onItemTapped,
),
);
}
}
Here is the NavModel
:
import 'package:flutter/material.dart';
import '../screens/Profile_Screen.dart';
import '../screens/Venture_Feed_Screen.dart';
import '../screens/Add_Media_Screen.dart';
import '../widgets/BottomNavBar.dart';
class NavModel extends StatefulWidget {
static const routeName = '/nav-model';
@override
_NavModelState createState() => _NavModelState();
}
class _NavModelState extends State<NavModel> {
int _currentIndex = 0;
final tabs = [
VentureFeedScreen(),
ProfileScreen(),
AddMediaScreen(),
];
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
body: tabs[_currentIndex],
bottomNavigationBar: BottomNavBar(),
),
);
}
}