Trying to create a dropdown menu with at least 50 items using the compose code below:
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun Spinner(label: String, options: List<String>) {
var expanded by remember { mutableStateOf(false) }
var text by remember { mutableStateOf(options[0]) }
ExposedDropdownMenuBox(
expanded = expanded,
onExpandedChange = { expanded = it },
) {
TextField(
// The `menuAnchor` modifier must be passed to the text field to handle
// expanding/collapsing the menu on click. A read-only text field has
// the anchor type `PrimaryNotEditable`.
modifier = Modifier.menuAnchor(),
value = text,
onValueChange = {},
readOnly = true,
singleLine = true,
label = { Text(label) },
trailingIcon = { ExposedDropdownMenuDefaults.TrailingIcon(expanded = expanded) },
colors = ExposedDropdownMenuDefaults.textFieldColors(),
)
ExposedDropdownMenu(
expanded = expanded,
onDismissRequest = { expanded = false },
) {
options.forEach { option ->
DropdownMenuItem(
text = { Text(option, style = MaterialTheme.typography.bodyLarge) },
onClick = {
text = option
expanded = false
},
contentPadding = ExposedDropdownMenuDefaults.ItemContentPadding,
)
}
}
}
}
The code functions correctly but exhibits noticeable lag compared to a View equivalent. Additionally, the menu fails to close when the trailing icon is clicked.
How can I optimize the code to achieve performance comparable to the View spinner and ensure the menu closes when the trailing icon is clicked?