【发布时间】:2020-01-14 13:23:53
【问题描述】:
我目前正在学习 Flutter 并取得了不错的进步,所以如果这是一个菜鸟问题,请多多包涵。
Flutter 最近更新到 1.9.1,随之而来的是新的小部件 ToggleButton 类;
这正是我所追求的,所以我在我的代码中实现了这个小部件,如下所示
var isSelected1 = [false, true];
var isSelected2 = [false, true];
ToggleButtons(
borderColor: Colors.black,
fillColor: Colors.grey,
borderWidth: 2,
selectedBorderColor: Colors.black,
selectedColor: Colors.white,
borderRadius: BorderRadius.circular(0),
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
'Open 24 Hours',
style: TextStyle(fontSize: 16),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
'Custom Hours',
style: TextStyle(fontSize: 16),
),
),
],
onPressed: (int index) {
setState(() {
for (int buttonIndex = 0;
buttonIndex < isSelected2.length;
buttonIndex++) {
if (buttonIndex == index) {
isSelected2[buttonIndex] = true;
} else {
isSelected2[buttonIndex] = false;
}
}
});
},
isSelected: isSelected2,
),`
我要做的是在选择按钮时显示一个小部件。
我已经尝试了很多方法来处理 if、and、else 语句,但到目前为止我无法弄清楚。
例如
if (buttonIndex == index[0]) {
// code here}
else {
//code here}
我哪里错了?
谢谢!
【问题讨论】: