I am trying to design a UI like this in Jetpack Compose:
Whenever an option is selected, it should get underlined in the UI as shown above. However, when I try to arrange it in Jetpack compose I get a very weird behavior:
Here's what I tried:
@Composable
fun PaymentsScreen(navController: NavController) {
var textState by remember { mutableStateOf("EA DUMPLING CARDS") }
TitleOptionBar(
options = listOf("EA DUMPLING CARDS", "OTHER PAYMENTS"),
selectedOption = textState,
onOptionSelected = {newOption -> textState = newOption}
)
}
@Composable
fun TitleOptionBar(
options: List<String>,
selectedOption: String,
onOptionSelected: (String) -> Unit
) {
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceEvenly
) {
options.forEach { option ->
// Create individual option views here
Box(
modifier = Modifier.weight(weight = 1F).clickable { onOptionSelected(option) },
contentAlignment = Alignment.Center
) {
Text(
text = option,
modifier = Modifier.padding(16.dp),
fontWeight = if (option == selectedOption) FontWeight.Bold else FontWeight.Light
)
}
if (option == selectedOption) {
Box(
modifier = Modifier
.height(5.dp)
.background(Color.Blue)
.weight(weight = 1F)
) {
// This line will appear under the selected box
}
}
}
}
}
What should I change in the code?
You can try something like this:
Also, you can checkout material tab.