vendredi 21 août 2020

How to define a variable for checkbox widget limited to the very instance?

class MemberList extends StatefulWidget {
  @override
    _MemberListState createState() => _MemberListState();
}

class _MemberListState extends State<MemberList> {
  @override
  Widget build(BuildContext context) {

    final members = Provider.of<List<Member>>(context); 
    final presentMembers = new List();
    bool _present = false;

    return ListView.builder(
      scrollDirection: Axis.vertical,
      shrinkWrap: true,
      physics: ClampingScrollPhysics(),
      itemCount: members.length,
      itemBuilder: (context, index){
        return Padding(
          padding: EdgeInsets.only(top: 8.0),
          child: Card(
            color: Colors.white,
            margin: EdgeInsets.fromLTRB(20.0, 6.0, 20.0, 0),
            child: ListTile(
              contentPadding: EdgeInsets.symmetric(horizontal: 20.0),
              title: Text(members[index].name,style: TextStyle(color: Colors.black,fontWeight: FontWeight.bold),),
              trailing: Checkbox(
                value:  members[index] != null,
                onChanged: (bool value) {
                  setState(() {
                    _present = value;
                  });
                  if(_present == true){
                  presentMembers.add(members[index].name);
                  }else{presentMembers.remove(members[index].name);}
                },
                activeColor: Colors.black,
              ),
            ),
          ),
        );
      }
    );
  }
}

In the above you code if you see '_present' is repeatedly created. I want that variable such that it is limited to that instance only. Because with this code what is happening is whenever I check on the checkbox it gets unchecked within mini-seconds because the value of the same variable in other instance is still false. My end goal is to get a dynamic list of members present generated by checking the box. I hope the question will be clear.:))




Aucun commentaire:

Enregistrer un commentaire