Widgets 11 | BottomNavigationBar
In this tutorial, you will learn how to use the BottomNavigationBar.
Before Start
We have simply used the BottomNavigationBar
before, So in this tutorial, let's look at its detail. This widget usually used with the parameter bottomNavigationBar
of the Scaffold
. It shows at the bottom of the screen. Let's create a page to show our widget first.
It will show the picture as below, we will replace the Container to our BottomNavigationBar
widget.
Simple Use
You can use the bottomNavigationBar
as a common widget, but usually, we suggest you use the BottomNavigationBar
that provided by the flutter. So Let's look at an example.
If you click this, it isn't change the page, I will do that late. Let's look at the constructor first.
Constructor
The constructor is so important to use a widget, so before using it, you should watch them.
The items
is our children buttons, the onTap
is a function that when you click the item, it will be called. The currentIndex
set the current bottom button. Others I will explain them one by one.
items
It is a List, it defines as below.
It is a BottomNavigationBarItem
list, so we can see the constructor of the BottomNavigationBarItem
for more detail.
The icon
is the icon of the item, it is a widget, but usually we should use the Icon
widget. The title
is also a widget, but usually we use a Text
. The activeIcon
is a widget too. If your item is current item, then the activeIcon
will show. If you don't set it or set to null, it will show the icon
. The backgroundColor
is also easy to understand,it will set the background color of the bottom bar. But you should care about that it has the effect only when you set the type: BottomNavigationBarType.shifting
in the BottomNavigationBar
. Let's look at an example.
The result shows in the picture.
You can see that the background color doesn't show as we want to. Because the default type is BottomNavigationBarType.fixed
, so you can see change the type to shifting
to see the effect .
The result is below. But the titles of other items are hidden, so make sure that is your requirements.
onTap & currentIndex
When we click the items above, it doesn't change anything, but we want to change some contents. So these two parameters are very helpful for this. First, we define a variable in the _BottomNavBarState
, var _curIndex = 0;
, then use it in the BottomNavigationBar
.
Also, we should define a content variable in _BottomNavBarState
class, var contents = "Home";
. Use it in the Scaffold
.
Then set the onTap
as follows.
The whole code about the BottomNavigationBar
as below.
Then you will get the result as below.
fixedColor
If you do not set a color to the icon or title, it will show this color of the parameter. Let's just change the first item as follows.
And change the fixedColor
to PURPLE
, you can get the below result. But you should care about that if the type is BottomNavigationBarType.shifting
, it hasn't the effect.
iconSize
If you do not set the size of the item, you can set it with the parameter of the iconSize
. Now I set the iconSize = 50
and change the first item as below.
Then it will show like this. The icon becomes larger and the title uses default size. But I recommend you just custom your own icon and title, don't use this one.
Conclusion
Today, we have learned how to use the BottomNavigationBar
in the flutter. Its style is so common in our app. The flutter makes it easy to implement this function. So keep learning, you will be good at developing with the flutter.
Thanks for your reading!
The end.
Whole code in GitHub,star to support.
Last updated