Skip to content

Checkbox

Checkbox is a basic selection component in Miuix, supporting three states: checked, unchecked, and indeterminate. It provides an interactive selection control with animation effects, suitable for multiple selection scenarios and enabling/disabling configuration items.

Import

kotlin
import top.yukonga.miuix.kmp.basic.Checkbox
import androidx.compose.ui.state.ToggleableState

Basic Usage

The Checkbox component can be used to create selectable options:

kotlin
var state by remember { mutableStateOf(ToggleableState.Off) }

Checkbox(
    state = state,
    onClick = { state = if (state == ToggleableState.On) ToggleableState.Off else ToggleableState.On }
)

Component States

Disabled State

kotlin
Checkbox(
    state = ToggleableState.Off,
    onClick = null,
    enabled = false
)

Indeterminate State

The indeterminate state is typically used to represent a parent checkbox where only some child checkboxes are selected. When indeterminate, a horizontal dash is shown instead of a checkmark.

kotlin
var parentState by remember { mutableStateOf(ToggleableState.Indeterminate) }
val childStates = remember { mutableStateListOf(true, false, true) }

// Cycle: Off → Indeterminate → On → Off
Checkbox(
    state = parentState,
    onClick = {
        parentState = when (parentState) {
            ToggleableState.Off -> ToggleableState.Indeterminate
            ToggleableState.Indeterminate -> ToggleableState.On
            ToggleableState.On -> ToggleableState.Off
        }
    }
)

Properties

Checkbox Properties

Property NameTypeDescriptionDefault ValueRequired
stateToggleableStateThe current state of the Checkbox (On, Off, or Indeterminate)-Yes
onClick(() -> Unit)?Callback when the Checkbox is clicked; null = non-interactive-Yes
modifierModifierModifier applied to the CheckboxModifierNo
colorsCheckboxColorsColor configuration for the CheckboxCheckboxDefaults.checkboxColors()No
enabledBooleanWhether the Checkbox is interactivetrueNo

CheckboxDefaults Object

The CheckboxDefaults object provides default color configurations for the Checkbox component.

Methods

Method NameTypeDescription
checkboxColors()CheckboxColorsCreates default color config for checkbox

CheckboxColors Class

Property NameTypeDescription
checkedForegroundColorColorForeground color when checked (checkmark)
uncheckedForegroundColorColorForeground color when unchecked
disabledCheckedForegroundColorColorForeground color when disabled and checked
disabledUncheckedForegroundColorColorForeground color when disabled and unchecked
checkedBackgroundColorColorBackground color when checked
uncheckedBackgroundColorColorBackground color when unchecked
disabledCheckedBackgroundColorColorBackground color when disabled and checked
disabledUncheckedBackgroundColorColorBackground color when disabled and unchecked

The indeterminate state shares the same colors as the checked state.

Advanced Usage

Custom Colors

kotlin
var state by remember { mutableStateOf(ToggleableState.Off) }

Checkbox(
    state = state,
    onClick = { state = if (state == ToggleableState.On) ToggleableState.Off else ToggleableState.On },
    colors = CheckboxDefaults.checkboxColors(
        checkedBackgroundColor = Color.Red,
        checkedForegroundColor = Color.White
    )
)

Parent-Child Checkboxes (Indeterminate Pattern)

kotlin
val childStates = remember { mutableStateListOf(false, true, false) }
val parentState by remember {
    derivedStateOf {
        when {
            childStates.all { it } -> ToggleableState.On
            childStates.none { it } -> ToggleableState.Off
            else -> ToggleableState.Indeterminate
        }
    }
}

Column {
    Checkbox(
        state = parentState,
        onClick = {
            val newValue = parentState != ToggleableState.On
            childStates.indices.forEach { childStates[it] = newValue }
        }
    )
    childStates.forEachIndexed { index, checked ->
        Checkbox(
            state = ToggleableState(checked),
            onClick = { childStates[index] = !childStates[index] }
        )
    }
}

Using with Text

kotlin
var state by remember { mutableStateOf(ToggleableState.Off) }

Row(
    verticalAlignment = Alignment.CenterVertically,
    modifier = Modifier.padding(16.dp)
) {
    Checkbox(
        state = state,
        onClick = { state = if (state == ToggleableState.On) ToggleableState.Off else ToggleableState.On }
    )
    Spacer(modifier = Modifier.width(8.dp))
    Text(text = "Accept Terms and Privacy Policy")
}

Using in Lists

kotlin
val options = listOf("Option A", "Option B", "Option C")
val checkedStates = remember { mutableStateListOf(false, true, false) }

LazyColumn {
    items(options.size) { index ->
        Row(
            modifier = Modifier.fillMaxWidth().padding(16.dp),
            verticalAlignment = Alignment.CenterVertically
        ) {
            Checkbox(
                state = ToggleableState(checkedStates[index]),
                onClick = { checkedStates[index] = !checkedStates[index] }
            )
            Spacer(modifier = Modifier.width(8.dp))
            Text(text = options[index])
        }
    }
}

Migration from Deprecated API

The old checked: Boolean / onCheckedChange API is deprecated. Migrate as follows:

kotlin
// Before (deprecated)
Checkbox(
    checked = checked,
    onCheckedChange = { checked = it }
)

// After
Checkbox(
    state = ToggleableState(checked),
    onClick = { checked = !checked }
)

Changelog

Released under the Apache-2.0 License