I have such code from here: https://developer.android.com/jetpack/compose/gestures
Box(modifier = Modifier.fillMaxSize()) {
var offsetX by remember { mutableStateOf(0f) }
var offsetY by remember { mutableStateOf(0f) }
Box(
Modifier
.offset { IntOffset(offsetX.roundToInt(), offsetY.roundToInt()) }
.background(Color.Red)
.size(120.dp) // makes it rectangle. wrap_content without it
.align(Alignment.BottomEnd)
.pointerInput(Unit) {
detectDragGestures { change, dragAmount ->
change.consume()
offsetX += dragAmount.x
offsetY += dragAmount.y
}
}
) {
// todo
}
}
So for end
side of x
I create something like this:
val newOffsetX = if ((offsetX + dragAmount.x) < 0) { offsetX + dragAmount.x } else { 0 }
offsetX = newOffsetX
But how can I found start
of x
and prevent my draggable box go out of screen?
Is there a way to do it for both X
and Y
?