【发布时间】:2020-04-05 16:26:22
【问题描述】:
我下面的代码正在运行,但不是根据列表的长度返回多个小部件,而是在第一轮停止,经过大量研究和谷歌搜索后,我知道它停止是因为我正在返回一个小部件。所以基本上 for 循环在遇到“返回”时停止。 如果我没有在它返回的小部件之前添加“返回”,或者它给出错误说“小部件期望返回类型但没有返回”。所以没有“我认为”我知道这个问题,但我找不到解决方案。
@override
Widget build(BuildContext context) {
for (var allAttributes in widget.allAttributes) {
//print(allAttributes.name);
bool attributeCheck;
if(widget.attributes.length > 0){
for(var attributes in widget.attributes){
if(allAttributes.id == attributes.attributeId){
return Row(
children: <Widget>[
new Container(
alignment: Alignment(-1.0, -1.0),
child: Padding(
padding: const EdgeInsets.only(bottom: 10.0, right: 10.0),
child: Text(
allAttributes.name + ':',
style: TextStyle(
color: Colors.black,
fontSize: 20,
fontWeight: FontWeight.w600),
),
)),
DropdownButton<Attributes>(
hint: Text("Select item"),
value: selectedUser,
onChanged: (Attributes Value) {
setState(() {
selectedUser = Value;
});
},
items: widget.attributes.map((Attributes attributes) {
return DropdownMenuItem<Attributes>(
value: attributes,
child: Row(
children: <Widget>[
SizedBox(
width: 10,
),
Text(
attributes.value,
style: TextStyle(color: Colors.black),
),
],
),
);
}).toList(),
),
],
);
}
}
}
}
return Text('Nothing');
}
我确实尝试过使用地图,但它也不起作用,这是地图的代码:
@override
Widget build(BuildContext context) {
widget.allAttributes.map((AllAttributes allAttributes) {
//print(allAttributes.name);
widget.attributes.map((Attributes attributes){
return Row(
children: <Widget>[
new Container(
alignment: Alignment(-1.0, -1.0),
child: Padding(
padding: const EdgeInsets.only(bottom: 10.0, right: 10.0),
child: Text(
allAttributes.name + ':',
style: TextStyle(
color: Colors.black,
fontSize: 20,
fontWeight: FontWeight.w600),
),
)),
DropdownButton<Attributes>(
hint: Text("Select item"),
value: selectedUser,
onChanged: (Attributes Value) {
setState(() {
selectedUser = Value;
});
},
items: widget.attributes.map((Attributes attributes) {
return DropdownMenuItem<Attributes>(
value: attributes,
child: Row(
children: <Widget>[
SizedBox(
width: 10,
),
Text(
attributes.value,
style: TextStyle(color: Colors.black),
),
],
),
);
}).toList(),
),
],
);
}).toList();
}).toList();
return Text('Nothing');
}
【问题讨论】:
-
你需要构建一个小部件数组然后返回它
-
请问您能提供一些代码吗?谢谢
标签: flutter