I'm using `Modifier.pointerInput` to track a point...
# compose
e
I'm using
Modifier.pointerInput
to track a pointer over a bitmap and pull out the color of the underlying pixel. I also use an
AnimatedVisibility
to show a
Text
label under the position where the pointer is that shows the hex of the color. Is it expected that using
val foo by transition.animate*
in an
AnimatedVisibilityScope
will cause the function to recompose even after the transition reaches the target state? If I remove the
transition.animate*
calls the function only composes once, and when I put them in it recomposes on every pointer move. Code in 🧵
This is a simplified version of what is happening.
Copy code
var selectedPosition by remember { mutableStateOf(Offset.Zero) }
var selectedColor by remember { mutableStateOf(Color.Unspecified) }

Box(modifier = Modifier.pointerInput {
  selectedPosition = ...
  selectedColor = ...
}) {
  val isLabelVisible = selectedColor.isSpecified && selectedPosition != Offset.Zero
  AnimatedVisibility(
    visible = isLabelVisible,
    enter = EnterTransition.None,
    exit = ExitTransition.None
  ) {
    ColorLabel(
      position = { selectedPosition },
      color = { selectedColor }
    )
  }
}

@Composable
fun AnimatedVisibilityScope.ColorLabel(
  position: () -> Offset,
  color: () -> Color
) {
  val labelWidth by transition.animateDp {
    if(it == EnterExitState.Visible) 100.dp else 0.dp
  }

  println("Recomposes on every pointer move if labelWidth is present")

  ColorLabel(color, labelWidth)
}
t
So, you're reading
labelWidth
in the call to
ColorLabel
, and that will require
transition.animateDp
to be called to provide that value. If you replace
by transition.animateDp
with
= transition.animateDp
, then the
State<Dp>
won't be read immediately, and depending on where you read it in
ColorLabel
, the call to
ColorLabel
could be skipped.
But it looks like you could reuse the same transition by hoisting: https://developer.android.com/jetpack/compose/animation#encapsulate-transition
e
Hoisting did the trick, thank you!