'How to know if Text is visible on Jetpack Compose?

I have a scrollable screen and I would like to do action when a specific text appears/disappears in that screen. Is there any way to do that? Thanks



Solution 1:[1]

val isVisible = remember {
  mutableStateOf(false)
}

In compose as of 2022, you should do something like this for state of views.

remember() can hold state with any value type that you want.

Solution 2:[2]

Use a MutableState to hold the visibility.

val text1Visibility = mutableStateOf(true)

@Composable
fun Text(){
  if(text1Visibility.value)
    Text(text = "hello world")
}

the above do the trick for visible and gone, for invisible, useModifier.drawOpacity(0f) for now.

@Composable
fun Text(){
    Text(text = "hello world", modifier = Modifier.drawOpacity(0f))
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 JuJu Juned
Solution 2 ming chen