Skip to content

Instantly share code, notes, and snippets.

@Sottti
Created October 14, 2025 09:11
Show Gist options
  • Save Sottti/b4373d2941f33d20f4a82e0c15c5a02b to your computer and use it in GitHub Desktop.
Save Sottti/b4373d2941f33d20f4a82e0c15c5a02b to your computer and use it in GitHub Desktop.
public fun Modifier.centerInParent(): Modifier =
this then layout { measurable, constraints ->
val maxWidthAllowedByParent = constraints.maxWidth
val maxHeightAllowedByTheParent = constraints.maxHeight
val placeable = measurable.measure(constraints)
val start = maxWidthAllowedByParent / 2 - placeable.width / 2
val top = maxHeightAllowedByTheParent / 2 - placeable.height / 2
layout(width = placeable.width, height = placeable.height) {
placeable.placeRelative(x = start, y = top)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment