https://kotlinlang.org logo
Title
k

KotlinLeaner

07/29/2022, 2:29 PM
Another question, when I am trying to click on single item, it's clicking on whole another items as well instead of single and color is not changing as well.
binding.itemComposable.setContent {
    val interactionSource = remember { MutableInteractionSource() }
    val isPressed by interactionSource.collectIsPressedAsState()
    val backgroundColor = if (isPressed) DuckEggBlue else OffWhite
    val clickable = Modifier.clickable(
        interactionSource = interactionSource,
        indication = LocalIndication.current
    ) {
        println("Item Click")
    }
    Column(
        modifier = Modifier
            .fillMaxSize(),
        verticalArrangement = Arrangement.spacedBy(12.dp)
    ) {
        val options = getOptions()
        options.forEachIndexed { _, optionText ->
            Card(
                shape = RoundedCornerShape(4.dp),
                modifier = Modifier.then(clickable)
            ) {
                Text(
                    modifier = Modifier
                        .fillMaxWidth()
                        .background(backgroundColor)
                        .padding(16.dp),
                    text = optionText,
                    style = Typography.h3,
                    fontWeight = FontWeight.Medium,
                    color = Slate
                )
            }
        }
    }
}
s

Stylianos Gakis

07/29/2022, 3:26 PM
You’re passing the same instance of interactionSource to all of your items. You could move the block from
val interactionSource
until before
Column
to inside the lambda
options.forEachIndexed {
so each item gets its own instance.
k

KotlinLeaner

07/29/2022, 3:27 PM
So how can I fix this problem?
s

Stylianos Gakis

07/29/2022, 3:28 PM
You could move the block from
val interactionSource
until before
Column
to inside the lambda
options.forEachIndexed {
so each item gets its own instance.
k

KotlinLeaner

07/29/2022, 3:28 PM
okk got it