I want to display a dropdown menu in my App under TopAppBar.
When I click the dropdowm menu, the list is displayed, but nothing happens when I select an item from the list. Moreover the dropdown menu is not collapsing. It looks like I can't click anything in the app anymore. What am I missing?
val countriesList = mutableListOf<String>("India", "USA", "Canada", "Germany","Australia")
@Composable
fun CountrySelection() {
// State variables
var countryName: String by remember { mutableStateOf(countriesList[0]) }
var expanded by remember { mutableStateOf(false)}
Box(Modifier.fillMaxWidth(),contentAlignment = Alignment.Center) {
Row(
Modifier
.padding(24.dp)
.clickable {
expanded = !expanded
}
.padding(8.dp),
horizontalArrangement = Arrangement.Center,
verticalAlignment = Alignment.CenterVertically
) { // Anchor view
Text(text = countryName,fontSize = 18.sp,modifier = Modifier.padding(end = 8.dp)) // Country name label
Icon(imageVector = Icons.Filled.ArrowDropDown, contentDescription = "")
//
DropdownMenu(expanded = expanded, onDismissRequest = {
expanded = false
}) {
countriesList.forEach{ country->
DropdownMenuItem(onClick = {
expanded = false
countryName = country
}) {
Text(text = country)
}
}
}
}
}
}