Prevent closing bottom sheet with background click with accompanist library

6.4k Views Asked by At

I have tried accompanist library for navigation from this article and I want to prevent bottom sheet to be closed when I click on the background of bottom sheet (the gray area) and to make it non clickable at all, how can I achieve this?

This is the code from the link

@Composable
fun MyApp() {
    val navController = rememberNavController()
    val bottomSheetNavigator = rememberBottomSheetNavigator()
    navController.navigatorProvider += bottomSheetNavigator


    ModalBottomSheetLayout(
        bottomSheetNavigator = bottomSheetNavigator
    ) {
        NavHost(navController, startDestination = "home") {
            composable(route = "home") {
                Button(onClick = { navController.navigate("sheet") }) {
                    Text("Click me to see something cool!")
                }
            }
            bottomSheet(route = "sheet") {
                Text("This is a cool bottom sheet!")
                Button(onClick = { navController.navigate("home") }) {
                    Text("Take me back, please!")
                }
                Spacer(modifier = Modifier.padding(200.dp))
            }
        }
    }
}
1

There are 1 best solutions below

0
On

ModalBottomSheetLayout has this sheetState parameter set as following:

sheetState: ModalBottomSheetState = rememberModalBottomSheetState(ModalBottomSheetValue.Hidden)

You can prevent the dismiss behaviour by passing this parameter as following:

// This prevents dismissing the ModalBottomSheet
    val sheetState = rememberModalBottomSheetState(
        initialValue = ModalBottomSheetValue.Hidden,
        confirmStateChange = { false }
    )