I was wondering if there is a solution for Exposed drop-down menu for jetpack compose? I couldn't find a proper solution for this component inside jetpack compose. Any help?

Drop-down

推荐答案

The version 1.1.0-alpha06 introduced the implementation of ExposedDropdownMenu based on ExposedDropdownMenuBox with TextField and DropdownMenu inside.

比如:

    val options = listOf("Option 1", "Option 2", "Option 3", "Option 4", "Option 5")
    var expanded by remember { mutableStateOf(false) }
    var selectedOptionText by remember { mutableStateOf(options[0]) }
    
    ExposedDropdownMenuBox(
        expanded = expanded,
        onExpandedChange = {
            expanded = !expanded
        }
    ) {
        TextField(
            readOnly = true,
            value = selectedOptionText,
            onValueChange = { },
            label = { Text("Label") },
            trailingIcon = {
                ExposedDropdownMenuDefaults.TrailingIcon(
                    expanded = expanded
                )
            },
            colors = ExposedDropdownMenuDefaults.textFieldColors()
        )
        ExposedDropdownMenu(
            expanded = expanded,
            onDismissRequest = {
                expanded = false
            }
        ) {
            options.forEach { selectionOption ->
                DropdownMenuItem(
                    onClick = {
                        selectedOptionText = selectionOption
                        expanded = false
                    }
                ) {
                    Text(text = selectionOption)
                }
            }
        }
    }

enter image description here

With the version 1.0.x there isn't a built-in component.
You can use a OutlinedTextField + DropdownMenu.

这只是一个基本(非常基本)的实现:

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

var textfieldSize by remember { mutableStateOf(Size.Zero)}

val icon = if (expanded)
    Icons.Filled.ArrowDropUp //it requires androidx.compose.material:material-icons-extended
else
    Icons.Filled.ArrowDropDown


Column() {
    OutlinedTextField(
        value = selectedText,
        onValueChange = { selectedText = it },
        modifier = Modifier
            .fillMaxWidth()
            .onGloballyPositioned { coordinates ->
                //This value is used to assign to the DropDown the same width
                textfieldSize = coordinates.size.toSize()
            },
        label = {Text("Label")},
        trailingIcon = {
            Icon(icon,"contentDescription",
                 Modifier.clickable { expanded = !expanded })
        }
    )
    DropdownMenu(
        expanded = expanded,
        onDismissRequest = { expanded = false },
        modifier = Modifier
            .width(with(LocalDensity.current){textfieldSize.width.toDp()})
    ) {
        suggestions.forEach { label ->
            DropdownMenuItem(onClick = {
                selectedText = label
            }) {
                Text(text = label)
            }
        }
    }
}

enter image description here enter image description here

Kotlin相关问答推荐

查看流数据和改进的HTTP请求的模型

在Kotlin中将String转换为T

KMP:未能添加cafe.adriel.voyager依赖项

两个LocalDateTime之间的Kotlin差异

找不到有效的 Docker 环境

有什么方法可以要求在 kotlin 中的类型参数上进行注释?

通用接口继承

通过顺序多米诺骨牌操作列表管理对象的最佳方法是什么?

使用空键映射获取

Kotlin JS JSON 反序列化

如何用 kotlin 打包 List

如何在 kotlin 中生成 json 对象?

变量后的Android问号

Kotlin DataBinding 将静态函数传递到布局 xml

Kotlin通过映射委托属性,如果映射中不存在,则抛出NoTouchElementException

Spring Boot:更改属性占位符符号

Kotlin中具有多个参数的绑定适配器

Kotlin:如何修改成对的值?

如何在使用Koin DI的活动之间共享同一个ViewModel实例?

Kotlin中对象和数据类的区别是什么?