sk eat
12/19/2024, 4:58 AMpositionInWindow()
, but it didn’t quite meet my requirements.
If anyone knows the correct approach or can provide guidance, I would greatly appreciate it! 😊Stylianos Gakis
12/19/2024, 9:41 AMStylianos Gakis
12/19/2024, 9:44 AMStylianos Gakis
12/19/2024, 9:51 AMAlex Vanyo
12/19/2024, 4:30 PMpositionOnScreen()
, but I’d be suspicious of most cases of calculating things relative to the overall screen dimension versus calculating within your windowsk eat
12/20/2024, 7:03 AMpositionInWindow()
and subtracting the screen size and navigation bar height, I was able to implement the solution successfully. It turned out that the issue was caused by not considering the navigation bar size earlier. Thank you for your response!
Code:
fun Modifier.calculateDistanceFromBottom(
onDistanceCalculated: (Dp) -> Unit
): Modifier = composed {
val density = LocalDensity.current
val view = LocalView.current
val context = LocalContext.current
val windowHeightDp = with(density) { view.rootView.height.toDp() }
val systemBottomPadding = context.getBottomNavigationBarHeight()
var lastDistanceCache by remember { mutableStateOf<Dp?>(null) }
this.onGloballyPositioned { layoutCoordinates ->
val viewWindowPositionDp = with(density) { layoutCoordinates.positionInWindow().y.toDp() }
val viewHeightDp = with(density) { layoutCoordinates.size.height.toDp() }
val viewBottomPositionDp = viewWindowPositionDp + viewHeightDp
val bottomDistanceDp = windowHeightDp - viewBottomPositionDp - systemBottomPadding
if (lastDistanceCache != bottomDistanceDp) {
lastDistanceCache = bottomDistanceDp
onDistanceCalculated(bottomDistanceDp)
}
}
}