I'm new in flutter and I'm trying to implement something like radio group with custom buttons in Android. For this I created StatefulWidget, which hold list of selectable buttons. For every button I was set press listener where I do something like this:
setState(() {
buttons.forEach((button) => button.isSelected = false);
buttons[selectedButtonIndex].isSelected = true;
});
And then my CustomButtonWidget
changes color depending on the parameter isSelected
.
All this works well. However, I have an additional requirement. I need my RadioGroupWidget
to return the selected button type. For this I created a callback :
final ValueChanged<ButtonType> onChanged;
And now my button press listener looking like this:
onTap: () {
setState(() {
buttons.forEach((button) => button.isSelected = false);
buttons[selectedButtonIndex].isSelected = true;
});
onChanged(buttons[selectedButtonIndex].type);
}
Next I get this type of button in my other widget which is using RadioGroupWidget
:
CustomRadioGroup(
onChanged: (value) {
setState(() {
buttonType= value;
});
}),
)
As you can see I call again setState
. This is what leads to the problem. But I need to do this, because I need to update the state of another widget (for example let's call it InfoWidget
) depending on the selected button.
After all these manipulations, the state of the InfoWidget
is updated correctly, but the state of the selected button in the RadioGroupWidget
does not change. I tried to debug this and I see that at first the parameter isSelected
is set to true for the desired button, but after the state of the button I selected is not updated, because its parameter isSelected
becomes false. And I don't understand why this is happening.
Please help, I am completely confused.