why would this not work ```@Composable fun Chip( ...
# compose
o
why would this not work
Copy code
@Composable
fun Chip(
    label: String,
    enabled: MutableState<Boolean>,
    onClick: () -> Unit,
) {
    val (selected, setSelected) = enabled

    val chipColor =
        if (selected) MaterialTheme.colors.Action else MaterialTheme.colors.ForegroundMuted

    Surface(
        shape = RoundedCornerShape(15.dp),
        color = chipColor.copy(alpha = 0.2f),
        border = BorderStroke(
            width = 1.dp, color = chipColor
        ),
        modifier = Modifier
            .toggleable(
                value = selected,
                enabled = true,
                onValueChange = setSelected
            )
            .clickable {
                onClick()
            },
but this would
Copy code
modifier = Modifier
            .clickable {
                onClick()
                setSelected(!selected)
            },
Thanks
work as in successfully toggling the selected state, causing chip color to change
r
Because it looks like the last one specified (
toggleable
or
clickable
) takes precedence.
m
I suspect that toggleable and clickable would interfere with each other. And as @Rick Regan said above, the order matters, so it’s likely that toggleable is just getting overridden by clickable.
o
Ok so either or, yea makes sense thank you!
m
@oday To be honest, you may also just want to hoist the selected state out of your chip, and anyone who wants to know when the value was changed can react to it in the setSelected callback:
Copy code
@Composable
fun Chip(
    label: String,
    enabled: Boolean,
    selected: Boolean,
    setSelected (Boolean) -> Unit
)
I generally try to avoid any remember state in my controls unless it’s 100% necessary. Also, i would avoid passing MutableState instances to controls as well (i don’t even see where you’re using that anyway). It causes issues with recomposition when you do that. You should always pass a value and value setter instead.
It also has the nice side effect of making your Chip previewable in both on and off states