, but the handler does not have access to the Canvas's width, to constrain the drag. How can I pass the Canvas's width to the drag handler? Example in thread...
robnik
02/28/2021, 3:14 PM
Example:
Copy code
val offset = remember { mutableStateOf(0f) }
val dragState = rememberDraggableState { dx ->
offset.value = (offset.value + dx).coerceIn(0f, ???) // canvas width?
}
Canvas(Modifier.fillMaxWidth().height(40.dp)
.draggable(orientation = Orientation.Horizontal,
state = dragState)) {
// this.size.width is available here. How to pass to the drag handler?
drawThing(offset)
}
s
Se7eN
02/28/2021, 4:09 PM
You can use
Modifier.onSizeChanged
on the
Canvas
to get it's size:
Copy code
val size by remember { mutableStateOf(0.dp) }
val dragState = rememberDraggableState { dx ->
offset.value = (offset.value + dx).coerceIn(0f, size.width)
}
Canvas(modifier = Modifier
...
.onSizeChanged { size = it }
...
)