Flutter-如何在工具栏中对齐徽章图标?

时间:2019-10-09 05:11:33

标签: flutter dart flutter-layout

我在Flutter应用中使用了badges package。这是我的代码:

appBar: AppBar(
  title: Text("test"),
  actions: <Widget>[
    IconButton(
      icon: Icon(Icons.search),
      onPressed: () {},
    ),
    Badge(
      position: BadgePosition.topRight(top: 0, right: 2),
      animationDuration: Duration(milliseconds: 300),
      animationType: BadgeAnimationType.slide,
      badgeContent: Text(
        '7',
        style: TextStyle(color: Colors.white),
      ),
      child: IconButton(icon: Icon(Icons.shopping_cart), onPressed: () {}),
    ),
  ],
)

这是我的结果:

enter image description here

但是图标未垂直对齐。   我想垂直对齐购物车图标。使其与标题和搜索图标处于同一级别。

enter image description here

我该如何解决?

2 个答案:

答案 0 :(得分:1)

更早的

enter image description here

现在:

enter image description here

只需将Badge包裹在AlignCenter中即可。

Align(
  child: Badge(
    position: BadgePosition.topRight(top: 0, right: 2),
    animationDuration: Duration(milliseconds: 300),
    animationType: BadgeAnimationType.slide,
    badgeContent: Text(
      '7',
      style: TextStyle(color: Colors.white),
    ),
    child: IconButton(icon: Icon(Icons.shopping_cart), onPressed: () {}),
  ),
)

答案 1 :(得分:1)

您可以使用Center小部件包装徽章:

Center(
  child: Badge(
    position: BadgePosition.topRight(top: 0, right: 2),
    animationDuration: Duration(milliseconds: 300),
    animationType: BadgeAnimationType.slide,
    badgeContent: Text(
      '7',
      style: TextStyle(color: Colors.white),
    ),
    child:
    IconButton(icon: Icon(Icons.shopping_cart), onPressed: () {}),
  ),
)