How to know if Text is visible on Jetpack Compose?

Viewed 2152

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

2 Answers

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))
}
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.

Related