Flutter 在列中生成选择芯片的列表
我有一个字符串数组,我试图将其显示为choice芯片。 我看到了颤振网站(这里:)的文档,我复制了它,尝试在我的应用程序中使用它 这就是我所拥有的:Flutter 在列中生成选择芯片的列表,flutter,dart,Flutter,Dart,我有一个字符串数组,我试图将其显示为choice芯片。 我看到了颤振网站(这里:)的文档,我复制了它,尝试在我的应用程序中使用它 这就是我所拥有的: Widget categoriesList(BuildContext context) { return Wrap( children: [ List<Widget>.generate( options.length, (int idx) {
Widget categoriesList(BuildContext context) {
return Wrap(
children: [
List<Widget>.generate(
options.length,
(int idx) {
return ChoiceChip(
label: Text(options[idx]),
selected: _value == idx,
onSelected: (bool selected) {
setState(() {
_value = selected ? idx : null;
});
});
},
).toList()
],
);
}
@override
Widget build(BuildContext context) {
return Container(
child: Column(
children: [
Title(),
categoriesList(context),
],
),
);
}
}
我试着搜索,这个错误似乎很常见,但我真的不知道我的代码有什么问题——即使在看到标记为正确的答案之后。而且,这段代码来自flifter文档,所以我想应该没问题
我遗漏了什么?.toList()
自动生成列表
,我可以看到您在其中解析小部件列表的[]
。只需删除
[]
,如下所示:return Wrap(
children: List<Widget>.generate(
options.length,
(int idx) {
return ChoiceChip(
label: Text(options[idx]),
selected: _value == idx,
onSelected: (bool selected) {
setState(() {
_value = selected ? idx : null;
});
});
},
).toList(),
);
return Wrap(
子项:List.generate(
选项。长度,
(int idx){
返回选择权(
标签:文本(选项[idx]),
所选:_值==idx,
onSelected:(布尔选定){
设置状态(){
_值=所选?idx:null;
});
});
},
).toList(),
);
return Wrap(
children: List<Widget>.generate(
options.length,
(int idx) {
return ChoiceChip(
label: Text(options[idx]),
selected: _value == idx,
onSelected: (bool selected) {
setState(() {
_value = selected ? idx : null;
});
});
},
).toList(),
);