{% setvar book_path %}/reference/androidx/_book.yaml{% endsetvar %} {% include "_shared/_reference-head-tags.html" %}

SwipeToDismissBoxState

@ExperimentalMaterial3Api
public final class SwipeToDismissBoxState


State of the SwipeToDismissBox composable.

Summary

Nested types

Public constructors

SwipeToDismissBoxState(
    @NonNull SwipeToDismissBoxValue initialValue,
    @NonNull Density density,
    @NonNull Function1<@NonNull SwipeToDismissBoxValue, @NonNull Boolean> confirmValueChange,
    @NonNull Function1<@NonNull Float, @NonNull Float> positionalThreshold
)

Public methods

final void

Dismiss the component in the given direction, with an animation and suspend.

final @NonNull SwipeToDismissBoxValue

The current state value of the SwipeToDismissBoxState.

final @NonNull SwipeToDismissBoxValue

The direction (if any) in which the composable has been or is being dismissed.

final float

The fraction of the progress going from currentValue to targetValue, within 0f..1f bounds.

final @NonNull SwipeToDismissBoxValue

The target state.

final float

Require the current offset.

final void

Reset the component to the default position with animation and suspend until it if fully reset or animation has been cancelled.

final void

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

Public constructors

SwipeToDismissBoxState

public SwipeToDismissBoxState(
    @NonNull SwipeToDismissBoxValue initialValue,
    @NonNull Density density,
    @NonNull Function1<@NonNull SwipeToDismissBoxValue, @NonNull Boolean> confirmValueChange,
    @NonNull Function1<@NonNull Float, @NonNull Float> positionalThreshold
)
Parameters
@NonNull SwipeToDismissBoxValue initialValue

The initial value of the state.

@NonNull Density density

The density that this state can use to convert values to and from dp.

@NonNull Function1<@NonNull SwipeToDismissBoxValue, @NonNull Boolean> confirmValueChange

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

@NonNull Function1<@NonNull Float, @NonNull Float> positionalThreshold

The positional threshold to be used when calculating the target state while a swipe is in progress and when settling after the swipe ends. This is the distance from the start of a transition. It will be, depending on the direction of the interaction, added or subtracted from/to the origin offset. It should always be a positive value.

Public methods

dismiss

public final void dismiss(@NonNull SwipeToDismissBoxValue direction)

Dismiss the component in the given direction, with an animation and suspend. This method will throw CancellationException if the animation is interrupted

Parameters
@NonNull SwipeToDismissBoxValue direction

The dismiss direction.

getCurrentValue

public final @NonNull SwipeToDismissBoxValue getCurrentValue()

The current state value of the SwipeToDismissBoxState.

getDismissDirection

public final @NonNull SwipeToDismissBoxValue getDismissDirection()

The direction (if any) in which the composable has been or is being dismissed.

Use this to change the background of the SwipeToDismissBox if you want different actions on each side.

getProgress

@<Error class: unknown class>
public final float getProgress()

The fraction of the progress going from currentValue to targetValue, within 0f..1f bounds.

getTargetValue

public final @NonNull SwipeToDismissBoxValue getTargetValue()

The target state. This is the closest state to the current offset (taking into account positional thresholds). If no interactions like animations or drags are in progress, this will be the current state.

requireOffset

public final float requireOffset()

Require the current offset.

Throws
kotlin.IllegalStateException

If the offset has not been initialized yet

reset

public final void reset()

Reset the component to the default position with animation and suspend until it if fully reset or animation has been cancelled. This method will throw CancellationException if the animation is interrupted

Returns
void

the reason the reset animation ended

snapTo

public final void snapTo(@NonNull SwipeToDismissBoxValue targetValue)

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

Parameters
@NonNull SwipeToDismissBoxValue targetValue

The new target value