如何获取swipeable jetpack compose的阈值?

How to get the threshold value of swipeable jetpack compose?

我想要 swipeable 项的 threshold 值,这样当达到某个阈值时我可以做一些事情。比如改变某项滑动空白处的颜色。

可以像这样添加滑动:

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

您可以使用swipeableState查看当前刷卡进度并与阈值进行比较。我正在使用 derivedStateOf 来防止冗余重组。

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) },