androidx.compose.material3

TriStateCheckbox

Composable function

One of the checkbox types. It works like Checkbox. Only the parameters that differ are described here.

A checkbox with a third state: partly checked. Use it for a "select all" box when only some items are checked.

A parent checkbox for a list
A parent checkbox for a list
val items = remember { mutableStateListOf(true, false, true) }
val parent = when {
    items.all { it } -> ToggleableState.On
    items.none { it } -> ToggleableState.Off
    else -> ToggleableState.Indeterminate
}
Column {
    Row(verticalAlignment = Alignment.CenterVertically) {
        TriStateCheckbox(state = parent, onClick = {
            val newValue = parent != ToggleableState.On
            items.indices.forEach { items[it] = newValue }
        })
        Text("All toppings")
    }
    listOf("Cheese", "Olives", "Basil").forEachIndexed { i, name ->
        Row(Modifier.padding(start = 24.dp), verticalAlignment = Alignment.CenterVertically) {
            Checkbox(checked = items[i], onCheckedChange = { items[i] = it })
            Text(name)
        }
    }
}

Basic

Uses the default line widths.

@Composable
fun TriStateCheckbox(
    state: ToggleableState,
    onClick: (() -> Unit)?,
    modifier: Modifier = Modifier,
    enabled: Boolean = true,
    colors: CheckboxColors = CheckboxDefaults.colors(),
    interactionSource: MutableInteractionSource? = null,
)

state: ToggleableStateRequired

The state of a TriStateCheckbox: On, Off or Indeterminate (a dash). Use it for a parent checkbox when only some of its children are checked.

A parent checkbox for a list
A parent checkbox for a list
val items = remember { mutableStateListOf(true, false, true) }
val parent = when {
    items.all { it } -> ToggleableState.On
    items.none { it } -> ToggleableState.Off
    else -> ToggleableState.Indeterminate
}
Column {
    Row(verticalAlignment = Alignment.CenterVertically) {
        TriStateCheckbox(state = parent, onClick = {
            val newValue = parent != ToggleableState.On
            items.indices.forEach { items[it] = newValue }
        })
        Text("All toppings")
    }
    listOf("Cheese", "Olives", "Basil").forEachIndexed { i, name ->
        Row(Modifier.padding(start = 24.dp), verticalAlignment = Alignment.CenterVertically) {
            Checkbox(checked = items[i], onCheckedChange = { items[i] = it })
            Text(name)
        }
    }
}

onClick: (() -> Unit)?Required

Called when a TriStateCheckbox is clicked. You decide the next state. Pass null if a whole row handles the click.

Same as in Checkbox: modifier, enabled, colors, interactionSource.

Custom strokesSince 1.9

Takes checkmarkStroke and outlineStroke to change line widths.

@Composable
fun TriStateCheckbox(
    state: ToggleableState,
    onClick: (() -> Unit)?,
    checkmarkStroke: Stroke,
    outlineStroke: Stroke,
    modifier: Modifier = Modifier,
    enabled: Boolean = true,
    colors: CheckboxColors = CheckboxDefaults.colors(),
    interactionSource: MutableInteractionSource? = null,
)

Same as above: state, onClick.

Same as in Checkbox: checkmarkStroke, outlineStroke, modifier, enabled, colors, interactionSource.

Checkbox types

See also