Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jetpack Compose Material3 - Label for Switch

I need to implement a switch with a label with Jetpack Compose and Material3.

My solution so far (it basically just extends the existing switch component and adds the label property):

@Composable
fun LabeledSwitch(
    checked: Boolean,
    onCheckedChange: ((Boolean) -> Unit)?,
    modifier: Modifier = Modifier,
    thumbContent: (@Composable () -> Unit)? = null,
    enabled: Boolean = true,
    interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
    colors: SwitchColors = SwitchDefaults.colors(),
    label: (@Composable () -> Unit),
) {
    Row(
        horizontalArrangement = Arrangement.SpaceBetween,
        verticalAlignment = Alignment.CenterVertically
    ) {
        label()
        Switch(
            checked = checked,
            onCheckedChange = onCheckedChange,
            thumbContent = thumbContent,
            enabled = enabled,
            interactionSource = interactionSource,
            colors = colors
        )
    }
}

This correctly displays the label (e.g. {Text("Test")}) next to the switch.

However, I would like to forward all click events on the label to the switch, so that you can touch the label to toggle the switch value.

It basically should work like the old <Switch> component in XML layouts.

One idea I had was adding a modifier to the container like so:

Row(
    horizontalArrangement = Arrangement.SpaceBetween,
    verticalAlignment = Alignment.CenterVertically,
    modifier = modifier.then(Modifier
        .clickable { onCheckedChange?.invoke(!checked) }
    )

But this is not optimal as it shows a ripple effect on the whole item.

Is there any better solution? Maybe even without a custom component?

like image 924
SapuSeven Avatar asked Jul 31 '26 21:07

SapuSeven


1 Answers

You can do it like this. If you don't want ripple on Switch set onCheckedChange = null

enter image description here

@Composable
private fun SwitchWithLabel(label: String, state: Boolean, onStateChange: (Boolean) -> Unit) {

    val interactionSource = remember { MutableInteractionSource() }
    Row(
        modifier = Modifier
            .clickable(
                interactionSource = interactionSource,
                // This is for removing ripple when Row is clicked
                indication = null,
                role = Role.Switch,
                onClick = {
                    onStateChange(!state)
                }
            )
            .padding(8.dp),
        verticalAlignment = Alignment.CenterVertically

    ) {

        Text(text = label)
        Spacer(modifier = Modifier.padding(start = 8.dp))
        Switch(
            checked = state,
            onCheckedChange = {
                onStateChange(it)
            }
        )
    }
}
like image 196
Thracian Avatar answered Aug 02 '26 14:08

Thracian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!