8

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))
            }
        }
    }
}
Phil Dukhov
  • 67,741
  • 15
  • 184
  • 220
Svilen Rusev
  • 309
  • 4
  • 15
  • Inside `ModalBottomSheetLayout` we can see a composable named `Scrim` which detects tap gestures to trigger the sheet closing. Unfortunately, there is no property exposed to control that behavior. – Abhimanyu Apr 26 '22 at 09:28
  • I also did not found any exposed property, most of them are inner, and was hoping for some workaround, but..... – Svilen Rusev Apr 26 '22 at 10:10

1 Answers1

3

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 }
    )
ilkeraslan
  • 87
  • 9