Google is committed to advancing racial equity for Black communities. See how.

SwipeableState

@Stable open class SwipeableState<T>
kotlin.Any
   ↳ androidx.compose.material.SwipeableState

State of the swipeable modifier.

This contains necessary information about any ongoing swipe or animation and provides methods to change the state either immediately or by starting an animation. To create and remember a SwipeableState with the default animation clock, use rememberSwipeableState.

Summary

Public constructors
<init>(initialValue: T, clock: AnimationClockObservable, animationSpec: AnimationSpec<Float> = AnimationSpec, confirmStateChange: (newValue: T) -> Boolean = { true })

State of the swipeable modifier.

Public methods
Unit
animateTo(targetValue: T, anim: AnimationSpec<Float> = animationSpec, onEnd: (AnimationEndReason, T) -> Unit = null)

Set the state to the target value by starting an animation.

Float

Force swipeable to consume drag delta provided from outside of the regular swipeable gesture flow.

Unit
performFling(velocity: Float, onEnd: () -> Unit)

Perform fling with settling to one of the anchors which is determined by the given velocity.

Unit
snapTo(targetValue: T)

Set the state to the target value immediately, without any animation.

Companion functions
Saver<SwipeableState<T>, T>
Saver(clock: AnimationClockObservable, animationSpec: AnimationSpec<Float>, confirmStateChange: (T) -> Boolean)

The default Saver implementation for SwipeableState.

Properties
Float

The direction in which the swipeable is moving, relative to the current value.

Boolean

Whether the state is currently animating.

State<Float>

The current position (in pixels) of the swipeable.

State<Float>

The amount by which the swipeable has been swiped past its bounds.

SwipeProgress<T>

Information about the ongoing swipe or animation, if any.

T

The target value of the state.

T

The current value of the state.

Public constructors

<init>

SwipeableState(
    initialValue: T,
    clock: AnimationClockObservable,
    animationSpec: AnimationSpec<Float> = AnimationSpec,
    confirmStateChange: (newValue: T) -> Boolean = { true })

State of the swipeable modifier.

This contains necessary information about any ongoing swipe or animation and provides methods to change the state either immediately or by starting an animation. To create and remember a SwipeableState with the default animation clock, use rememberSwipeableState.

Parameters
initialValue: T The initial value of the state.
clock: AnimationClockObservable The animation clock that will be used to drive the animations.
animationSpec: AnimationSpec<Float> = AnimationSpec The default animation that will be used to animate to a new state.
confirmStateChange: (newValue: T) -> Boolean = { true } Optional callback invoked to confirm or veto a pending state change.

Public methods

animateTo

fun animateTo(
    targetValue: T,
    anim: AnimationSpec<Float> = animationSpec,
    onEnd: (AnimationEndReason, T) -> Unit = null
): Unit

Set the state to the target value by starting an animation.

Parameters
targetValue: T The new value to animate to.
anim: AnimationSpec<Float> = animationSpec The animation that will be used to animate to the new value.
onEnd: (AnimationEndReason, T) -> Unit = null Optional callback that will be invoked when the animation ended for any reason.

performDrag

fun performDrag(delta: Float): Float

Force swipeable to consume drag delta provided from outside of the regular swipeable gesture flow.

Note: This method performs generic drag and it won't settle to any particular anchor, * leaving swipeable in between anchors. When done dragging, performFling must be called as well to ensure swipeable will settle at the anchor.

In general cases, swipeable drags by itself when being swiped. This method is to be used for nested scroll logic that wraps the swipeable. In nested scroll developer may want to force drag when the child scroll container reaches the bound.

Parameters
delta: Float delta in pixels to drag by
Return
the amount of delta consumed

performFling

fun performFling(
    velocity: Float,
    onEnd:&nbs