Skip to content

Switch

Switch is a basic toggle component in Miuix used to switch between two states. It provides an interactive switch control with animation effects, suitable for enabling and disabling settings.

Import

kotlin
import top.yukonga.miuix.kmp.basic.Switch

Basic Usage

The Switch component can be used to toggle between two states:

kotlin
var checked by remember { mutableStateOf(false) }

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

Component States

Disabled State

kotlin
var checked by remember { mutableStateOf(false) }

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

Properties

Switch Properties

Property NameTypeDescriptionDefault ValueRequired
checkedBooleanWhether the switch is checked-Yes
onCheckedChange((Boolean) -> Unit)?Callback when switch state changes-No
modifierModifierModifier applied to the switchModifierNo
colorsSwitchColorsColor configuration for the switchSwitchDefaults.switchColors()No
enabledBooleanWhether the switch is interactivetrueNo

SwitchDefaults Object

The SwitchDefaults object provides default color configurations for the Switch component.

Methods

Method NameTypeDescription
switchColors()SwitchColorsCreates default color config for the switch

SwitchColors Class

Property NameTypeDescriptionDefault ValueRequired
checkedThumbColorColorThumb color when checked-Yes
uncheckedThumbColorColorThumb color when unchecked-Yes
disabledCheckedThumbColorColorThumb color when disabled and checked-Yes
disabledUncheckedThumbColorColorThumb color when disabled and unchecked-Yes
checkedTrackColorColorTrack color when checked-Yes
uncheckedTrackColorColorTrack color when unchecked-Yes
disabledCheckedTrackColorColorTrack color when disabled and checked-Yes
disabledUncheckedTrackColorColorTrack color when disabled and unchecked-Yes

Advanced Usage

Custom Colors

kotlin
var checked by remember { mutableStateOf(false) }

Switch(
    checked = checked,
    onCheckedChange = { checked = it },
    colors = SwitchDefaults.switchColors(
        checkedTrackColor = Color.Red,
        checkedThumbColor = Color.White
    )
)

Using with Text

kotlin
var checked by remember { mutableStateOf(false) }

Row(
    verticalAlignment = Alignment.CenterVertically,
    modifier = Modifier.padding(16.dp)
) {
    Switch(
        checked = checked,
        onCheckedChange = { checked = it }
    )
    Spacer(modifier = Modifier.width(8.dp))
    Text(text = if (checked) "Enabled" else "Disabled")
}

Using in Lists

kotlin
val options = listOf("Airplane Mode", "Bluetooth", "Location Services")
val checkedStates = remember { mutableStateListOf(false, true, false) }

LazyColumn {
    items(options.size) { index ->
        Row(
            modifier = Modifier.fillMaxWidth().padding(16.dp),
            horizontalArrangement = Arrangement.SpaceBetween,
            verticalAlignment = Alignment.CenterVertically
        ) {
            Text(text = options[index])
            Switch(
                checked = checkedStates[index],
                onCheckedChange = { checkedStates[index] = it }
            )
        }
    }
}

Clickable List Row

kotlin
data class Option(val text: String, var isSelected: Boolean)
val options = remember {
    mutableStateListOf(
        Option("Airplane Mode", false),
        Option("Bluetooth", true),
        Option("Location Services", false)
    )
}

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

Released under the Apache-2.0 License