Vladimir
06/18/2024, 3:34 PM@Composable
fun Spinner(itemList: List<String>,
selectedItem: String,
onItemSelected: (selectedItem: String) -> Unit
) {
var expanded by rememberSaveable() {
mutableStateOf(false)
}
Box(contentAlignment = Alignment.Center, modifier = Modifier.fillMaxWidth()) {
OutlinedButton(
onClick = { expanded = true },
shape = RectangleShape,
modifier = Modifier.fillMaxWidth(0.8f)
) {
Text(
text = selectedItem,
overflow = TextOverflow.Ellipsis,
maxLines = 1,
modifier = Modifier.weight(
1f
)
)
Icon(Icons.Default.ArrowDropDown, contentDescription = null)
}
DropdownMenu(
expanded = expanded,
onDismissRequest = { expanded = false },
modifier = Modifier.fillMaxWidth(0.8f)
) {
itemList.forEach {
DropdownMenuItem(onClick = {
expanded = false
onItemSelected(it)
}, text = { Text(text = it) })
}
}
}
}
While the OutlinedButton
respects the contentAlignment = Alignment.Center
rule provided by the parent Box element, the DropdownMenu
does not, even though it is also inside the aforementioned parent? What am I doing wrong?
Also, it appears it is necessary for child elements to provide their own maximum widths, they cannot automatically extend to the maximum width of the parent element?