Jetpack Compose: share state between Delegating modifier.Node - Stack Overflow

admin2025-05-02  2

Let's say that I have the following custom modifier:

fun Modifier.moveable(): Modifier = composed {
    var initialDragOffset: Offset by remember { mutableStateOf(Offset.Zero) }
    var dragOffset: Offset? by remember { mutableStateOf(null) }

    this
        .pointerInput(Unit) {
            this.detectDragGestures(
                onDragStart = { initialDragOffset = it },
                onDragEnd = { dragOffset = null },
                onDragCancel = { dragOffset = null }
            ) { changes, _ -> dragOffset = changes.position }
        }
        .graphicsLayer {
            if (dragOffset != null) {
                translationX = dragOffset!!.x - initialDragOffset.x
                translationY = dragOffset!!.y - initialDragOffset.y
                alpha = 0.7f
            }
        }
}

(This is a simplified version of my modifier).

This is a simple case of a modifier (here pointerInput) that outputs data which is then consumed by another modifier (here graphicsLayer).

How do I port this from composed to Modifier.Node? How do I delegate to two other modifiers with a shared state using this new Node optimized API?

转载请注明原文地址:http://www.anycun.com/QandA/1746121041a91960.html