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> = DefaultAnimationSpec, 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.

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> = DefaultAnimationSpec,
    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> = DefaultAnimationSpec 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.

snapTo

fun snapTo(targetValue: T): Unit

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

Parameters
targetValue: T The new target value to set value to.

Properties

direction

val direction: Float

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

This will be either 1f if it is is moving from left to right or top to bottom, -1f if it is moving from right to left or bottom to top, or 0f if no swipe or animation is in progress.

isAnimationRunning

var isAnimationRunning: Boolean

Whether the state is currently animating.

offset

val offset: State<Float>

The current position (in pixels) of the swipeable.

You should use this state to offset your content accordingly. The recommended way is to use Modifier.offsetPx. This includes the resistance by default, if resistance is enabled.

overflow

val overflow: State<Float>

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

progress

val progress: SwipeProgress<T>

Information about the ongoing swipe or animation, if any. See SwipeProgress for details.

If no swipe or animation is in progress, this returns SwipeProgress(value, value, 1f).

targetValue

val targetValue: T

The target value of the state.

If a swipe is in progress, this is the value that the swipeable would animate to if the swipe finished. If an animation is running, this is the target value of that animation. Finally, if no swipe or animation is in progress, this is the same as the value.

value

var value: T

The current value of the state.

If no swipe or animation is in progress, this corresponds to the anchor at which the swipeable is currently settled. If a swipe or animation is in progress, this corresponds the last anchor at which the swipeable was settled before the swipe or animation started.

Companion functions

Saver

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

The default Saver implementation for SwipeableState.