https://kotlinlang.org logo
#compose
Title
# compose
n

Nat Strangerweather

10/31/2020, 5:41 PM
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

Se7eN

10/31/2020, 6:05 PM
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

Nat Strangerweather

10/31/2020, 6:07 PM
Ok, thanks for this Ashar, you've been a star!
s

Se7eN

11/01/2020, 9:42 AM
Np man 🙂
4 Views