dart - 如何在 BottomNavigationBarItem 中绝对定位徽章

标签 dart flutter

我正在使用 BottomNavigationBarItem 来显示带有图标和文本的底部导航 UI。我希望能够在这些徽章上添加带有数值的徽章。这是我目前的尝试:

  bottomNavigationBar:
      new BottomNavigationBar(items: <BottomNavigationBarItem>[
        new BottomNavigationBarItem(
        icon: new Stack(
          children: <Widget>[
            const Icon(Icons.home),
            new Positioned(
                top: 0.0,
                left: 0.0,
                child: new Container(
                  decoration: new BoxDecoration(
                      borderRadius: new BorderRadius.circular(4.0),
                      color: Colors.red),
                  width: 16.0,
                  child: new Text(
                    "12",
                    style: const TextStyle(color: Colors.white),
                  ),
                ))
          ],
        ),
        title: new Text("Home")),
        new BottomNavigationBarItem(
        icon: const Icon(Icons.star), title: new Text("Star")),
      ],
      type: BottomNavigationBarType.fixed),

但是,徽章位于图标的边界框内,因此它与图标严重重叠:

enter image description here

相反,我想要的是这样的东西:

enter image description here

是否可以使用 BottomNavigationBarItem 小部件实现此目的?如果不是,什么是好的解决方法?

最佳答案

确保徽章位置正确,溢出的 child 应该是可见的

icon: new Stack(
              overflow: Overflow.visible,
              children: <Widget>[
                const Icon(Icons.home),
                new Positioned(
                    top: -1.0,
                    right: -6.0,
                    child: new Container(
                      decoration: new BoxDecoration(
                          borderRadius: new BorderRadius.circular(4.0), color: Colors.red),
                      width: 16.0,
                      child: new Text(
                        "12",
                        style: const TextStyle(color: Colors.white),
                      ),
                    ))
              ],
            ),

enter image description here

关于dart - 如何在 BottomNavigationBarItem 中绝对定位徽章,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50185502/

相关文章:

flutter - 周期性定时器内的“事件处理程序正常完成后调用发射”问题

flutter - 为什么在 StatefulWidget 中没有定义 build 方法?

dom - 在Dart中查询表格中的单元格

dart - 扩展列以适合屏幕宽度

eclipse - 调试Dart应用程序时变量的唯一ID(哈希?)

python - 是否可以使用Flutter中的remove.bg API开发应用程序?

flutter - Future builder 没有获取数据

flutter - Dart,If else 语句,有没有更好的方法来选择下一个 Map 项?

plugins - device_calendar 0.1.0 插件,用于日历访问的 flutter 限制权限

sorting - 按两个属性分别对List <object>进行排序,其中一个属性以升序排列,另一个属性以降序排列