Flutter 如何在颤振中的ListView中添加复选框?

Flutter 如何在颤振中的ListView中添加复选框?,flutter,Flutter,我使用了下面的代码,当检查一个项目时,从中检查所有项目。如何将代码修复为不检查所有项目 class CheckBoxInListView extends StatefulWidget { @override _CheckBoxInListViewState createState() => _CheckBoxInListViewState(); } class _CheckBoxInListViewState extends State<CheckBoxInListView

我使用了下面的代码,当检查一个项目时,从中检查所有项目。如何将代码修复为不检查所有项目

class CheckBoxInListView extends StatefulWidget {
  @override
  _CheckBoxInListViewState createState() => _CheckBoxInListViewState();
}

class _CheckBoxInListViewState extends State<CheckBoxInListView> {
  bool _isChecked = false;

  List<String> _texts = [
    "InduceSmile.com",
    "Flutter.io",
    "google.com",
    "youtube.com",
    "yahoo.com",
    "gmail.com"
  ];

  @override
  Widget build(BuildContext context) {
    return ListView(
      padding: EdgeInsets.all(8.0),
      children: _texts
          .map((text) => CheckboxListTile(
                title: Text(text),
                value: _isChecked,
                onChanged: (val) {
                  setState(() {
                    _isChecked = val;
                  });
                },
              ))
          .toList(),
    );
  }
}
类CheckBoxInListView扩展StatefulWidget{
@凌驾
_CheckBoxInListViewState createState()=>\u CheckBoxInListViewState();
}
类_CheckBoxInListViewState扩展状态{
bool\u isChecked=false;
列表_文本=[
“微笑网”,
“flatter.io”,
“google.com”,
“youtube.com”,
“yahoo.com”,
“gmail.com”
];
@凌驾
小部件构建(构建上下文){
返回列表视图(
填充:边缘设置。全部(8.0),
儿童:_文本
.map((文本)=>CheckboxListTile(
标题:文本(Text),
值:\已检查,
一旦更改:(val){
设置状态(){
_isChecked=val;
});
},
))
.toList(),
);
}
}

您应该有一个选中的列表,并将它们分别分配给每个项目

类CheckBoxInListView扩展StatefulWidget{
@凌驾
_CheckBoxInListViewState createState()=>\u CheckBoxInListViewState();
}
类_CheckBoxInListViewState扩展状态{
最终清单_项目=[
SimpleModel('InduceSmile.com',false),
SimpleModel('flatter.io',false),
SimpleModel('google.com',false),
SimpleModel('youtube.com',false),
SimpleModel('yahoo.com',false),
SimpleModel('gmail.com',false),
];
@凌驾
小部件构建(构建上下文)=>ListView(
填充:常量边集。全部(8),
子项:_项
.地图(
(SimpleModel项)=>CheckboxListTile(
标题:文本(项目名称),
值:item.isChecked,
一旦更改:(布尔瓦尔){
设置状态(()=>item.isChecked=val);
},
),
)
.toList(),
);
}
类SimpleModel{
字符串标题;
布尔被检查过;
SimpleModel(this.title,this.isChecked);
}

只需列出“\u isChecked”变量并使用它即可


class CheckBoxInListView extends StatefulWidget {
  @override
  _CheckBoxInListViewState createState() => _CheckBoxInListViewState();
}

class _CheckBoxInListViewState extends State<CheckBoxInListView> {
  List<String> _texts = [
    "InduceSmile.com",
    "Flutter.io",
    "google.com",
    "youtube.com",
    "yahoo.com",
    "gmail.com"
  ];

  List<bool> _isChecked;

  @override
  void initState() {
    super.initState();
    _isChecked = List<bool>.filled(_texts.length, false);
  }

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      itemCount: _texts.length,
      itemBuilder: (context, index) {
        return CheckboxListTile(
          title: Text(_texts[index]),
          value: _isChecked[index],
          onChanged: (val) {
            setState(
              () {
                _isChecked[index] = val;
              },
            );
          },
        );
      },
    );
  }
}

类CheckBoxInListView扩展StatefulWidget{
@凌驾
_CheckBoxInListViewState createState()=>\u CheckBoxInListViewState();
}
类_CheckBoxInListViewState扩展状态{
列表_文本=[
“微笑网”,
“flatter.io”,
“google.com”,
“youtube.com”,
“yahoo.com”,
“gmail.com”
];
检查列表;
@凌驾
void initState(){
super.initState();
_isChecked=List.filled(_text.length,false);
}
@凌驾
小部件构建(构建上下文){
返回ListView.builder(
itemCount:_text.length,
itemBuilder:(上下文,索引){
返回CheckboxListTile(
标题:文本(文本[索引]),
值:_已检查[索引],
一旦更改:(val){
设定状态(
() {
_isChecked[index]=val;
},
);
},
);
},
);
}
}

这很有效。您能解释一下initState()中的以下代码是如何工作的吗_isChecked=List.filled(_text.length,false);它创建一个给定长度的列表(第一个参数)并填充初始值(第二个参数)所有列表。由于“值:_isChecked[index]”部分,需要初始化