Home > Enterprise >  How to calculate a value inside a widget and pass that variable to a parent property?
How to calculate a value inside a widget and pass that variable to a parent property?

Time:09-05

I have a ExpansionTile widget and I want to calculate a value inside it and pass that variable (total) to the trailing property. How is it possible to achieve this?

for (var name in widget.namesList)

              Padding(
                padding: const EdgeInsets.all(8.0),
                child: Material(
                  child: Card(
                    shape: RoundedRectangleBorder(
                    ),
                    ExpansionTile(
                        title: Text(
                          name,
                        ),
                        children: <Widget>[
                          for (var food in widget.foodDetails)
                            if (food.foodEatenBy.contains(name))
                              Padding(
                                padding: const EdgeInsets.all(3.0),
                                child: Text(
                                      (double.parse(food.foodTotalAfterTax
                                                  .toString()
                                                  .substring(
                                                      1,
                                                      food.foodTotalAfterTax
                                                              .toString()
                                                              .length -
                                                          1)) /
                                              food.foodEatenBy.length)
                                          .toStringAsFixed(2), //keep adding this value to a variable total

                                ),
                              ),
                        ],
                        trailing: total,
                      ),
                    ),
                  ),
                ),
              ),

CodePudding user response:

You could use a variable that's reactive(RX, Stream, SetState) and make the ExpansionTile listen to it,but at the same time make sure the child doing the changes is not rebuilt to avoid infinite rebuilding.

Why do you need to pass the value up the tree? I think there might be a better way of achieving what you want to do than going this route, it sounds hacky.

CodePudding user response:

You may use a Builder and inside builder you can calculate and store it to a variable and you can assign that variable to your trail.

CodePudding user response:

The following should work.

for (var name in widget.namesList)
  // v-- Here - Add a Builder
  Builder(
    builder: (context) {
      // v-- Here - Calculate the total accumulated
      var totalAcc = 0.0;
      for (var food in widget.foodDetails)
        if (food.foodEatenBy.contains(name)) {
          totalAcc  = (double.parse(food.foodTotalAfterTax
                  .toString()
                  .substring(1,
                      food.foodTotalAfterTax.toString().length - 1)) /
              food.foodEatenBy.length);
        }
      // v-- Here - Assign the total to be displayed
      final total = totalAcc.toStringAsFixed(2);

      return Padding(
        padding: const EdgeInsets.all(8.0),
        child: Material(
          child: Card(
            shape: RoundedRectangleBorder(),
            child: ExpansionTile(
              title: Text(
                name,
              ),
              children: <Widget>[
                for (var food in widget.foodDetails)
                  if (food.foodEatenBy.contains(name))
                    Padding(
                      padding: const EdgeInsets.all(3.0),
                      child: Text(
                        (double.parse(food.foodTotalAfterTax
                                    .toString()
                                    .substring(
                                        1,
                                        food.foodTotalAfterTax
                                                .toString()
                                                .length -
                                            1)) /
                                food.foodEatenBy.length)
                            .toStringAsFixed(2),
                      ),
                    ),
              ],
              trailing: Text(total),
            ),
          ),
        ),
      );
    },
  ),
  • Related