简体   繁体   中英

CheckBoxListTile Flutter Single Choice Selection

I tried in Flutter get Data from the RESTAPI link Using the JSON parsing method. I made the widget able to checked on the checked box but it checked all instead of Single Choice , I tried multiple ways I still couldn't get it right. This is my following code

           ListView.builder(
              shrinkWrap: true,
              cacheExtent: 34,
              primary: true,
              itemCount: snapshot.data.length,
              itemBuilder: (BuildContext context, int index) {
                return CheckboxListTile(
                  activeColor: Const.msinAccent,
                  title:
                      Text(snapshot.data[index].name.toString().toUpperCase()),
                  value: _isChecked,
                  secondary: CircleAvatar(
                    backgroundImage:
                        NetworkImage(snapshot.data[index].pic, scale: 13.3),
                  ),
                  onChanged: (bool val) {
                    setState(() {
                      this._isChecked = val;
                    });
                  },
                  tristate: true,
                );
              },
            );

Did I Did Something wrong or miss something in general?

It is OK then, I found the solution.

Set selectedsubs = Set();

 ListView.builder(
              shrinkWrap: true,
              cacheExtent: 34,
              primary: true,
              itemCount: snapshot.data.length,
              itemBuilder: (BuildContext context, int index) {
                return CheckboxListTile(
                  activeColor: Const.msinAccent,
                  title:
                      Text(snapshot.data[index].name.toString().toUpperCase()),
                  value: selectedsubs.contains(snapshot.data[index].name),
                  secondary: CircleAvatar(
                    backgroundImage:
                        NetworkImage(snapshot.data[index].pic, scale: 13.3),
                  ),
                  onChanged: (bool val) {
                    setState(() {
                      this._isChecked = val;
                      selectedsubs.add(snapshot.data[index].name);
                    });
                  },
                  tristate: true,
                );
              },
            );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM