lundi 31 août 2020

Remove the array value from CheckboxListTile in flutter

I am trying to have list of CheckboxListTile. I am getting the array of the checked value. I am adding the all the checked values arrays and set them to the list of text but the problem is I couldnot remove the value when unchecked. I have tried as follows:

My function to get checkValues from checkBox:

  onChecked(var items) {
    setState(() {
      myItems.addAll(items);
    });

    for (var i = 0; i < myItems.length; i++) {
      bool repeated = false;
      for (var j = 0; j < nonRepated.length; j++) {
        if (myItems[i] == nonRepated[j]) {
          repeated = true;
        }
      }
      if (!repeated) {
        nonRepated.add(myItems[i]);
      }
    }
    print(nonRepated);
  }

And I set the values in text as follows:

  for (int i = 0; i < nonRepated.length; i++) Text(nonRepated[i])

I have passed the function as follows:

 CheckboxWidget(cropsProduce[i].items[i].items[i].data,onChecked)
                                

My list of checkbox

  ListView(
        shrinkWrap: true,
        physics: const NeverScrollableScrollPhysics(),
        children: widget.values.keys.map((String key) {
          return new CheckboxListTile(
            title: new Text(key),
            value: widget.values[key],
            activeColor: Theme.of(context).primaryColor,
            checkColor: Colors.white,
            onChanged: (bool value) {
              setState(() {
                widget.values[key] = value;
              });
              setValues(widget.values[key]);
            },
          );
        }).toList(),
      );

     void setValues(bool myValue) {
    widget.values.forEach((key, myValue) {
      if (myValue == true) {
        if (tmpArray.contains(key) != widget.values.containsKey(key))
          tmpArray.add(key);

        widget._onChecked(allValues);
      } else if (myValue == false) {
        print("Else false");
        print(myValue);
        tmpArray.remove(key);

        widget._onChecked(allValues);
      }
    });
  }



Aucun commentaire:

Enregistrer un commentaire