SwipeableState
Deprecated SwipeableDeprecation
@ExperimentalMaterialApi
open class SwipeableState<T>(
initialValue: T,
internal val animationSpec: AnimationSpec<Float> = AnimationSpec,
internal val 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 | The initial value of the state. |
animationSpec | The default animation that will be used to animate to a new state. |
confirmStateChange | Optional callback invoked to confirm or veto a pending state change. |
Properties
var 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.
var isAnimationRunning: Boolean
Whether the state is currently animating.
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.
val overflow: State<Float>
The amount by which the swipeable
has been swiped past its bounds.
@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
.
@ExperimentalMaterialApi
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)
.
@ExperimentalMaterialApi
val direction: Float
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.
Functions
@ExperimentalMaterialApi
suspend fun snapTo(targetValue: T)
Set the state without any animation and suspend until it's set
Parameters
targetValue | The new target value to set currentValue to. |
@ExperimentalMaterialApi
suspend fun animateTo(targetValue: T, anim: AnimationSpec<Float> = animationSpec)
Set the state to the target value by starting an animation.
Parameters
targetValue | The new value to animate to. |
anim | The animation that will be used to animate to the new value. |
suspend fun performFling(velocity: Float)
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 | velocity to fling and settle with |
Returns
the reason fling ended |
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 | delta in pixels to drag by |
Returns
the amount of delta consumed |
Companion Object
Methods
fun <T : Any> Saver(
animationSpec: AnimationSpec<Float>,
confirmStateChange: (T) -> Boolean,
) =
Saver<SwipeableState<T>, T>(
save = { it.currentValue },
restore = { SwipeableState(it, animationSpec, confirmStateChange) },
)
The default Saver
implementation for SwipeableState
.