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
.
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.
Badges can be created using Badge() widget.
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.
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 .
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"), )
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.
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.
By default it's below the red badge. Usually Icon, IconButton, Text or button. Shadow of the badge. Gradient color for the badge content.
You can also use badges
package, a picture from its page :
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,
),
)),
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