Composables
Docs Login
Pairel

Preference Toggle

Jetpack Compose Component

Download
package com.composables.pairel.components

import androidx.compose.animation.animateColorAsState
import androidx.compose.animation.core.tween
import androidx.compose.foundation.background
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.composables.compose.ripple.rememberRippleIndication
import com.composables.pairel.theme.Theme
import com.composeunstyled.Thumb
import com.composeunstyled.Track
import com.composeunstyled.UnstyledSwitch

@Composable
fun PreferenceToggle(
    label: String,
    initialChecked: Boolean,
    onCheckedChange: (Boolean) -> Unit,
    modifier: Modifier = Modifier,
) {
    val interactionSource = remember { MutableInteractionSource() }
    var checked by remember(initialChecked) { mutableStateOf(initialChecked) }
    val trackColor by
        animateColorAsState(
            targetValue = if (checked) Theme.colors.accent else Theme.colors.surfaceVariant,
            animationSpec = tween(durationMillis = 180),
            label = "Toggle track",
        )

    UnstyledSwitch(
        checked = checked,
        onCheckedChange = {
            checked = it
            onCheckedChange(it)
        },
        modifier = modifier.clip(Theme.shapes.small).background(Theme.colors.surface),
        interactionSource = interactionSource,
        indication = rememberRippleIndication(color = Theme.colors.onSurface),
        accessibilityLabel = label,
    ) {
        Row(
            Modifier.padding(vertical = 8.dp),
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.spacedBy(12.dp),
        ) {
            Text(
                label,
                modifier = Modifier.weight(1f),
                style = Theme.typography.body.copy(fontSize = 14.sp),
            )
            Track(
                modifier =
                    Modifier.size(width = 44.dp, height = 26.dp)
                        .clip(CircleShape)
                        .background(trackColor)
            ) {
                Thumb(
                    animationSpec = tween(durationMillis = 180),
                    modifier =
                        Modifier.padding(3.dp)
                            .size(20.dp)
                            .clip(CircleShape)
                            .background(Theme.colors.surface),
                )
            }
        }
    }
}

More components from Pairel

by Alex Styl

Search screens and components

Screen flows

Screens

UI Elements

One Android lesson, every Wednesday

Building Android apps?

What Alex learned building Android apps for 14 years, so you don't have to. Reads in under a minute.

Join 3,500+ Android developers. Unsubscribe any time.

You're subscribed

The next lesson lands in on Wednesday.