compose-markdown icon indicating copy to clipboard operation
compose-markdown copied to clipboard

Cannot scroll MarkdownText inside a LazyColumn

Open phucynwa opened this issue 2 years ago • 3 comments

I tried to use MarkdownText inside a LazyColumn but it isn't able to be scrolled.

                Box(modifier = Modifier.fillMaxSize()) {
                    LazyColumn(
                        state = rememberLazyListState(),
                        modifier = Modifier.fillMaxSize()
                    ) {
                        item {
                            MarkdownText(
                                markdown = demo,
                                viewId = R.id.markdownTextId,
                                modifier = Modifier
                                    .fillMaxWidth()
                                    .wrapContentHeight()
                            )
                        }
                    }
                }

I also tried using Column and verticalScroll modifier but it doesn't work.

                    Column(
                        modifier = Modifier
                            .fillMaxSize()
                            .verticalScroll(rememberScrollState())
                    ) {
                        MarkdownText(
                            markdown = demo,
                            viewId = R.id.markdownTextId,
                            modifier = Modifier
                                .fillMaxWidth()
                                .wrapContentHeight()
                        )
                    }

Do you have any solution for this problem. Thank you.

Note: If I can find a good solution, I'll push my PR to this repository. 😄

phucynwa avatar Dec 07 '21 04:12 phucynwa

@phucynwa Have you found a solution? We are also struggling with this problem.

mamilov avatar Jan 12 '22 09:01 mamilov

I was able to fix it by wrapping the Component in a Box and adding a transparent Surface on top. It will disable Links, but we don't use them in our use case, so it might help you as well.

val density = LocalDensity.current
var height by remember { mutableStateOf(0) }
val heightDp = remember(height) { with(density) { height.toDp() } }

BoxWithConstraints(modifier = Modifier.onSizeChanged {
    height = it.height
}) {
    MarkdownText(
        markdown = markdown,
        style = style,
        fontResource = fontResource,
        color = color,
        textAlign = textAlign,
        modifier = modifier
    )
    Surface(
        color = Color.Transparent,
        modifier = Modifier
            .fillMaxSize()
            .height(heightDp)
    ) {}
}

mamilov avatar Jan 12 '22 12:01 mamilov

Hello @phucynwa, Running the sample project, your code works fine 🤔. Can you share more details about the problem? Compose version, Android version...

I tried to use MarkdownText inside a LazyColumn but it isn't able to be scrolled.

                Box(modifier = Modifier.fillMaxSize()) {
                    LazyColumn(
                        state = rememberLazyListState(),
                        modifier = Modifier.fillMaxSize()
                    ) {
                        item {
                            MarkdownText(
                                markdown = demo,
                                viewId = R.id.markdownTextId,
                                modifier = Modifier
                                    .fillMaxWidth()
                                    .wrapContentHeight()
                            )
                        }
                    }
                }

I also tried using Column and verticalScroll modifier but it doesn't work.

                    Column(
                        modifier = Modifier
                            .fillMaxSize()
                            .verticalScroll(rememberScrollState())
                    ) {
                        MarkdownText(
                            markdown = demo,
                            viewId = R.id.markdownTextId,
                            modifier = Modifier
                                .fillMaxWidth()
                                .wrapContentHeight()
                        )
                    }

Do you have any solution for this problem. Thank you.

Note: If I can find a good solution, I'll push my PR to this repository. 😄

jeziellago avatar Jan 12 '22 21:01 jeziellago