Clicking outside of DropdownMenu does not dismiss the menu, DropdownMenuItem does not receive clicks

Viewed 388

I am experimenting DropdownMenu and from the documentation, it should call onDismissRequest when I tap outside:

onDismissRequest will be called when the menu should close - for example when there is a tap outside the menu, or when the back key is pressed.

Clicking on "back" can successfully dismiss the menu.

Another weird thing is that clicking on an DropdownMenuItem does not close the menu, either since it cannot receive any clicking.

class MainActivity : ComponentActivity() {

    @ExperimentalMaterialApi
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
                SampleScreen()
        }
    }
}

@Composable
fun SampleScreen() {
    Scaffold(bottomBar = {MyBottomBar()}) {
        Text("hello world")
    }
}

@Composable
private fun MyBottomBar() {
    var expanded by remember { mutableStateOf(false) }
    BottomAppBar {
        Box(modifier = Modifier.fillMaxWidth()) {
            Row(modifier = Modifier.align(Alignment.CenterEnd)) {
                Box {
                    IconButton(
                        onClick = {expanded = true}
                    ) {
                        Icon(
                            Icons.Filled.MoreVert,
                            contentDescription = "more"
                        )
                    }
                    DropdownMenu(
                        expanded = expanded,
                        onDismissRequest = {
                            expanded = false
                        }
                    ) {
                        DropdownMenuItem(onClick = { expanded = false }) {
                            Text("item1")
                        }
                        DropdownMenuItem(onClick = { expanded = false }) {
                            Text("item2")
                        }
                    }
                }
            }
        }
    }
}

Screen:

sample screen

0 Answers
Related