Saif Alhaider
06/07/2024, 2:52 PMmin-width
and `max-width`properties, where the width stays between 400 and 450 dp as the window resizes. How can I achieve this with the minimum amount of recomposition?Saif Alhaider
06/07/2024, 2:55 PMwidthIn
modifier but didn't workShane Schaefer
06/07/2024, 3:23 PMSaif Alhaider
06/07/2024, 3:27 PMModifier.weight(..)
to assign a percentage of the width might not guarantee the component stays within the specified range.Shane Schaefer
06/07/2024, 3:34 PMSaif Alhaider
06/07/2024, 6:50 PM@Composable
fun Modifier.dynamicWidth(
min: Dp,
max: Dp,
density: Density,
onWidthChange: (Dp) -> Unit,
) = this.then(
Modifier.onGloballyPositioned { layoutCoordinates ->
val parentWidth = with(density) { layoutCoordinates.parentLayoutCoordinates?.size?.width?.toDp() }
val childWidth = with(density) { layoutCoordinates.size.width.toDp() }
val result = (parentWidth?.minus(childWidth) ?: 0.dp)
val newWidth = when {
result < max && result > min -> result
result < max && result < min -> min
else -> max
}
onWidthChange(newWidth)
}
)
After that, I attempted to update an animated state value and passed it as a width. I must admit, though, that this solution isn't ideal because it doesn't rely on the available space within the parent container, but rather on the parent width as a whole.Saif Alhaider
06/07/2024, 6:54 PM