Apologies if this question is obvious, I've spent ...
# compose-desktop
m
Apologies if this question is obvious, I've spent a while searching (even asking chatgpt) but how do I get a working vertical scrollbar on a multi-line text field. For some reason one doesn't appear by default. I tried using
VerticalScrollbar
and syncing them via a
scrollState
but it doesn't seem to work properly.
i
Desktop has an experimental API for BasicTextField + Scrollbar connection:
Copy code
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.VerticalScrollbar
import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.rememberScrollbarAdapter
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.TextFieldScrollState
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.window.singleWindowApplication

@OptIn(ExperimentalFoundationApi::class)
fun main() = singleWindowApplication {
    val scrollState = remember { TextFieldScrollState(Orientation.Vertical) }

    Box(Modifier.fillMaxSize()) {
        BasicTextField(
            value = (1..10000).joinToString { "a " },
            {},
            modifier = Modifier.fillMaxSize(),
            scrollState = scrollState
        )

        VerticalScrollbar(
            rememberScrollbarAdapter(scrollState),
            modifier = Modifier.fillMaxHeight().align(Alignment.CenterEnd)
        )
    }
}
(it doesn't exist for TextField)
m
thank you both