首页 > 解决方案 > 如何计算动态小部件 totalPrice 变量值

问题描述

class _ChildDataState extends State<ChildData> {
  int totalPrice = 0;
  List totalHarga = List();

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        Container(
          child: ListView.builder(
            shrinkWrap: true,
            physics: NeverScrollableScrollPhysics(),
            itemCount: widget.items.length,
            itemBuilder: (context, i) {
              int price = int.parse(widget.items[i].price.split('.').first);

              return Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Row(
                    children: [
                      Container(
                        height: 100,
                        width: 100,
                        decoration: BoxDecoration(
                          shape: BoxShape.circle,
                          image: DecorationImage(
                            fit: BoxFit.cover,
                            image: NetworkImage(
                              widget.items[i].image ?? '',
                            ),
                          ),
                        ),
                      ),
                      SizedBox(width: 10),
                      Expanded(
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Text(
                              widget.items[i].name,
                              style: blackTextFont.copyWith(
                                  fontWeight: FontWeight.bold),
                            ),
                            SizedBox(height: 5),
                            Text(
                                "Rp. ${widget.items[i].price.split('.').first} /   ${widget.items[i].unit}",
                                style: blackNumberFont),
                            SizedBox(height: 8),
                            Text(widget.items[i].description),
                            SizedBox(height: 8),
                            SizedBox(height: 8),
                            widget.items[i].counter != 0
                                ? Row(
                                    mainAxisAlignment:
                                        MainAxisAlignment.spaceEvenly,
                                    children: [
                                      Container(
                                        decoration: BoxDecoration(
                                          borderRadius:
                                              BorderRadius.circular(10),
                                          color: mainColor,
                                        ),
                                        width: 30,
                                        height: 30,
                                        child: IconButton(
                                          icon: Icon(
                                            Icons.remove,
                                            size: 15,
                                            color: Colors.white,
                                          ),
                                          onPressed: () {
                                            setState(
                                              () {
                                                widget.items[i].counter--;
                                                totalPrice -= price;
                                                widget.onTotalChange(totalPrice.toString());
                                              },
                                            );
                                          },
                                        ),
                                      ),
                                      Text(widget.items[i].counter.toString()),
                                      Container(
                                        decoration: BoxDecoration(
                                          borderRadius:
                                              BorderRadius.circular(10),
                                          color: mainColor,
                                        ),
                                        width: 30,
                                        height: 30,
                                        child: IconButton(
                                          icon: Icon(
                                            Icons.add,
                                            size: 15,
                                            color: Colors.white,
                                          ),
                                          onPressed: () {
                                            setState(() {
                                              widget.items[i].counter++;
                                              totalPrice += price;
                                              widget.onTotalChange(totalPrice.toString());
                                            });
                                          },
                                        ),
                                      ),
                                    ],
                                  )
                                : Center(
                                    child: GestureDetector(
                                      child: Container(
                                          decoration: BoxDecoration(
                                            borderRadius:
                                                BorderRadius.circular(4),
                                            color: mainColor,
                                          ),
                                          width: double.infinity,
                                          height: 30,
                                          child: Center(
                                            child: Text("ADD",
                                                style: whiteTextFont),
                                          )),
                                      onTap: () {
                                        setState(() {
                                          widget.items[i].counter++;
                                          totalPrice += price;
                                          widget.onTotalChange(totalPrice.toString());
                                        });
                                      },
                                    ),
                                  ),
                          ],
                        ),
                      )
                    ],
                  ),
                ],
              );
            },
          ),
        ),
      ],
    );

我有一个产品数据和产品子数据,当我想显示产品数据和产品子数据时,我必须创建一个新的小部件并将其放在产品数据“ListView.builder”中。我已经解决了计算产品子总价格的问题,现在我想从不同的小部件中计算整个总价格。

标签: flutterdart

解决方案


您可以执行以下操作。我从我的项目中提供此代码以供您理解。你可以检查一次。

  double getPrice() {
    double price = 0;
    AppData.cartList.forEach((x) {
      price += x.price * x.widget.items[i];
    });
    return price;
  }

为了更好地理解,您可以检查想要传递的总价格在底部导航栏内颤振


推荐阅读