Skip to content

Instantly share code, notes, and snippets.

@7kashif
Created March 22, 2022 18:17
Show Gist options
  • Save 7kashif/6efb428444062cfe3a14f40ab144b083 to your computer and use it in GitHub Desktop.
Save 7kashif/6efb428444062cfe3a14f40ab144b083 to your computer and use it in GitHub Desktop.
A toggle button with more than one states.
@Composable
fun TriStateToggle() {
val states = listOf(
"State 1",
"State 2",
"State 3",
)
var selectedOption by remember {
mutableStateOf(states[1])
}
val onSelectionChange = { text: String ->
selectedOption = text
}
Surface(
shape = RoundedCornerShape(24.dp),
elevation = 4.dp,
modifier = Modifier
.wrapContentSize()
) {
Row(
modifier = Modifier
.clip(shape = RoundedCornerShape(24.dp))
.background(Color.LightGray)
) {
states.forEach { text->
Text(
text = text,
color = Color.White,
modifier = Modifier
.clip(shape = RoundedCornerShape(24.dp))
.clickable {
onSelectionChange(text)
}
.background(
if (text == selectedOption) {
Color.Magenta
} else {
Color.LightGray
}
)
.padding(
vertical = 12.dp,
horizontal = 16.dp,
),
)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment