The state a style that can be updated to reflect the current state of a component.
StyleStateSample
@Composable
fun StyleStateSample() {
// Create a styleable clickable box
@Composable
fun ClickableStyleableBox(
onClick: () -> Unit,
modifier: Modifier = Modifier,
style: Style = Style,
enabled: Boolean = true,
) {
val interactionSource = remember { MutableInteractionSource() }
val styleState = rememberUpdatedStyleState(interactionSource) { it.isEnabled = enabled }
Box(
modifier =
modifier
.clickable(interactionSource = interactionSource, onClick = onClick)
.styleable(styleState, style)
)
}
// Create a 150x150 green box that is clickable
ClickableStyleableBox(
onClick = {},
style = {
background(Color.Green)
size(150.dp)
hovered { background(Color.Yellow) }
pressed { background(Color.Red) }
disabled { background(Color.Gray) }
},
)
}