[英]DropDownMenu not showing when click in compose
I have an image and I want to show a dropdownMenuItem when user click in the image.我有一张图片,我想在用户点击图片时显示一个 dropdownMenuItem。 I was debugging the app and I can see that the code go through the DropdownDemo method but is not showing anything.我正在调试应用程序,我可以通过 DropdownDemo 方法看到代码 go,但没有显示任何内容。
Am I doing something wrong?难道我做错了什么?
Click code :点击代码:
@Composable
fun Header(currentItem: CartListItems) {
var showDialog by remember { mutableStateOf(false) }
Box(Modifier.fillMaxWidth()) {
Text(modifier = Modifier.align(Alignment.TopStart),
text = currentItem.type,
color = colorResource(id = R.color.app_grey_dark),
fontSize = 12.sp)
Image(painter = painterResource(R.drawable.three_dots),
contentDescription = "more options button",
Modifier
.align(Alignment.CenterEnd)
.width(24.dp)
.height(6.75.dp)
.clickable(indication = null,
interactionSource = remember { MutableInteractionSource() },
onClick = {
showDialog = true
}))
if(showDialog) {
DropdownDemo()
showDialog = false
}
}
}
Dropmenu:下拉菜单:
@Composable
fun DropdownDemo() {
var expanded by remember { mutableStateOf(false) }
val items = listOf("A", "B", "C", "D", "E", "F")
val disabledValue = "B"
var selectedIndex by remember { mutableStateOf(0) }
Box(modifier = Modifier
.fillMaxSize()
.wrapContentSize(Alignment.TopStart)) {
Text(items[selectedIndex],modifier = Modifier
.fillMaxWidth()
.clickable(onClick = { expanded = true })
.background(
Color.Gray
))
DropdownMenu(
expanded = expanded,
onDismissRequest = { expanded = false },
modifier = Modifier
.fillMaxWidth()
.background(
Color.Red
)
) {
items.forEachIndexed { index, s ->
DropdownMenuItem(onClick = {
selectedIndex = index
expanded = false
}) {
val disabledText = if (s == disabledValue) {
" (Disabled)"
} else {
""
}
Text(text = s + disabledText)
}
}
}
}
}
showDialog
appears to be a MutableState
object. Hence, when the image is clicked, it becomes true
, and a recomposition is triggered, after which the conditional block is executed, displaying the DropDownMenu
. showDialog
似乎是一个MutableState
object。因此,当单击图像时,它变为true
,并触发重组,之后执行条件块,显示DropDownMenu
。 However, in the very next line.但是,在下一行。 You equate showDialog
to false
, re-trigerring a recomposition, and rendering the DropDownMenu
collapsed.您将showDialog
等同于false
,重新触发重组,并呈现DropDownMenu
折叠。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.