Home > Net >  How to get the threshold value of swipeable jetpack compose?
How to get the threshold value of swipeable jetpack compose?

Time:04-02

I want threshold value of swipeable item so I can do something when a certain threshold is reached. For example, changing the color of an item swiped empty place.

The swipeable can be added like :

Box(
    modifier = Modifier
        .fillMaxWidth()
        .height(IntrinsicSize.Min)
        .swipeable(
            state = swipeAbleState,
            anchors = anchors,
            thresholds = { _, _ ->
                FractionalThreshold(.5f)
            },
            orientation = Orientation.Horizontal
        )
)

CodePudding user response:

You can use swipeableState to check current swipe progress and compare it with the threshold. I'm using derivedStateOf to prevent redundant recompositions.

val threshold = 0.3f
val thresholdReached by remember {
    derivedStateOf {
        swipeableState.progress.from != swipeableState.progress.to // check that we are not in the initial state
                && swipeableState.progress.fraction > threshold
    }
}
// ...
thresholds = { _, _ -> FractionalThreshold(threshold) },
  • Related