Baran Gungor
Baran Gungor

Reputation: 215

How to add badge to BottomNavigationBarItem in Flutter?

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.

Upvotes: 0

Views: 3404

Answers (3)

dGoran
dGoran

Reputation: 883

you do not need to use new/const, etc. see code below...

 bottomNavigationBar: BottomNavigationBar(items: [
          BottomNavigationBarItem(
            label: 'aaaaaa',
            icon: Stack(children: <Widget>[
              Icon(Icons.home),
              Positioned(
                // draw a red marble
                top: 0.0,
                right: 0.0,
                child: Icon(Icons.brightness_1, size: 8.0, color: Colors.redAccent),
              )
            ]),
          ),
          BottomNavigationBarItem(
            label: 'dddddd',
            icon: Stack(children: <Widget>[
              Icon(Icons.home),
              Positioned(
                // draw a red marble
                top: 0.0,
                right: 0.0,
                child: Icon(Icons.brightness_1, size: 8.0, color: Colors.redAccent),
              )
            ]),
          )
        ]),

Upvotes: 0

Tech Helper
Tech Helper

Reputation: 51

The type of MyCart is <Widget> and you set the items property on BottomNavigationBar to type List<BottomNavigationBarItem> set it to List<Widget>.Don't set it to List<dynamic> because all the children must be flutter widgets.If you do that and again call MyCart() you will display to following Widget tree:

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),
            )
          ]
        ),
      )

could be other solutions

Upvotes: 0

Stale Noobs
Stale Noobs

Reputation: 130

Remove the const keyword before declaring the items inside BottomNavigationBar

Upvotes: 3

Related Questions