samedi 28 novembre 2020

checkbox lost checked value in flutter

I show my list of answers via ListView.builder and check value on checkbox work ok, but when I scroll down and turn back checked value is lost. Other way when lost focus in checked answer automatic checkbox lost checked value. Below is my code. I would be grateful if someone could help me.

 class AnswerItem extends StatefulWidget {
  @override
  _AnswerItemState createState() => _AnswerItemState();
}

class _AnswerItemState extends State<AnswerItem> {

    List<bool> _data = [false, false, false, false];

  void _onChange(bool value, int index) {
    setState(() {
      _data[index] = value;
    });
  }

  Widget build(BuildContext context) {

    final questionItems = Provider.of<Item>(context);
    List<Answer> listOfAnswers = questionItems.answers.toList();

    return SingleChildScrollView(
       child:
             ListView.builder(
                shrinkWrap: true,
                itemCount: listOfAnswers.length,
                itemBuilder: (context, index) {
                  return Padding(
                      padding: const EdgeInsets.symmetric(horizontal: 25),
                      child: Card(
                           child: Padding(
                           padding: const EdgeInsets.symmetric(horizontal: 10),
                               child: CheckboxListTile(
                                    value: _data[index],
                                    title: Text(listOfAnswers[index].title),
                                    onChanged: (val) {
                                          _onChange(val, index);
                                    },
                               ),
                            ),
                         ),
                      );
                   },
                 ),
              );
          }      
       }



Aucun commentaire:

Enregistrer un commentaire