【发布时间】:2020-02-23 23:57:49
【问题描述】:
ListView(
physics: NeverScrollableScrollPhysics(),
children: <Widget>[
ToolControlButton(
onPressed: () => onControlPress(0),
isSelected: selectedControl[0],
icon: Icons.category,
text: 'Option1',
),
ToolControlButton(
onPressed: () => onControlPress(1),
isSelected: selectedControl[1],
icon: Icons.category,
text: 'Option2',
),
ToolControlButton(
onPressed: () => onControlPress(2),
isSelected: selectedControl[2],
icon: Icons.category,
text: 'Option3',
),
ToolControlButton(
onPressed: () => onControlPress(3),
isSelected: selectedControl[3],
icon: Icons.category,
text: 'Option4',
)
],
)
我有布尔值列表
List<bool> selectedControl = [true, false, false, false];
和小部件列表
List<Widget> cont = [
Container(color: Colors.red),
Container(color: Colors.green,
Container(color: Colors.orange),
Container(color: Colors.blue)
];
我想在 selectedControl 具有真值的索引处显示 cont 小部件。到目前为止,这是我设法做到的,并且有效。有没有更好的方法来达到这个目的?
return cont[selectedControl.indexWhere((selected) => selected == true)],
【问题讨论】:
-
为什么不只保留所选项目的索引而不是布尔值列表?
-
嗨,尤金。我刚刚编辑了我的问题。基本上我正在使用 selectedControl 列表在屏幕左侧选择列表视图中的列表图块,据此我可以在右侧显示适当的窗口,这是容器列表。希望清楚