RenderWithConstraints
fun ResourcesScope.RenderWithConstraints(style: Style? = null, content: ComponentScope.(SizeConstraints) -> Component): Component
Utility for creating a layout tree that depends on its own and/or its children's size.
For example, Component A is returned if it fits within the specified width otherwise Component B is returned.
Example:
RenderWithConstraints { sizeConstraints ->
val textComponent = Text(textSize = 16.sp, text = "Some text to measure")
val textOutputSize = Size()
textComponent.measure(
context,
SizeSpec.makeSizeSpec(0, UNSPECIFIED),
SizeSpec.makeSizeSpec(0, UNSPECIFIED),
textOutputSize)
// Small component to use in case textComponent doesn't fit within
// the current layout.
val imageComponent = Image(drawable = drawableRes(R.drawable.ic_launcher))
// Assuming sizeConstraints.hasBoundedWidth == true
val doesTextFit = textOutputSize.width <= sizeConstraints.maxWidth
if (doesTextFit) textComponent else imageComponent
}
Content copied to clipboard