简体   繁体   English

如何在 flutter 的 appBar 图标处的购物车图标上添加商品编号? 我怎样才能让它动画添加新项目?

[英]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.我想在 flutter 中的购物车图标上添加商品编号,如图所示,并且想在将新商品添加到购物车时制作动画。 My cart icon is in appBar.我的购物车图标在 appBar 中。

在此处输入图像描述

It can be achieved by this piece of code in appBar.可以通过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
                                ),
                              ),
                            )),


                      ],
                    )),

              ],
            ),
          )
        )

        ,)],

You can use badges package .您可以使用徽章包

Example:例子:

Chip(
  backgroundColor: Colors.deepPurple,
  padding: EdgeInsets.all(0),
  label: Text('BADGE', style: TextStyle(color: Colors.white)),
),
Badge(
  badgeColor: Colors.deepPurple,
  shape: BadgeShape.square,
  borderRadius: 20,
  toAnimate: false,
  badgeContent:
      Text('BADGE', style: TextStyle(color: Colors.white)),
),

Try this package.试试这个包。 It is simple and easy to use.它简单易用。

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

Badges徽章

Here is the simple and modified code from above.这是上面的简单和修改的代码。 Did a few minor changes做了一些小改动

actions: [
          Padding(
            padding: const EdgeInsets.all(10.0),
            child: Container(
              width: 35.0,
              alignment: Alignment.center,
              child: GestureDetector(
                onTap: () {
                  // Todo:navigate to cart screen
                  if (kDebugMode) {
                    print("tapped on cart icon");
                  }
                },
                child: Stack(
                  children: <Widget>[
                    const IconButton(
                      icon: Icon(
                        Icons.shopping_cart_sharp,
                        color: kPrimaryColor,
                      ),
                      onPressed: null,
                    ),
                    1 == 0
                        ? Container()
                        : Positioned(
                            top: 0,
                            right: 0,
                            child: Stack(
                              children: <Widget>[
                                Container(
                                  height: 20.0,
                                  width: 20.0,
                                  decoration: const BoxDecoration(
                                    color: kTextColorSecondary,
                                    shape: BoxShape.circle,
                                  ),
                                  child: const Center(
                                    child: Text(
                                      "10",
                                      style: TextStyle(
                                        color: kTextColor,
                                        fontSize: 11.0,
                                        fontWeight: FontWeight.bold,
                                      ),
                                    ),
                                  ),
                                ),
                              ],
                            ),
                          ),
                  ],
                ),
              ),
            ),
          ),
        ],

Number of Items Shown on Shopping cart items in Ap Ap中购物车商品显示的商品数量

  import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const HomeScreen(),
    );
  }
}

class HomeScreen extends StatefulWidget {
  const HomeScreen({Key? key}) : super(key: key);

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("STORE"),
        centerTitle: true,
        leading: Stack(
          alignment: AlignmentDirectional.center,
          children: [
            IconButton(
              onPressed: () {},
              icon: const Icon(
                Icons.shopping_cart_rounded,
                size: 30,
              ),
            ),
            Positioned(
              top: 4,
              right: 6,
              child: Container(
                height: 22,
                width: 22,
                decoration: const BoxDecoration(
                  shape: BoxShape.circle,
                  color: Colors.purple,
                ),
                child: const Center(
                    child: Text(
                  "12",
                  style: TextStyle(
                    fontSize: 11,
                    fontWeight: FontWeight.bold,
                  ),
                )),
              ),
            ),
          ],
        ),
        actions: [
          IconButton(onPressed: () {}, icon: const Icon(Icons.settings)),
          IconButton(
              onPressed: () {}, icon: const Icon(Icons.more_vert_rounded)),
        ],
      ),
    );
  }
}

You can use Stack As above to show the number of products added to cart right on the Icon (Shopping Cart).您可以使用 Stack As above 在图标(购物车)上显示添加到购物车的产品数量。

    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: () {}),
      ),
    ],
  ),

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

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