swipeable
Functions summary
Modifier |
@ExperimentalMaterialApiThis function is deprecated. Material's Swipeable has been replaced by Foundation's AnchoredDraggable APIs. |
Cmn
|
Functions
Modifier.swipeable
@ExperimentalMaterialApi
fun <T : Any?> Modifier.swipeable(
state: SwipeableState<T>,
anchors: Map<Float, T>,
orientation: Orientation,
enabled: Boolean = true,
reverseDirection: Boolean = false,
interactionSource: MutableInteractionSource? = null,
thresholds: (from, to) -> ThresholdConfig = { _, _ -> FixedThreshold(56.dp) },
resistance: ResistanceConfig? = resistanceConfig(anchors.keys),
velocityThreshold: Dp = VelocityThreshold
): Modifier
Enable swipe gestures between a set of predefined states.
To use this, you must provide a map of anchors (in pixels) to states (of type T). Note that this map cannot be empty and cannot have two anchors mapped to the same state.
When a swipe is detected, the offset of the SwipeableState will be updated with the swipe delta. You should use this offset to move your content accordingly (see Modifier.offsetPx). When the swipe ends, the offset will be animated to one of the anchors and when that anchor is reached, the value of the SwipeableState will also be updated to the state corresponding to the new anchor. The target anchor is calculated based on the provided positional thresholds.
Swiping is constrained between the minimum and maximum anchors. If the user attempts to swipe past these bounds, a resistance effect will be applied by default. The amount of resistance at each edge is specified by the resistance config. To disable all resistance, set it to null.
For an example of a swipeable with three states, see:
import androidx.compose.foundation.background import androidx.compose.foundation.gestures.Orientation import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.offset import androidx.compose.foundation.layout.size import androidx.compose.foundation.layout.width import androidx.compose.material.FractionalThreshold import androidx.compose.material.Text import androidx.compose.material.rememberSwipeableState import androidx.compose.material.swipeable import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.unit.IntOffset import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp // Draw a slider-like composable consisting of a red square moving along a // black background, with three states: "A" (min), "B" (middle), and "C" (max). val width = 350.dp val squareSize = 50.dp val swipeableState = rememberSwipeableState("A") val sizePx = with(LocalDensity.current) { (width - squareSize).toPx() } val anchors = mapOf(0f to "A", sizePx / 2 to "B", sizePx to "C") Box( modifier = Modifier.width(width) .swipeable( state = swipeableState, anchors = anchors, thresholds = { _, _ -> FractionalThreshold(0.5f) }, orientation = Orientation.Horizontal, ) .background(Color.Black) ) { Box( Modifier.offset { IntOffset(swipeableState.offset.value.roundToInt(), 0) } .size(squareSize) .background(Color.Red), contentAlignment = Alignment.Center, ) { Text(swipeableState.currentValue, color = Color.White, fontSize = 24.sp) } }
| Parameters | |
|---|---|
<T : Any?> |
The type of the state. |
state: SwipeableState<T> |
The state of the |
anchors: Map<Float, T> |
Pairs of anchors and states, used to map anchors to states and vice versa. |
orientation: Orientation |
The orientation in which the |
enabled: Boolean = true |
Whether this |
reverseDirection: Boolean = false |
Whether to reverse the direction of the swipe, so a top to bottom swipe will behave like bottom to top, and a left to right swipe will behave like right to left. |
interactionSource: MutableInteractionSource? = null |
Optional |
thresholds: (from, to) -> ThresholdConfig = { _, _ -> FixedThreshold(56.dp) } |
Specifies where the thresholds between the states are. The thresholds will be used to determine which state to animate to when swiping stops. This is represented as a lambda that takes two states and returns the threshold between them in the form of a |
resistance: ResistanceConfig? = resistanceConfig(anchors.keys) |
Controls how much resistance will be applied when swiping past the bounds. |
velocityThreshold: Dp = VelocityThreshold |
The threshold (in dp per second) that the end velocity has to exceed in order to animate to the next state, even if the positional |