Skip to content

CheckBox

CheckBox is a basic selection component in Miuix, used for toggling between checked and unchecked states. 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

Basic Usage

The CheckBox component can be used to create selectable options:

kotlin
var checked by remember { mutableStateOf(false) }

Checkbox(
    checked = checked,
    onCheckedChange = { checked = it }
)

Component States

Disabled State

kotlin
var checked by remember { mutableStateOf(false) }

Checkbox(
    checked = checked,
    onCheckedChange = { checked = it },
    enabled = false
)

Properties

Checkbox Properties

Property NameTypeDescriptionDefault ValueRequired
checkedBooleanWhether checkbox is checked-Yes
onCheckedChange((Boolean) -> Unit)?Callback when check state changes-Yes
modifierModifierModifier applied to the checkboxModifierNo
colorsCheckboxColorsColor configuration for checkboxCheckboxDefaults.checkboxColors()No
enabledBooleanWhether 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

Advanced Usage

Custom Colors

kotlin
var checked by remember { mutableStateOf(false) }

Checkbox(
    checked = checked,
    onCheckedChange = { checked = it },
    colors = CheckboxDefaults.checkboxColors(
        checkedBackgroundColor = Color.Red,
        checkedForegroundColor = Color.White
    )
)

Using with Text

kotlin
var checked by remember { mutableStateOf(false) }

Row(
    verticalAlignment = Alignment.CenterVertically,
    modifier = Modifier.padding(16.dp)
) {
    Checkbox(
        checked = checked,
        onCheckedChange = { checked = it }
    )
    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(
                checked = checkedStates[index],
                onCheckedChange = { checkedStates[index] = it }
            )
            Spacer(modifier = Modifier.width(8.dp))
            Text(text = options[index])
        }
    }
}

Clickable Row in List

kotlin
data class Option(val text: String, var isSelected: Boolean)
val options = remember {
    mutableStateListOf(
        Option("Option 1", false),
        Option("Option 2", true),
        Option("Option 3", false)
    )
}

LazyColumn {
    items(options.size) { index ->
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .clickable {
                    options[index] = options[index].copy(
                        isSelected = !options[index].isSelected
                    )
                }
                .padding(16.dp),
            verticalAlignment = Alignment.CenterVertically
        ) {
            Checkbox(
                checked = options[index].isSelected,
                onCheckedChange = { selected ->
                    options[index] = options[index].copy(isSelected = selected)
                }
            )
            Spacer(modifier = Modifier.width(16.dp))
            Text(text = options[index].text)
        }
    }
}

Released under the Apache-2.0 License