skip to Main Content

I have to calculate the sub-total amount, shipping cost, and packaging cost and show them on CartView page.
I fetched data from API and I got a price, quantity, shipping cost, etc. There subtotal = price* quantity.
There is my code:

controller.obx((response) => response != null
                  ? Container(
                      child: ListView.builder(
                        scrollDirection: Axis.vertical,
                        shrinkWrap: true,
                        itemCount: response.length,
                        itemBuilder: (BuildContext context, index) {
                          var subTotal =
                              response[index].quantity!.toDouble() *
                                  response[index].product!.price!.toDouble();
                          return Column(
                            children: [
                              ListTile(
                                title: Text('Sub Total'.tr.toString()),
                                trailing: Text(subTotal.toString()),
                              ),
                              ListTile(
                                title: Text('Discount'.tr.toString()),
                                trailing: Text(
                                    '00'), /*${response[index].product!.price!.toInt()+response[index+1].product!.price!.toInt()}*/
                              ),
                              ListTile(
                                title: Text('Delivery Charge'.tr.toString()),
                                trailing: Text(
                                    '${response[index].quantity!.toDouble() * response[index].product!.shippingCost!.toDouble()} tk'), /*${response[index].product!.price!.toInt()+response[index+1].product!.price!.toInt()}*/
                              ),
                              ListTile(
                                title: Text('Packaging Cost'.tr.toString()),
                                trailing: Text( response[index].basketQty == '1'? '50': '120'),
                              ),
                            ],
                          );
                        },
                      ),
                    )
                  : Text('')),

In this way I got subtotal for first item then got another subtotal for second item and so on, but I need just one subtitle that will have all item subtotal in sum form.

2

Answers


  1. Use a global variable subTotal and keep adding the the total of each item in it.

     var subTotal = 0;                                  
     controller.obx((response) => response != null
                  ? Container(
                      child: ListView.builder(
                        scrollDirection: Axis.vertical,
                        shrinkWrap: true,
                        itemCount: response.length,
                        itemBuilder: (BuildContext context, index) {
                          subTotal +=
                              response[index].quantity!.toDouble() *
                                  response[index].product!.price!.toDouble();
                          return Column(
                            children: [
                              ListTile(
                                title: Text('Sub Total'.tr.toString()),
                                trailing: Text(subTotal.toString()),
                              ),
                              ListTile(
                                title: Text('Discount'.tr.toString()),
                                trailing: Text(
                                    '00'), /*${response[index].product!.price!.toInt()+response[index+1].product!.price!.toInt()}*/
                              ),
                              ListTile(
                                title: Text('Delivery Charge'.tr.toString()),
                                trailing: Text(
                                    '${response[index].quantity!.toDouble() * response[index].product!.shippingCost!.toDouble()} tk'), /*${response[index].product!.price!.toInt()+response[index+1].product!.price!.toInt()}*/
                              ),
                              ListTile(
                                title: Text('Packaging Cost'.tr.toString()),
                                trailing: Text( response[index].basketQty == '1'? '50': '120'),
                              ),
                            ],
                          );
                        },
                      ),
                    )
                  : Text('')),
    
    Login or Signup to reply.
  2. You can wrap your Container inside the Column and you can use fold method on your response (iterable) . Your code would look something like this

    controller.obx((response) => response != null
                      ? Column(
                  children: [
                    Container(
                      child: ListView.builder(
                        scrollDirection: Axis.vertical,
                        shrinkWrap: true,
                        itemCount: response.length,
                        itemBuilder: (BuildContext context, index) {
                          var subTotal = response[index].quantity!.toDouble() *
                              response[index].product!.price!.toDouble();
                          return Column(
                            children: [
                              ListTile(
                                title: Text('Sub Total'.tr.toString()),
                                trailing: Text(subTotal.toString()),
                              ),
                              ListTile(
                                title: Text('Discount'.tr.toString()),
                                trailing: Text(
                                    '00'), /*${response[index].product!.price!.toInt()+response[index+1].product!.price!.toInt()}*/
                              ),
                              ListTile(
                                title: Text('Delivery Charge'.tr.toString()),
                                trailing: Text(
                                    '${response[index].quantity!.toDouble() * response[index].product!.shippingCost!.toDouble()} tk'), /*${response[index].product!.price!.toInt()+response[index+1].product!.price!.toInt()}*/
                              ),
                              ListTile(
                                title: Text('Packaging Cost'.tr.toString()),
                                trailing: Text(response[index].basketQty == '1'
                                    ? '50'
                                    : '120'),
                              ),
                            ],
                          );
                        },
                      ),
                    ),
                     ListTile(
                      title: Text("Total :",),
                      trailing: Text(response.fold<double>(
                          0,
                              (previousValue, element) =>
                          previousValue +
                              element.quantity!.toDouble() *
                                  element.product!.price!.toDouble()).toString(),),
                    ),
                  ],
                )
              : Text('')),
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search