Created
March 31, 2022 10:56
-
-
Save tauheed0007/f3cf87315b9cdc9922aff0570755d0ad to your computer and use it in GitHub Desktop.
This is how we can use 3 toggle buttons with 3 icons
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ToggleButtons( | |
children: <Widget>[ | |
Icon(Icons.ac_unit), | |
Icon(Icons.call), | |
Icon(Icons.cake), | |
], | |
onPressed: (int index) { | |
setState(() { | |
isSelected[index] = !isSelected[index]; | |
}); | |
}, | |
isSelected: isSelected, | |
), | |
ToggleButtons( | |
children: <Widget>[ | |
Icon(Icons.ac_unit), | |
Icon(Icons.call), | |
Icon(Icons.cake), | |
], | |
onPressed: (int index) { | |
setState(() { | |
for (int buttonIndex = 0; buttonIndex < isSelected.length; buttonIndex++) { | |
if (buttonIndex == index) { | |
isSelected[buttonIndex] = true; | |
} else { | |
isSelected[buttonIndex] = false; | |
} | |
} | |
}); | |
}, | |
isSelected: isSelected, | |
), | |
ToggleButtons( | |
children: <Widget>[ | |
Icon(Icons.ac_unit), | |
Icon(Icons.call), | |
Icon(Icons.cake), | |
], | |
onPressed: (int index) { | |
setState(() { | |
for (int buttonIndex = 0; buttonIndex < isSelected.length; buttonIndex++) { | |
if (buttonIndex == index) { | |
isSelected[buttonIndex] = !isSelected[buttonIndex]; | |
} else { | |
isSelected[buttonIndex] = false; | |
} | |
} | |
}); | |
}, | |
isSelected: isSelected, | |
), | |
ToggleButtons( | |
children: <Widget>[ | |
Icon(Icons.ac_unit), | |
Icon(Icons.call), | |
Icon(Icons.cake), | |
], | |
onPressed: (int index) { | |
int count = 0; | |
isSelected.forEach((bool val) { | |
if (val) count++; | |
}); | |
if (isSelected[index] && count < 2) | |
return; | |
setState(() { | |
isSelected[index] = !isSelected[index]; | |
}); | |
}, | |
isSelected: isSelected, | |
), |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment