繁体   English   中英

如何在 Flutter 中为 BottomNavigationBarItem 添加徽章?

[英]How to add badge to BottomNavigationBarItem in Flutter?

我有一个底部导航栏

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

我想为 MyCart 图标添加徽章,我看到 Stack 被用于 BottomNavigationBar 的图标,如下所示:

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

但是当我使用它时,我收到此错误:

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

在声明BottomNavigationBar内的项目之前删除const关键字

MyCart的类型是<Widget>并且您将BottomNavigationBar上的items属性设置为List<BottomNavigationBarItem>将其设置为List<Widget>不要将其设置为List<dynamic>因为所有孩子都必须是 flutter widgets.If你这样做并再次调用MyCart()你将显示到以下小部件树:

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

可能是其他解决方案

您不需要使用 new/const 等。请参阅下面的代码...

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM