Gesture detector that waits for pointer down and touch slop in the horizontal direction and then calls [onHorizontalDrag] for each horizontal drag event.
DetectHorizontalDragGesturesSample
@Composable
fun DetectHorizontalDragGesturesSample() {
val offsetX = remember { mutableStateOf(0f) }
val offsetY = remember { mutableStateOf(0f) }
var width by remember { mutableStateOf(0f) }
Box(Modifier.fillMaxSize().onSizeChanged { width = it.width.toFloat() }) {
Box(
Modifier.offset { IntOffset(offsetX.value.roundToInt(), offsetY.value.roundToInt()) }
.fillMaxHeight()
.width(50.dp)
.background(Color.Blue)
.pointerInput(Unit) {
detectHorizontalDragGestures { _, dragAmount ->
val originalX = offsetX.value
val newValue = (originalX + dragAmount).coerceIn(0f, width - 50.dp.toPx())
offsetX.value = newValue
}
}
)
}
}