Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a badge count number as part of a BottomNavigationBarItem in flutter

I am writing a flutter app which updates the number of notifications in the BottomNavigationBar.

I used a bottom navigation library(AHBottomNavigation) to accomplish the same goal in native android(java) but I cant seem to find my way around it using flutter.

 items: <BottomNavigationBarItem>[
                  BottomNavigationBarItem(
                      title: Text('Home'), icon: Icon(Icons.home)),
                  BottomNavigationBarItem(
                      title: Text('Friends'), icon:Icon(Icons.notifications)),
                  BottomNavigationBarItem(
                      title: Text('Settings'), icon: Icon(Icons.settings)),
                ],

I want to get what is in label 2 with the 4 attached to the BottomNavigationBarItem.

enter image description here

like image 439
EngineerDanny Avatar asked Sep 06 '19 23:09

EngineerDanny


People also ask

How do you add notification counts in flutter?

Create an IconButton inside Stack and use Positioned widget to set the child of Positioned widget which will a be Text widget over the IconButton . The Text widget will be used to show the count of notifications.

How do you add a badge to the bottom navigation bar in flutter?

Badges can be created using Badge() widget.

How do you add badges in flutter?

Here's an example of how you can add the Badge widget to your project: First, drag the Badge widget from the Base Elements tab and carefully drop it into the Actions section of the AppBar. Now, add the IconButton widget inside the Badge widget. Customize the Icon and its color as per your requirement.

How do you change badge size in flutter?

You can use size property for Icon . For normal Icon you can use size but for IconButton with your own images you should use scale .

How to add a counter badge in flutter?

The counter badge is very necessary on cart buttons, inbox buttons, orders like UI. See the example below for more details: First, add badges Flutter package in your project by adding the following lines in pubspec.yaml file: How to Show Badge Counter on Icon? Badge( child: Icon(Icons.shopping_cart), badgeContent: Text("3"), )

How to display notification badge on bottom navigation bar’s icon?

To Display a notification badge on Bottom Navigation Bar’s icon follows the below code snippet. We will get output like the below: It can be done by stacking two icons using the Stack Widget and Positioned Widget.

How to change the location of badge in widget?

You can use the position property of Badge () widget to change the location of the badge. The output with the above position will look like below: How to Apply Badge on Other Widgets? You can show badges on any kind of widget, you just need to pass your widget on the child property of Badge.

Where do I put the badge content?

By default it's below the red badge. Usually Icon, IconButton, Text or button. Shadow of the badge. Gradient color for the badge content.


1 Answers

You can also use badges package, a picture from its page :

package example image

And then provide it as icon to your BottomNavigationBarItem :

BottomNavigationBarItem(
  icon: BadgeIconButton(
                        itemCount: 5, // required
                        icon: Icon(
                          Icons.monetization_on,
                          color:
                              _selectedIndex == 2 ? Colors.blue : Colors.grey,
                        ), // required
                        badgeColor: Colors.red, // default: Colors.red
                        badgeTextColor: Colors.white, // default: Colors.white
                        hideZeroCount: true, // default: true
                        onPressed: null),
                    title: Text(
                      'Item',
                      style: TextStyle(
                        color: _selectedIndex == 2 ? Colors.blue : Colors.grey,
                      ),
                    )),
like image 71
Mazin Ibrahim Avatar answered Sep 24 '22 00:09

Mazin Ibrahim