gpt4 book ai didi

flutter - 如何为每个切换按钮添加一个int值? [ flutter ]

转载 作者:行者123 更新时间:2023-12-03 03:56:08 25 4
gpt4 key购买 nike

我有三个切换按钮,如何为每个切换按钮添加一个int值?

 ToggleButtons(
children: [
Text('5 '),
Text('6'),
Text('7'),
],
onPressed: (int index) {
setState(() {
for (int buttonIndex = 0;
buttonIndex < isSelected2.length;
buttonIndex++) {
if (buttonIndex == index) {
isSelected2[buttonIndex] = true;
} else {
isSelected2[buttonIndex] = false;
}
}
});
},
isSelected: isSelected2,
),

最佳答案

您的问题并不十分清楚,但是我认为如果用户按下带有文本“5”的按钮,此值应应用于整数变量,该变量将在其他任何地方使用,可以这样实现:

...
int _choice;
...

ToggleButtons(
children: [
Text('5'),
Text('6'),
Text('7'),
],
onPressed: (int index) {
setState(() {
isSelected2[index] = !isSelected2[index];
switch(index){
case 0:
_choice = 5;
break;
case 1:
_choice = 6;
break;
case 2:
_choice = 7;
break;
}
}
);
},
isSelected: isSelected2,
),

关于flutter - 如何为每个切换按钮添加一个int值? [ flutter ],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61197506/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com