Sean Proctor
10/28/2022, 8:03 PMSean Proctor
10/28/2022, 8:05 PM@Composable
actual fun DropdownSelector(
modifier: Modifier,
selectedIndex: Int,
items: List<String>,
label: String,
errorRes: StringResource?,
onChange: (Int) -> Unit,
) {
var expanded by remember { mutableStateOf(false) }
val selectedItemText = when {
items.isEmpty() -> ""
selectedIndex < 0 -> items[0]
else -> items[selectedIndex]
}
val indicatorColor = when {
errorRes != null -> MaterialTheme.colors.error.copy(alpha = ContentAlpha.high)
expanded -> MaterialTheme.colors.primary.copy(alpha = ContentAlpha.high)
else -> MaterialTheme.colors.onSurface.copy(alpha = 0.42f)
}
Column(modifier) {
OutlinedTextField(
value = selectedItemText,
label = { Text(label) },
onValueChange = {},
readOnly = true,
trailingIcon = {
IconButton(
onClick = {
expanded = !expanded
},
) {
Icon(
imageVector = Icons.Default.ArrowDropDown,
contentDescription = "select",
Modifier.rotate(
if (expanded) 180f else 0f
),
)
}
},
isError = errorRes != null
)
DropdownMenu(
expanded = expanded,
onDismissRequest = {
expanded = false
},
) {
items.forEachIndexed { index, item ->
DropdownMenuItem(
onClick = {
onChange(index)
expanded = false
}
) {
Text(text = item)
}
}
}
if (errorRes != null) {
Text(
text = label,
modifier = Modifier
.background(color = MaterialTheme.colors.background),
style = MaterialTheme.typography.caption.copy(color = indicatorColor)
)
}
}
}
Sean Proctor
10/28/2022, 8:08 PMIconButton
while the popup is showing, causes both actions to happen, which is clearly a mistake. Unfortunately the onDismissRequest
is called first.orangy
enabled = !expanded
on icon button?Sean Proctor
10/28/2022, 8:25 PMonClick
for the icon button and the onDismissRequest
for the DropdownMenu
orangy
Sean Proctor
10/28/2022, 8:26 PMSean Proctor
10/28/2022, 8:27 PMorangy
Sean Proctor
10/28/2022, 8:29 PMSean Proctor
10/28/2022, 8:29 PMorangy
IconButton
, it’s trivial, and remove the line about providing disabled ContentAlpha 🙂orangy
Sean Proctor
10/28/2022, 8:33 PMorangy
DropDownMenu
should be capturing mouse input and consume click outside
cc @Igor DeminSean Proctor
10/28/2022, 8:39 PM