I want only one selected iconbutton from these four. when one is selected (green color) so, another should be un selected (black color). How can I manage it. Please see below image.
here is logical code for that section:
bool btn1 = false;
bool btn2 = false;
bool btn3 = false;
bool btn4 = false;
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
IconButton(
onPressed: () {
setState(() {
btn1 = !btn1;
});
},
icon: const Icon(Icons.music_note_outlined),
color: btn1 ? Colors.green : Colors.black,
),
IconButton(
onPressed: () {
setState(() {
btn2 = !btn2;
});
},
icon: const Icon(Icons.music_video_outlined),
color: btn2 ? Colors.green : Colors.black,
),
IconButton(
onPressed: () {
setState(() {
btn3 = !btn3;
});
},
icon: const Icon(Icons.headphones_outlined),
color: btn3 ? Colors.green : Colors.black,
),
IconButton(
onPressed: () {
setState(() {
btn4 = !btn4;
});
},
icon: const Icon(Icons.multitrack_audio_rounded),
color: btn4 ? Colors.green : Colors.black,
)
],
),
I hope i could clear
2
Answers
Declare variable like this :
Your widget :
Create a Button class with bool object and Button(InkWell). Then create a List of that class and initially assign false value that indicates that button is not pressed. When you press the button, make value at that index true. Something like this.
After that you can generate a list of that button.
Then you can loop through buttons and make isSelected = true for that button. This answer is general and is only for understanding purpose and may have errors therefore you can write your own solution according to it.