DEV Community

Cover image for Jetpack compose onDragStopped animation
Tristan Elliott
Tristan Elliott

Posted on • Edited on

Jetpack compose onDragStopped animation

Resources

Just show me the code

    var offsetX by remember { mutableStateOf(0f) }
    val draggableState = DraggableState { delta ->
            offsetX += delta
    }
   Box(
        modifier = Modifier.draggable(
            orientation = Orientation.Horizontal,
            state = draggableState,
            onDragStopped = {

                draggableState.drag(MutatePriority.PreventUserInput) {
                    Animatable(offsetX).animateTo(
                        targetValue = 0f,
                        tween(durationMillis = 300)
                    ) {
                        dragBy(value - offsetX)
                    }
                }
            }
    )
    ){
       //The Column is the item that will do the moving
        Column(
            modifier = Modifier
                .offset { IntOffset(offsetX.roundToInt(), 0) }
                ) {
      }
}

Enter fullscreen mode Exit fullscreen mode

Conclusion

  • Thank you for taking the time out of your day to read this blog post of mine. If you have any questions or concerns please comment below or reach out to me on Twitter.

Top comments (0)