Composables
Docs Login
Pairel

Checkbox

Jetpack Compose Component

Download
package com.composables.pairel.components

import androidx.compose.animation.animateColorAsState
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.scaleIn
import androidx.compose.animation.scaleOut
import androidx.compose.foundation.background
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.size
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 com.composables.compose.ripple.rememberRippleIndication
import com.composables.pairel.theme.Check
import com.composables.pairel.theme.Icons
import com.composables.pairel.theme.Space
import com.composables.pairel.theme.Theme
import com.composeunstyled.CheckedIndicator
import com.composeunstyled.UnstyledCheckbox

@Composable
fun Checkbox(
    label: String,
    initialChecked: Boolean,
    onCheckedChange: (Boolean) -> Unit,
    modifier: Modifier = Modifier,
    enabled: Boolean = true,
) {
    var checked by remember(initialChecked) { mutableStateOf(initialChecked) }
    val boxColor by
        animateColorAsState(
            if (checked) Theme.colors.accent else Theme.colors.surfaceVariant,
            label = "Checkbox color",
        )
    val interactionSource = remember { MutableInteractionSource() }
    UnstyledCheckbox(
        checked = checked,
        onCheckedChange = {
            checked = it
            onCheckedChange(it)
        },
        enabled = enabled,
        modifier = modifier.clip(Theme.shapes.small),
        accessibilityLabel = label,
        interactionSource = interactionSource,
        indication = rememberRippleIndication(color = Theme.colors.accent),
    ) {
        Row(
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.spacedBy(Space * 3),
        ) {
            Box(Modifier.size(24.dp).clip(Theme.shapes.small).background(boxColor)) {
                CheckedIndicator(
                    modifier = Modifier.size(24.dp),
                    enter = fadeIn() + scaleIn(),
                    exit = fadeOut() + scaleOut(),
                ) {
                    Icon(Icons.Check, contentDescription = null, tint = Theme.colors.onAccent)
                }
            }
            Text(
                label,
                color = if (enabled) Theme.colors.onSurface else Theme.colors.onBackgroundMuted,
            )
        }
    }
}

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.