I'm taking a DropdownMenu from Kotlin Compose for Desktop and I want to include a vertical scroll bar. The source code for the DropdownMenu is here. They have a sample which works fine but I can't get it to display the vertical scroll bar. It doesn't display by default.
There is a VerticalScrollbar example also that I have attempted but I haven't gotten it to work with the DropdownMenu
.
Putting a verticalScroll()
within the DropdownMenu(Modifier)
results in an error Vertically scrolled component was measured with an infinity maximum height constraints, which is disallowed...
.
Adding a VerticalScrollbar()
beneath the DropdownMenu
results in an error Can't represent a size of 1073741824 in Constraints
.
So as far as I can tell there is something in the DropdownMenu
's Popup
or something like that which is making this difficult for me.
Is there a way I can implement the visible scroll bar? My layout is like this so far. (you can scroll down to the Dropdown menu below...)
data class Lookup(val id: String, val name: String)
fun main() = application {
Window(
onCloseRequest = ::exitApplication,
state = rememberWindowState(width = 1280.dp, height = 800.dp)
) {
MaterialTheme {
Scaffold {
Column(
modifier = Modifier
.fillMaxSize()
.verticalScroll(rememberScrollState()),
horizontalAlignment = Alignment.CenterHorizontally
) {
val lookups = LookupService.getLookups() // about 75 items
val (expanded, setExpanded) = remember { mutableStateOf(false) }
val (selected, setSelected) = remember { mutableStateOf<Lookup?>(null) }
Spacer(Modifier.height(20.dp))
Box(Modifier.wrapContentSize(Alignment.TopStart)) {
val icon = if (expanded) {
Icons.Filled.KeyboardArrowUp
} else {
Icons.Filled.KeyboardArrowDown
}
OutlinedTextField(
value = selected?.name ?: "",
onValueChange = { },
modifier = Modifier
.width(360.dp)
.onKeyEvent {
if (it.key == Key.DirectionDown && !expanded) {
setExpanded(true)
return@onKeyEvent true
}
return false
}
.clickable { setExpanded(true) },
singleLine = true,
label = { Text("Select an item") },
trailingIcon = {
Icon(icon, "Select an item", Modifier.clickable {
setExpanded(!expanded)
})
},
enabled = expanded,
colors = TextFieldDefaults.textFieldColors(
disabledTextColor = LocalContentColor.current.copy(LocalContentAlpha.current),
backgroundColor = Color.Transparent
)
)
DropdownMenu( // Desktop version, so it creates a "Popup" per the source code
expanded = expanded,
onDismissRequest = {
runBlocking { // to handle a glitch where the dropdown may "unexpand & expand" again on clicking
delay(200)
setExpanded(false)
}
},
modifier = Modifier
.width(360.dp)
.background(Color.White)
.clip(RoundedCornerShape(5.dp))
// SHOULD HAVE VERTICAL SCROLLBAR SHOW UP AS PART OF THIS DROPDOWNMENU COLUMN
) {
lookups.forEach { lookup ->
DropdownMenuItem(
onClick = {
setExpanded(false)
setSelected(lookup)
}
) {
Text(lookup.name)
}
}
}
}
}
}
}
}
}