Use checkboxes when people can enable or disable options independently.
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 com.composables.ui.components.Checkbox
import com.composables.ui.components.Text
@Composable
fun CheckboxExample() {
var checked by remember { mutableStateOf(true) }
Checkbox(checked = checked, onCheckedChange = { checked = it }) {
Text("Receive updates")
}
}Installation
implementation("com.composables:ui:0.1.0")Add the required dependencies
implementation("com.composables:composeunstyled:2.7.0")
Copy and paste the following sources into your project
components/Checkbox.kt
package com.composables.ui.components
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.background
import androidx.compose.foundation.border
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.RowScope
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.draw.clip
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.StrokeCap
import androidx.compose.ui.unit.dp
import com.composables.ui.theme.alphas
import com.composables.ui.theme.borderColor
import com.composables.ui.theme.defaultIndication
import com.composables.ui.theme.colors
import com.composables.ui.theme.controlColor
import com.composables.ui.theme.inverseIndication
import com.composables.ui.theme.disabledAlpha
import com.composables.ui.theme.ringColor
import com.composables.ui.theme.indications
import com.composables.ui.theme.onPanelColor
import com.composables.ui.theme.onPrimaryColor
import com.composables.ui.theme.primaryColor
import com.composeunstyled.CheckedIndicator
import com.composeunstyled.ProvideContentColor
import com.composeunstyled.UnstyledCheckbox
import com.composeunstyled.buildModifier
import com.composeunstyled.theme.Theme
/**
* A checkbox for independent binary selections.
*
* @param checked Whether the checkbox is currently checked.
* @param onCheckedChange Called when the checked state changes.
* @param modifier Modifier applied to the checkbox row.
* @param enabled Whether the checkbox can be interacted with.
* @param accessibilityLabel Accessible label announced for the checkbox.
* @param interactionSource Interaction source used for focus and press state.
* @param content Optional label or supporting content displayed next to the checkbox.
*/
@Composable
fun Checkbox(
checked: Boolean,
onCheckedChange: (Boolean) -> Unit,
modifier: Modifier = Modifier,
enabled: Boolean = true,
accessibilityLabel: String? = null,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
content: (@Composable RowScope.() -> Unit)? = null,
) {
val backgroundColor = if (checked) Theme[colors][primaryColor] else Theme[colors][controlColor]
val contentColor = if (checked) Theme[colors][onPrimaryColor] else Color.Transparent
val borderColor = if (checked) Theme[colors][primaryColor] else Theme[colors][borderColor]
val activeIndication = if (checked) Theme[indications][inverseIndication] else Theme[indications][defaultIndication]
UnstyledCheckbox(
checked = checked,
onCheckedChange = onCheckedChange,
enabled = enabled,
accessibilityLabel = accessibilityLabel,
interactionSource = interactionSource,
indication = null,
modifier = modifier.then(buildModifier { if (!enabled) add(Modifier.alpha(Theme[alphas][disabledAlpha])) }),
) {
Row(
horizontalArrangement = Arrangement.spacedBy(10.dp),
verticalAlignment = Alignment.CenterVertically,
) {
CheckedIndicator(
enter = fadeIn(),
exit = fadeOut(),
modifier = Modifier
.focusRing(
interactionSource = interactionSource,
color = Theme[colors][ringColor],
shape = RoundedCornerShape(5.dp),
)
.clip(RoundedCornerShape(5.dp))
.background(backgroundColor, RoundedCornerShape(5.dp))
.border(1.dp, borderColor, RoundedCornerShape(5.dp))
.size(20.dp),
indication = activeIndication
) {
Box(Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
CheckMark(contentColor)
}
}
if (content != null) {
ProvideContentColor(Theme[colors][onPanelColor]) {
content()
}
}
}
}
}
@Composable
private fun CheckMark(color: Color) {
Canvas(Modifier.size(14.dp)) {
val strokeWidth = 2.dp.toPx()
drawLine(
color,
Offset(size.width * 0.2f, size.height * 0.52f),
Offset(size.width * 0.42f, size.height * 0.74f),
strokeWidth,
cap = StrokeCap.Round
)
drawLine(
color,
Offset(size.width * 0.42f, size.height * 0.74f),
Offset(size.width * 0.8f, size.height * 0.28f),
strokeWidth,
cap = StrokeCap.Round
)
}
}components/Utils.kt
package com.composables.ui.components
import androidx.compose.animation.core.Spring
import androidx.compose.animation.core.animateDpAsState
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.spring
import androidx.compose.animation.core.tween
import androidx.compose.foundation.interaction.InteractionSource
import androidx.compose.foundation.interaction.collectIsFocusedAsState
import androidx.compose.foundation.interaction.collectIsPressedAsState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.composables.ui.theme.colors
import com.composables.ui.theme.ringColor
import com.composeunstyled.FocusRingVisibility
import com.composeunstyled.collectIsFocusVisibleAsState
import com.composeunstyled.outline
import com.composeunstyled.theme.Theme
@Composable
fun Modifier.focusRing(
interactionSource: InteractionSource,
width: Dp = 2.dp,
color: Color = Theme[colors][ringColor],
shape: Shape = RectangleShape,
offset: Dp = 0.dp,
visibility: FocusRingVisibility = FocusRingVisibility.FocusVisible,
): Modifier {
val showFocusRing by if (visibility == FocusRingVisibility.FocusVisible) {
interactionSource.collectIsFocusVisibleAsState()
} else {
interactionSource.collectIsFocusedAsState()
}
val animatedWidth by animateDpAsState(
targetValue = if (showFocusRing) width else 0.dp,
animationSpec = tween(durationMillis = 120),
label = "FocusRingWidth",
)
return this then Modifier.outline(
width = animatedWidth,
color = color,
shape = shape,
offset = offset,
)
}
@Composable
fun Modifier.bouncyPress(
interactionSource: InteractionSource,
enabled: Boolean = true,
pressedScale: Float = 0.98f,
): Modifier {
val pressed by interactionSource.collectIsPressedAsState()
val scale by animateFloatAsState(
targetValue = if (enabled && pressed) pressedScale else 1f,
animationSpec = spring(
dampingRatio = Spring.DampingRatioMediumBouncy,
stiffness = Spring.StiffnessMediumLow,
),
label = "BouncyPressScale",
)
return this then Modifier.graphicsLayer {
scaleX = scale
scaleY = scale
}
}Examples
Disabled
import androidx.compose.runtime.Composable
import com.composables.ui.components.Checkbox
import com.composables.ui.components.Text
@Composable
fun DisabledCheckboxExample() {
Checkbox(checked = false, onCheckedChange = {}, enabled = false) {
Text("Receive updates")
}
}Hierarchical selection
For parent and child selection flows, use the tri-state checkbox variant.
See Tri-State Checkbox.
API Reference
Checkbox
A checkbox for independent binary selections.
@Composable
fun Checkbox(
checked: Boolean,
onCheckedChange: (Boolean) -> Unit,
modifier: Modifier = Modifier,
enabled: Boolean = true,
accessibilityLabel: String? = null,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
content: (@Composable RowScope.() -> Unit)? = null,
)
| Parameter | Type | Description |
|---|---|---|
checked |
Boolean |
Whether the checkbox is currently checked. |
onCheckedChange |
(Boolean) -> Unit |
Called when the checked state changes. |
modifier |
Modifier |
Modifier applied to the checkbox row. |
enabled |
Boolean |
Whether the checkbox can be interacted with. |
accessibilityLabel |
String? |
Accessible label announced for the checkbox. |
interactionSource |
MutableInteractionSource |
Interaction source used for focus and press state. |
content |
(@Composable RowScope.() -> Unit)? |
Optional label or supporting content displayed next to the checkbox. |