Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add item number on the cart icon at at appBar icon in flutter? And how can I make it animate adding new item?

I want to add item numbers on the cart icon in flutter like this in picture, and want to animate while adding new item to cart. My cart icon is in appBar.

enter image description here

like image 979
Ammy Kang Avatar asked Jun 25 '18 08:06

Ammy Kang


People also ask

How do I add the cart icon on Flutter?

Having a shopping cart icon on the right side of the AppBar on Flutter it's not that hard, you can use basically a stack widget and inside the stack get IconButton and Positioned widgets.

How do I add icons and text in AppBar in Flutter?

You can add an icon to the AppBar by adding an IconButton widget to the actions list of the AppBar.

How do you label icons in Flutter?

The simplest way to create a button with icon and text in Flutter is to use the new Material button called ElevatedButton with an icon constructor. ElevatedButton. icon() gives you the ability to add the icon and label parameter to the button.


1 Answers

It can be achieved by this piece of code in 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
                                ),
                              ),
                            )),


                      ],
                    )),

              ],
            ),
          )
        )

        ,)],
like image 124
Ammy Kang Avatar answered Oct 18 '22 23:10

Ammy Kang