Jeff Jackson
02/09/2022, 9:04 PMDropdownMenuItem
s where one has a selection state (check mark)?Chris Sinco [G]
02/10/2022, 4:52 PMDropdownMenuItem
has a content slot so you are free to put whatever you want on it based on state.Chris Sinco [G]
02/10/2022, 4:52 PMJeff Jackson
02/10/2022, 6:52 PMenum class City(val title: String) {
NewYork("New York"),
London("London"),
Paris("Paris"),
Munich("Munich");
}
class MenuTestViewModel: ViewModel() {
var city by mutableStateOf(City.London)
}
@Composable
fun MenuTest(viewModel: MenuTestViewModel = MenuTestViewModel()) {
Scaffold (
topBar = {
TopAppBar(
title = { Text(text = viewModel.city.title) },
actions = {
OverflowMenu(viewModel = viewModel)
}
)
}
) {
}
}
@Composable
fun OverflowMenu(viewModel: MenuTestViewModel) {
var expanded by remember { mutableStateOf(false) }
Button(onClick = {
expanded = true
} ) {
Icon(Icons.Filled.MoreVert, "")
DropdownMenu(
expanded = expanded,
onDismissRequest = { expanded = false }
) {
City.values().forEach {
DropdownMenuItem(
onClick = {
viewModel.city = it
expanded = false
}
) {
val alpha = if (it == viewModel.city) 1f else 0f
Icon(Icons.Filled.Check, "", modifier = Modifier.alpha(alpha))
Box(modifier = Modifier.width(8.dp))
Text(it.title)
}
}
}
}
}
Chris Sinco [G]
02/10/2022, 7:37 PMJeff Jackson
02/11/2022, 4:46 PMSpacer
to work at all. And it’s a total hack to have to set the alpha of the Icon
in order to get the spacing of that right.Chris Sinco [G]
02/11/2022, 5:53 PMModifier.requiredSize(24.dp)
or whatever size you want. This should fix the title issues.Chris Sinco [G]
02/11/2022, 5:54 PMChris Sinco [G]
02/11/2022, 5:54 PMChris Sinco [G]
02/12/2022, 4:40 PM