Skip to content

Bug with animated CircleNode modifier #533

Open
@IanGClifton

Description

@IanGClifton

There's a subtle bug with this code:

class CircleNode(var color: Color) : Modifier.Node(), DrawModifierNode {
    private val alpha = Animatable(1f)

    override fun ContentDrawScope.draw() {
        drawCircle(color = color, alpha = alpha.value)
        drawContent()
    }

    override fun onAttach() {
        coroutineScope.launch {
            alpha.animateTo(
                0f,
                infiniteRepeatable(tween(1000), RepeatMode.Reverse)
            ) {
            }
        }
    }
}

Alpha is initially set to 1f and animated toward 0f. This works as expected until the Node is reused. If you scroll inside a lazy list, the Node will be reused and its value will be somewhere between 0f and 1f, so let's say 0.5f. When it is attached, alpha.animateTo will now animated from 0f to 0.5f. If you continue to scroll, this node will keep getting reattached with a smaller and smaller animation alpha until the animation is effectively not noticeable (though still running).

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions