简体   繁体   English

CheckBoxListTile Flutter 单选选择

[英]CheckBoxListTile Flutter Single Choice Selection

I tried in Flutter get Data from the RESTAPI link Using the JSON parsing method.我尝试在 Flutter 中使用 JSON 解析方法从 RESTAPI 链接获取数据。 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.我使小部件能够检查复选框,但它检查了所有而不是Single Choice ,我尝试了多种方法,但仍然无法正确。 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,
                );
              },
            );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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