How to add badge to BottomNavigationBarItem in Flutter?

Issue

I have a BottomNavigationBar

BottomNavigationBar(
        items: const <BottomNavigationBarItem>[
          BottomNavigationBarItem(
            icon: Icon(Icons.shopping_cart),
            label: 'MyCart',
          ),
          .
          .
          .
          ])

I want add badge to MyCart icon, i saw Stack was used for BottomNavigationBar’s icon like this:

new BottomNavigationBarItem(
        title: new Text('Home'),
        icon: new Stack(
          children: <Widget>[
            new Icon(Icons.home),
            new Positioned(  // draw a red marble
              top: 0.0,
              right: 0.0,
              child: new Icon(Icons.brightness_1, size: 8.0, 
                color: Colors.redAccent),
            )
          ]
        ),
      )

but when I use it I get this error:

The values in a const list literal must be constants.
Try removing the keyword 'const' from the list literal.

Solution

Remove the const keyword before declaring the items inside BottomNavigationBar

Answered By – Stale Noobs

Answer Checked By – David Marino (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *