SwipeableState

Added in 1.0.0
Deprecated in 1.6.0
Known direct subclasses
DismissState

State of the SwipeToDismiss composable.


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 companion functions

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

The default Saver implementation for SwipeableState.

Cmn

Public constructors

<T : Any?> SwipeableState(
    initialValue: T,
    animationSpec: AnimationSpec<Float>,
    confirmStateChange: (newValue) -> Boolean
)
Cmn

Public functions

suspend Unit

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

Cmn
Float

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

Cmn
suspend Unit
performFling(velocity: Float)

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

Cmn
suspend Unit

Set the state without any animation and suspend until it's set

Cmn

Public properties

T

The current value of the state.

Cmn
Float

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

Cmn
Boolean

Whether the state is currently animating.

Cmn
State<Float>

The current position (in pixels) of the swipeable.

Cmn
State<Float>

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

Cmn
SwipeProgress<T>

Information about the ongoing swipe or animation, if any.

Cmn
T

The target value of the state.

Cmn

Public companion functions

Saver

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

The default Saver implementation for SwipeableState.

Public constructors

SwipeableState

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

The initial value of the state.

animationSpec: AnimationSpec<Float> = AnimationSpec

The default animation that will be used to animate to a new state.

confirmStateChange: (newValue) -> Boolean = { true }

Optional callback invoked to confirm or veto a pending state change.

Public functions

animateTo

@ExperimentalMaterialApi
suspend fun animateTo(targetValue: T, anim: AnimationSpec<Float> = animationSpec): 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.

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

Returns
Float

the amount of delta consumed

performFling

suspend fun performFling(velocity: Float): Unit

Perform fling with settling to one of the anchors which is determined by the given velocity. Fling with settling swipeable will always consume all the velocity provided since it will settle at the anchor.

In general cases, swipeable flings 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 trigger settling fling when the child scroll container reaches the bound.

Parameters
velocity: Float

velocity to fling and settle with

Returns
Unit

the reason fling ended

snapTo

@ExperimentalMaterialApi
suspend fun snapTo(targetValue: T): Unit

Set the state without any animation and suspend until it's set

Parameters
targetValue: T

The new target value to set currentValue to.

Public properties

currentValue

val currentValue: 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.

direction

@ExperimentalMaterialApi
val directionFloat

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

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

val isAnimationRunningBoolean

Whether the state is currently animating.

offset

val offsetState<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 overflowState<Float>

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

progress

@ExperimentalMaterialApi
val progressSwipeProgress<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

@ExperimentalMaterialApi
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 currentValue.