0
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());
                                        });
                                      },
                                    ),
                                  ),
                          ],
                        ),
                      )
                    ],
                  ),
                ],
              );
            },
          ),
        ),
      ],
    );

I had a product data and the product child data then, when I want to show the product data and the product child data I have to create a new widget and place it inside product data `ListView.builder`. I have solved count the product child totalprice and now I want to count the entire totalPrice from the different widget.

1 Answers1

1

You can do something like below. I am giving this code from my project for your understanding. You can check it once.

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

And for better understanding, you can check want-to-pass-the-total-price-in-the-bottom-navigation-bar-in-flutter

Salim Murshed
  • 1,423
  • 1
  • 8
  • 19