Created
March 22, 2022 18:17
-
-
Save 7kashif/6efb428444062cfe3a14f40ab144b083 to your computer and use it in GitHub Desktop.
A toggle button with more than one states.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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