首页 > 解决方案 > 颤振复选框:当我勾选一个元素时,所有其他元素也被选中

问题描述

在我的这部分代码中,当我勾选 checkboxlist 的一个元素时,所有其他元素也都被选中。

我尝试将索引放在 _State[index] 上,但不起作用。

FutureBuilder<List<Map>>(
future: fetchUsersFromDatabase(),
builder: (context, snapshot) {
    if (snapshot.hasData) {
        return new ListView.builder(
        itemCount: snapshot.data.length,
        itemBuilder: (context, index) {
          return new Column(
            crossAxisAlignment:    CrossAxisAlignment.start,
            children: <Widget>[

              CheckboxListTile(

                  value:_State,
                  title: new Text(
                      snapshot.data[index]['checkcode'] +
                          ",Tk" +
                          snapshot.data[index]['amount'].toString(),
                      style: new TextStyle(
                          fontWeight: FontWeight.bold,
                          fontSize: 18.0)),
                  activeColor: Colors.blue,
                  onChanged: (b) {
                    setState(() {                       
                     _State= !_State;
                    });
                  }),


              new Divider(),
            ],
          );
        });
    } else if (snapshot.hasError) {
        return new Text("${snapshot.error}");
    }
}

期望分别选择列表的每个元素。

标签: androidflutterdartcheckboxlist

解决方案


不要使用 _state ,而是尝试直接将值传递给特定的图块

onChanged: (bool value) {
    setState(() {
         widget.formItems[widget.count]['list'][i]['value'] = value;
    });
}

推荐阅读