KAspectRatio

fun KAspectRatio(modifier: Modifier = Modifier, ratio: Float = 1.0f, content: @Composable () -> Unit)(source)

Constrains its content to a given aspect ratio — mirrors shadcn/ui AspectRatio.

This component is useful for maintaining consistent proportions for images, videos, or other media content regardless of their actual dimensions or the parent container's width.

Example usage:

KAspectRatio(ratio = 16f / 9f) {
Image(
painter = painterResource(Res.drawable.landscape),
contentDescription = "Landscape image",
modifier = Modifier.fillMaxSize(),
contentScale = ContentScale.Crop
)
}

Parameters

modifier

The modifier to be applied to the layout.

ratio

The width-to-height ratio (e.g., 1.0f for a square, 1.77f for 16:9).

content

The composable content to be constrained by the aspect ratio.