https://kotlinlang.org logo
#compose
Title
# compose
m

mertceyhan

05/11/2022, 3:25 PM
Hi all! I need to scroll on a component even screen has enough space, is that possible? I’ve tried the below code, it works well if the screen is small but it doesn’t work on if there is enough space to show all content. Please see the thread
Copy code
Surface(
    modifier = Modifier
        .fillMaxSize()
        .padding(horizontal = 16.dp),
    color = MaterialTheme.colors.background
) {
    val scrollState = rememberScrollState()
    val coroutineScope = rememberCoroutineScope()
    var secondTextY = remember { 0f }

    Column(
        modifier = Modifier
            .fillMaxSize()
            .verticalScroll(scrollState)
    ) {
        Spacer(modifier = Modifier.height(32.dp))
        Text(text = "TEXT 1")
        Spacer(modifier = Modifier.height(32.dp))
        Text(
            modifier = Modifier.onGloballyPositioned { layoutCoordinates ->
                secondTextY =
                    scrollState.value + layoutCoordinates.positionInRoot().y
            },
            text = "TEXT 2 "
        )
        Spacer(modifier = Modifier.height(32.dp))
        Button(onClick = {
            coroutineScope.launch {
                scrollState.animateScrollTo(secondTextY.toInt())
            }
        }) {
            Text(text = "Scroll to the second text")
        }
    }
}
3 Views