i have a bottomnavigation bar in flutter ,what i want is on click of the individual widgets of the bottombar it should navigate me to the next page
Widget _bottemTab() {
return new BottomNavigationBar(
currentIndex: _currentIndex,
onTap:(newIndex) => setState((){_currentIndex = newIndex;}),
type: BottomNavigationBarType.fixed,
items: [
new BottomNavigationBarItem(
icon: Image.asset(
"assets/home.png",
width: 24.0,
height: 24.0,
),
title: Text(
'Home',
),
),
new BottomNavigationBarItem(
icon: Image.asset(
"assets/shopping-bagg.png",
width: 24.0,
height: 24.0,
),
title: Text(
'Mall',
)),
new BottomNavigationBarItem(
icon: Image.asset(
"assets/qr-code.png",
width: 24.0,
height: 24.0,
),
title: Text(
'Scan',
)),
new BottomNavigationBarItem(
icon: Image.asset(
"assets/bank.png",
width: 24.0,
height: 24.0,
),
title: Text(
'Bank',
)),
new BottomNavigationBarItem(
icon: Image.asset(
"assets/delivery.png",
width: 24.0,
height: 24.0,
),
title: Text(
'Inbox',
)),
]);
}
i want is on click of the individual widgets of the bottombar it should navigate me to the next page which i have created separately for each menu items of bottom navigation view..any help is appreciated
In Flutter application, we usually set the bottom navigation bar in conjunction with the scaffold widget. Scaffold widget provides a Scaffold.bottomNavigationBar argument to set the bottom navigation bar. It is to note that only adding BottomNavigationBar will not display the navigation items.
Let’s get started. Once you get your basic flutter app up and running, your material app assigns the HomePage () class to the home attribute. Now create a stateful widget and name it as HomePage. Return a Scaffold from the HomePage class. Now this Scaffold is the main element that contains our bottom navigation bar.
A material widget that’s displayed at the bottom of an app for selecting among a small number of views, typically… What is not working? According to point 2, the Bottom Navigation Bar should remain unchanged but it does not.
A material widget that’s displayed at the bottom of an app for selecting among a small number of views, typically… What is not working? According to point 2, the Bottom Navigation Bar should remain unchanged but it does not. We also need to update the MainScreen to use the CommonBottomNavigationBar Widget-
In your build()
method, you can add this logic.
@override
Widget build(BuildContext context) {
Widget widget = Container(); // default
switch (_currentIndex) {
case 0:
widget = FirstPage();
break;
case 1:
widget = SecondPage();
break;
case 2:
widget = ThirdPage();
break;
}
return Scaffold(
body: widget,
// ...
);
}
Update:
I'm not sure how you did that, here you can see a small example of doing it right way.
int _index = 0;
@override
Widget build(BuildContext context) {
Widget child = Container();
switch(_index) {
case 0:
child = FlutterLogo(colors: Colors.orange);
break;
case 1:
child = FlutterLogo();
break;
}
return Scaffold(
appBar: AppBar(),
bottomNavigationBar: _bottomTab(),
body: SizedBox.expand(child: child),
);
}
Widget _bottomTab() {
return BottomNavigationBar(
currentIndex: _index,
onTap: (int index) => setState(() => _index = index),
backgroundColor: Colors.deepPurple,
items: [
BottomNavigationBarItem(icon: Icon(Icons.looks_one), title: Text("1")),
BottomNavigationBarItem(icon: Icon(Icons.looks_two), title: Text("2")),
],
);
}
Hope this helps.
int _selectedIndex = 0;
static const TextStyle optionStyle = TextStyle(fontSize: 30, fontWeight: FontWeight.bold);
static const _pages = <Widget>[
Home(),//this is a stateful widget on a separate file
Business(),//this is a stateful widget on a separate file
School(),//this is a stateful widget on a separate file
];
void _onItemTapped(int index) {
setState(() {
_selectedIndex = index;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('BottomNavigationBar Sample'),
),
body: Center(
child: _pages.elementAt(_selectedIndex),
),
bottomNavigationBar: BottomNavigationBar(
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.home),
title: Text('Home'),
),
BottomNavigationBarItem(
icon: Icon(Icons.business),
title: Text('Business'),
),
BottomNavigationBarItem(
icon: Icon(Icons.school),
title: Text('School'),
),
],
currentIndex: _selectedIndex,
selectedItemColor: Colors.amber[800],
onTap: _onItemTapped,
),
);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With