Skip to content

Instantly share code, notes, and snippets.

@muhammedesadcomert
Created January 2, 2024 07:33
Show Gist options
  • Save muhammedesadcomert/8a9b650d13d8d9b2dee7264414d79c39 to your computer and use it in GitHub Desktop.
Save muhammedesadcomert/8a9b650d13d8d9b2dee7264414d79c39 to your computer and use it in GitHub Desktop.
Get the first position of a Composable.
@Composable
fun Modifier.onFirstPlacement(onFirstPlacement: (LayoutCoordinates) -> Unit): Modifier {
var isFirstPlacement by remember { mutableStateOf(true) }
var currentPosition by remember { mutableStateOf(Offset.Zero) }
return onPlaced {
if (isFirstPlacement) {
onFirstPlacement(it)
if (currentPosition == it.positionInWindow()) {
isFirstPlacement = false
} else {
currentPosition = it.positionInWindow()
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment