How to get the height of the visible region of bottomsheet using Jetpack Compose?

664 Views Asked by At

When using ModalBottomSheetLayout in Jetpack Compose, how can I find out the height of the visible region given the content may be dynamic?

I have tried using,

  • LocalConfiguration.current.screenHeightDp
  • BoxWithConstraints
  • onGloballyPositioned Modifier.

Sample code for reference,

@OptIn(ExperimentalMaterialApi::class)
@Composable
fun BottomSheetHeight() {
    val coroutineScope = rememberCoroutineScope()
    val modalBottomSheetState: ModalBottomSheetState =
        rememberModalBottomSheetState(ModalBottomSheetValue.Hidden)

    ModalBottomSheetLayout(
        sheetState = modalBottomSheetState,
        sheetContent = {
            Text("How to find out the height of this bottom sheet when the content is dynamic")
        },
    ) {
        Box(
            modifier = Modifier.fillMaxSize(),
            contentAlignment = Alignment.Center,
        ) {
            Button(
                onClick = {
                    coroutineScope.launch {
                        if (modalBottomSheetState.currentValue == modalBottomSheetState.targetValue) {
                            modalBottomSheetState.show()
                        }
                    }
                },
            ) {
                Text("Open Bottom Sheet")
            }
        }
    }
}

Created an issue in the issue tracker.
https://issuetracker.google.com/issues/287390075

0

There are 0 best solutions below