Search code examples
flutterlistloopsdartwidget

iterate through a list of widgets based on a list of objects in a formula


I am trying to display a certain series of widgets based on objects contained in a formula. Here is how I am assigning the widgets to a variable based on their type.

      var l = TankFormulaLength(length: tankSpec.tankSpecs.imperialLength);
      var w = TankFormulaWidth(width: tankSpec.tankSpecs.imperialWidth);
      var h = TankFormulaHeight(height: tankSpec.tankSpecs.imperialHeight);
      var d = TankFormulaDiameter(diameter: tankSpec.tankSpecs.imperialDiameter);

      List<Widget> tankMeasurements = [l, w, h, d];

Here is where these are being used.

Column(children: <Widget>[
     ...tankFormula(tankMeasurements,
        tankSpec.tankSpecs.formula),
    ],
),

The formula is pulled from an api call that happens based on a tank specification and is delivered as show below..currently I am printing it to the console so I can see what formula each tank spec is using.enter image description here

in my tankFormula function, I want to take the specific formula and use it to iterate through my list of widgets and only return the ones specific to the formula. Currently I am able to make all of them show up, because they are not being filtered.

List<Widget> tankFormula(formulaComponents, formula) {
  List<Widget> calculatedFormula = [];

  print(formula);
  return formulaComponents;
}

I am unsure how to get the end result I am looking for. As shown in the screenshot below, I only needs the fields from the formula, 'Length' and 'Diameter', to show up as those are the only ones used in this particular formula for this specific tank specification. In other instances, it could be some of the additional fields i.e. 'Width or 'Height'. Again, based on the formula returned from the API. The additional fields 'Product', 'Tank Capacity', and 'Sensor Offset', will always be displayed.

enter image description here

I am aware that calculatedFormula is not being used. I created this variable to hold the returned List of Widgets that will be displayed on the screen. I am stuck and unsure how to move forward. Thank you sincerely for your help in advance!


Solution

  • Here is how I solved this problem. Because I was receiving a specific value from the api for each portion of the formula I needed to make sure I was passing that down to my custom widget through the function I had created and then checking to see which variables the formula contains.

        List<Widget> tankFormula(l, w, h, d, formula) {
          List<Widget> calculatedFormula = [];
            if (formula.contains('l')) {
              calculatedFormula.add(TankFormulaLength(length: l));
            }
            if (formula.contains('w')) {
              calculatedFormula.add(TankFormulaWidth(width: w));
            }
            if (formula.contains('h')) {
              calculatedFormula.add(TankFormulaHeight(height: h));
            }
            if (formula.contains('d')) {
              calculatedFormula.add(TankFormulaDiameter(diameter: d));
            }
    
            print(formula);
            return calculatedFormula;
          }