如何在flutter中的appBar图标上的购物车图标上添加项目编号?以及如何使它动画添加新项目?

时间:2018-06-25 08:37:01

标签: animation dart flutter

我想在购物车图标上添加商品编号,如图所示,并且要在向购物车中添加新商品时进行动画处理。我的购物车图标在appBar中。

enter image description here

4 个答案:

答案 0 :(得分:6)

您可以使用badges package

示例:

each.key and each.value

答案 1 :(得分:2)

可以通过appBar中的这段代码来实现。

appBar: new AppBar(

    actions: <Widget>[

      new Padding(padding: const EdgeInsets.all(10.0),

        child: new Container(
          height: 150.0,
          width: 30.0,
          child: new GestureDetector(
            onTap: () {
              Navigator.of(context).push(
                  new MaterialPageRoute(
                      builder:(BuildContext context) =>
                      new CartItemsScreen()
                  )
              );
            },

            child: new Stack(

              children: <Widget>[
                new IconButton(icon: new Icon(Icons.shopping_cart,
                  color: Colors.white,),
                    onPressed: null,
                ),
                list.length ==0 ? new Container() :
                new Positioned(

                    child: new Stack(
                      children: <Widget>[
                        new Icon(
                            Icons.brightness_1,
                            size: 20.0, color: Colors.green[800]),
                        new Positioned(
                            top: 3.0,
                            right: 4.0,
                            child: new Center(
                              child: new Text(
                                list.length.toString(),
                                style: new TextStyle(
                                    color: Colors.white,
                                    fontSize: 11.0,
                                    fontWeight: FontWeight.w500
                                ),
                              ),
                            )),


                      ],
                    )),

              ],
            ),
          )
        )

        ,)],

答案 2 :(得分:0)

试试这个包。它简单易用。

Badge(
   badgeContent: Text('3'),
   child: Icon(Icons.settings),
)

Badges

答案 3 :(得分:-1)

    appBar: new AppBar(
    title: new Text("Add Stock"),
    centerTitle: true,
    actions: [
      Badge(
        position: BadgePosition.topEnd(top: 3, end: 18),
        animationDuration: Duration(milliseconds: 300),
        animationType: BadgeAnimationType.slide,
        badgeContent: Text(
          '3',
          style: TextStyle(color: Colors.white),
        ),
        child: IconButton(
            icon: Icon(Icons.shopping_cart),
            padding: EdgeInsets.only(right: 30.0),
            onPressed: () {}),
      ),
    ],
  ),