Jetpact Compose `DropdownMenu` 的 `toggle` 和 `toggleModifier` 在哪里

时间:2021-05-26 08:20:45

标签: android kotlin android-jetpack-compose

在 Jetpact Compose 1.0.0-alpha11 中,以下是有效代码

DropdownMenu(
    toggle = {
        Text(
            text = selected.value.toString(),
            modifier = Modifier.clickable(onClick = { showMenu = true }),
            fontSize = 16.sp
        )
    },
    expanded = showMenu,
    onDismissRequest = { showMenu = false },
    toggleModifier = Modifier.background(Color.LightGray)
) {
    items.forEachIndexed { index, s ->
        DropdownMenuItem(
            onClick = {
                selected.value = items[index]
                showMenu = false
            }
        ) {
            Text(text = s.toString())
        }
    }
}

但是,迁移到 Jetpact Compose 1.0.0-beta07 后,不再支持 toggletoggleModifier

使用 DropdownMenu 的新方法是什么?

1 个答案:

答案 0 :(得分:1)

来自 toggletoggleModifierDropdownMenu1.0.0-alpha12 参数 were removed。他们没有被替换。

你必须做一些不同的事情,比如:

var expanded by remember { mutableStateOf(false) }
val suggestions = listOf("Item1", "Item2", "Item3")
var selectedText by remember { mutableStateOf("Item1") }

Box(
    //toggleModifier
    Modifier.background(Color.LightGray)
){ 
    //toggle 
    Text(
        text = selectedText,
        modifier = Modifier.clickable(onClick = { expanded = true }),
        fontSize = 16.sp
    )

    DropdownMenu(
        expanded = expanded,
        onDismissRequest = { expanded = false },

    ) {
        suggestions.forEach { label ->
            DropdownMenuItem(onClick = {
                selectedText = label
            }) {
                Text(text = label)
            }
        }
    }
}