mardi 31 mars 2020

How to make checkbox select, de-select and sellect all in List?

How to select all checkbox ? and if i unselect any checkbox in list then how to remain all selected checkboxes rather than unselect checkbox ?

class SelectAllCheckbox extends StatefulWidget {
  @override
  _SelectAllCheckboxState createState() => _SelectAllCheckboxState();
}

class _SelectAllCheckboxState extends State<SelectAllCheckbox> {
  List _selecteCategorysID = List();
  bool rememberMe = false;
  List<String>userList=['Sam','John','Rohan','Peter'];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body:  Column(
        children: <Widget>[
          Padding(
            padding: const EdgeInsets.only(left: 20, right: 20),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                Text("Select All"),
                Checkbox(
                    value: rememberMe,
                    onChanged: _onRememberMeChanged
                )
              ],
            ),
          ),
          SizedBox(
            height: 450,
            child: ListView.builder(
                itemCount: userList.length,
                itemBuilder: (context, item) {
                  return Card(
                      child: CheckboxListTile(
                        selected: false,
                        value: _selecteCategorysID.contains(userList[item]),
                        onChanged: (bool selected) {
                          _onCategorySelected(selected, userList[item]);
                        },

                        title: Text(userList[item]),
                      ));
                }),
          ),
        ],
      ));
  }

Logic code

  1. this is code for selecting and deselecting single checkbox
  2. it is possible to select all check boxes in this logic

List item

 void _onCategorySelected(bool selected, category_id) {
    if (selected == true) {
      setState(() {
        _selecteCategorysID.add(category_id);

      });
    } else {
      setState(() {
        _selecteCategorysID.remove(category_id);

      });
    }
  }

Logic code

this is for Select All check box

  void _onRememberMeChanged(bool newValue) => setState(() {
    rememberMe = newValue;

    if (rememberMe) {

      // TODO: Here goes your functionality to select all checkbox
    } else {

    }
  });
}



Aucun commentaire:

Enregistrer un commentaire