lundi 9 décembre 2019

Pass the position of a ListView Element's CheckBox to onChanged callback in Flutter

I wrote the peace of code below:

int numberLines = 50;
List<bool> checkBoxValues = [];

int i = 0;
while(i < numberLines)
{
    checkBoxValues.add(false);
    i++;
}

void medCheckedChanged(bool value) => setState(() => checkBoxValues[position] = value);

ListView.builder
(
    itemCount: numberLines,
    itemBuilder: (context, position)
    {
        return Column
        (
            children: <Widget>
            [
                Container
                (
                    height: resLayout.getScreenHeightPercentage(context, 6),
                    width: resLayout.getScreenHeightPercentage(context, 14),
                    alignment: Alignment(0, 0),
                    child: Checkbox
                    (
                        value: checkBoxValues[position],
                        onChanged: medCheckedChanged,   //pass to medCheckedChanged() the position
                    ),
                )
            ]
        ),
    }
)

As you can see, I need to use the position variable in my medCheckedChanged(bool value) callback, but I can't pass it as a parameter.... Is there any other way to do it?

Thanks in advance and Keep Coding with <3




Aucun commentaire:

Enregistrer un commentaire