I am making my own custom ResizableBox
composable. It just shows a red box with a handle at the bottom end, and when I drag the handle, it resizes the box.
This kind of works:
@Composable
fun ResizableBox(
modifier: Modifier = Modifier,
) {
var size by remember { mutableStateOf(DpSize(200.dp, 200.dp)) }
Box(modifier = Modifier
.background(Color.Red)
.then(modifier)
.size(size)
) {
Handle(modifier = Modifier.align(Alignment.BottomEnd)) {
size = DpSize(size.width + it.x.dp, size.height + it.y.dp)
}
}
}
The thing is that I am trying to use .then(modifier)
to allow the caller to set a default value for size
(instead of my hardcoded default 200x200). But when I set .size()
from the caller, then the size never gets updated (presumably because I change it in the inner modifier, but the first value being set is in the outer modifier).
In other words:
// This works with a box starting with size 200x200
ResizableBox()
// This creates a box with size 100x150, but the handle fails to resize the box
ResizableBox(modifier = Modifier.size(width = 100.dp, height = 150.dp))
I think I am missing something regarding how I should compose the two modifiers together. How can I make it work?