【问题标题】:CheckBoxListTile Flutter 2.0 Single Choice SelectionCheckBoxListTile Flutter 2.0 单选选择
【发布时间】:2021-07-19 13:21:44
【问题描述】:

我尝试在 Flutter 中使用 JSON 解析方法从 RESTAPI link 获取数据。我使小部件能够选中复选框,但它选中所有而不是单选,我尝试了多种方法,但仍然无法正确。这是我的以下代码:

           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,
                );
              },
            );

我是不是做错了什么或错过了什么?

【问题讨论】:

    标签: android json flutter rest


    【解决方案1】:

    那没关系,我找到了解决办法。

    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,
                    );
                  },
                );
    

    【讨论】:

      猜你喜欢
      • 2020-12-19
      • 1970-01-01
      • 1970-01-01
      • 2021-01-15
      • 2020-10-19
      • 2021-01-02
      • 2020-08-30
      • 2020-12-16
      • 2013-05-13
      相关资源
      最近更新 更多