State of Compose 2023 results are in! Click here to learn more

← Back to Material Compose

Switch

Component
in
Material
. Since 0.1.0-dev15

Overview

Code Examples

<a href="https://material.io/components/switches" class="external" target="_blank">Material Design switch</a>.

Switches toggle the state of a single item on or off.

!Switches image(https://developer.android.com/images/reference/androidx/compose/material/switches.png)

Overloads

Switch

@Composable
@OptIn(ExperimentalMaterialApi::class)
fun Switch(
    checked: Boolean,
    onCheckedChange: ((Boolean) -> Unit)?,
    modifier: Modifier = Modifier,
    enabled: Boolean = true,
    interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
    colors: SwitchColors = SwitchDefaults.colors()
)

Parameters

NameDescription
checkedwhether or not this component is checked
onCheckedChangecallback to be invoked when Switch is being clicked, therefore the change of checked state is requested. If null, then this is passive and relies entirely on a higher-level component to control the "checked" state.
modifierModifier to be applied to the switch layout
enabledwhether the component is enabled or grayed out
interactionSourcethe MutableInteractionSource representing the stream of Interactions for this Switch. You can create and pass in your own remembered MutableInteractionSource if you want to observe Interactions and customize the appearance / behavior of this Switch in different Interactions.
colorsSwitchColors that will be used to determine the color of the thumb and track in different states. See SwitchDefaults.colors