【问题标题】:Flutter checkbox: When I tick one element, all the others are getting selected too颤振复选框:当我勾选一个元素时,所有其他元素也被选中
【发布时间】:2019-08-20 10:33:06
【问题描述】:

在我的这部分代码中,当我勾选 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}");
    }
}

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

【问题讨论】:

    标签: android flutter dart checkboxlist


    【解决方案1】:

    你的状态对象需要是一个列表或地图,因为你在这里使用列表。然后,您可以在 initState() 中初始化您的 List&lt;bool&gt; _state,因此首先在您的有状态小部件上覆盖它,然后您将需要进行此类修改。

    
    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[index],
                      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[index]= !_State[index];
                        });
                      }),
    
    
                  new Divider(),
                ],
              );
            });
        } else if (snapshot.hasError) {
            return new Text("${snapshot.error}");
        }
    }
    
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 对于 checkboxList Widget,必须提供“value:”部分。
      猜你喜欢
      • 1970-01-01
      • 2014-03-12
      • 1970-01-01
      • 2020-10-02
      • 2018-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多