Hi there, could you please help me with this code?...
# compose
n
Hi there, could you please help me with this code? Basically, I have three buttons and I want to be able to select them individually but also to have several of them selected at the same time if needed. So far, all I managed to do is that when I select one all the others are selected too. I know I need to loop through them for it to work, but I don't understand how to do it:
Copy code
@Composable
fun CirclesLayout() {
    val selectedState = remember { mutableStateOf(false) }
    ScrollableRow {
        for (circle in listOf()) {
            Circles(
                color = if (!selectedState.value) Color(0xffA5D6A7) else Color(0xff4CAF50),
                gradientColor = Color(0xff4CAF50),
                text = "Alarms",
                selected = selectedState.value,
                onSelected = { selectedState.value = !selectedState.value }),
            Circles(
                color = if (!selectedState.value) Color(0xffB39DDB) else Color(0xff673AB7),
                gradientColor = Color(0xff673AB7),
                text = "Media Sounds",
                selected = selectedState.value,
                onSelected = { selectedState.value = !selectedState.value }),
            Circles(
                color = if (!selectedState.value) Color(0xffFFAB91) else Color(0xffFF5722),
                gradientColor = Color(0xffFF5722),
                text = "Touch Sounds",
                selected = selectedState.value,
                onSelected = { selectedState.value = !selectedState.value })
        }
    }
}
Any ideas? Thanks!
s
You need different states for each Circle. Consider moving the selectedState inside the Circles. Also, looks like you copied the wrong code because the loop would never run as the list is empty
n
Ok, thanks for this Ashar, you've been a star!
s
Np man 🙂