I want to update the current index of the BottomNavigation Bar by ValueNotifier or anything instead of . and I don't need to wrap the bottom bar by Scaffold , because when I did it , the home screen didn't load . And I jsut updated the current index by Push if back button pressed or WillPopScope ,but I need it pop.. to save scroll's position and updated the index . Please if you have a solution drop it here .
class AppBottomNavigation extends StatefulWidget {
const AppBottomNavigation({Key? key}) : super(key: key);
@override
_AppBottomNavigationState createState() => _AppBottomNavigationState();
}
class _AppBottomNavigationState extends State<AppBottomNavigation> {
int _selectedIndex = 0 ;
int _pastIndex = 0 ;
List<Widget> _screens =[
HomeScreen(),
Scaffold(body: Text("No widget yet")),
Scaffold(body: Text("No widget yet")),
Scaffold(body: Text("No widget yet")),
UserProfile()
];
List<dynamic> menuItems = [
{
'icon': 'assets/icons/home.svg',
'label': 'Home',
},
{
'icon': 'assets/icons/box.svg',
'label': 'Delivery',
},
{
'icon': 'assets/icons/chat.svg',
'label': 'Messages',
},
{
'icon': 'assets/icons/wallet.svg',
'label': 'Wallet',
},
{
'icon': 'assets/icons/profile.svg',
'label': 'Profile',
},
];
void _onItemTapped(int index) {
setState(() {
_selectedIndex = index;
});
if (index == 4) {
if(FirebaseAuth.instance.currentUser != null){
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => UserProfile(),
));
}else{
try {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => LoginScreen(),
));
} on Exception catch (e) {
print(e);
}
}
} else {
if(_selectedIndex != _pastIndex){
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => _screens[_selectedIndex],
));
}
}
setState(() {
_pastIndex = _selectedIndex;
});
}
@override
Widget build(BuildContext context) {
return BottomNavigationBar(
backgroundColor: Colors.white,
showUnselectedLabels: true,
unselectedItemColor: Colors.black87,
elevation: 32.0,
type: BottomNavigationBarType.fixed,
selectedLabelStyle: TextStyle(
height: 1.5,
fontSize: 12,
),
unselectedLabelStyle: TextStyle(
height: 1.5,
fontSize: 12,
),
items: menuItems.map((i) {
return BottomNavigationBarItem(
icon: SvgPicture.asset(i['icon']),
activeIcon: SvgPicture.asset(
i['icon'],
color: primaryColor,
),
label: i['label'],
);
}).toList(),
currentIndex: _selectedIndex,
selectedItemColor: MyEditColor,
onTap: _onItemTapped,
);
}
}
Here is the blulid Function of the HomeScreen and the call up of AppBottomNavigation class .
@override
Widget build(BuildContext context) {
super.build(context);
return Scaffold(
bottomNavigationBar: AppBottomNavigation(),
body: SafeArea(
child: SingleChildScrollView(
child: Column(
//....