Search code examples
androidandroid-jetpack-composeandroid-jetpackbottom-sheet

Disable BottomSheet outside touch in the jetpack compose


I used BottomSheet view in the jetpack compose but I want to lock the screen with bottomSheet until we click on the bottomShets's button and disable outside touching in the bottomSheet. How can I do it?

My bottomSheet:


@ExperimentalMaterialApi
@Composable
fun BottomSheet(
    modifier: Modifier = Modifier,
    composable: @Composable () -> Unit,
    scope: CoroutineScope
) {
    val bottomSheetScaffoldState = rememberBottomSheetScaffoldState(
        bottomSheetState = BottomSheetState(BottomSheetValue.Expanded)
    )
    BottomSheetScaffold(
        scaffoldState = bottomSheetScaffoldState,
        sheetContent = {
            Column(
                Modifier
                    .fillMaxWidth()
                    .height(200.dp)
                    .padding(8.dp),
                verticalArrangement = Arrangement.Center,
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                Button(colors = ButtonDefaults.buttonColors(
                    backgroundColor = AppColor.brandColor.BLUE_DE_FRANCE,
                    contentColor = AppColor.neutralColor.DOCTOR
                ),
                    shape = RoundedCornerShape(
                        small
                    ),
                    onClick = {
                        scope.launch {
                            bottomSheetScaffoldState.bottomSheetState.collapse()
                        }
                    }) {  
                }
            }
        },
        sheetPeekHeight = 0.dp,
        sheetShape = RoundedCornerShape(topEnd = medium, topStart = medium)
    ) {
        composable()
    }

}

The composable function is a google map screen


Solution

  • You can use sheetGesturesEnabled: Boolean = true/false attribute, if you want to change the bottom sheet cancelable or not cancelable from outside.

     BottomSheetScaffold(
            scaffoldState = bottomSheetScaffoldState,
            sheetGesturesEnabled = false,
            sheetContent = {
               ...
            },
            sheetPeekHeight = 0.dp,
            sheetShape = RoundedCornerShape(topEnd = medium, topStart = medium)
        ) {
            composable()
        }
    

    for proper documentation use this link: BottomSheetScaffold