0

I want to use the fillMaxSize modifier on a composable, but I want to know the size of the composable before fillMaxSize is applied and save the size in a mutable map.

I tried

.fillMaxSize()
.onSizeChanged {
    sizes[page] = it
}

and also tried

.fillMaxSize()
// according to documentation, the position of onGloballyPositioned modifier in the modifier chain doesn't change anything
.onGloballyPositioned {
    sizes[page] = it.size
}                     

but they didn't give me the correct size (the size was still influenced by fillMaxSize). So then I decided to use the layout modifier:

.fillMaxSize()
.layout { measurable, constraints ->
    val placeable = measurable.measure(
// I need to build my own constraints so the ones passed down from fillMaxSize are not used
        Constraints(
            minWidth = 0,
            maxWidth = constraints.maxWidth,
            minHeight = 0,
            maxHeight = constraints.maxHeight
        )
    )

    sizes[page] = IntSize(placeable.width, placeable.height)
    layout(placeable.width, placeable.height) {
        placeable.placeRelative(0,0)
    }
}

This seems to work, and I guess I could make a custom modifier out of it, but I am wondering if this is the way to go or is there an easier way?

  • Not sure I understand exactly your use case, but there is a composable `BoxWithConstraints` where you can access size constraints – Courtesy Jan 24 '23 at 11:37

0 Answers0