I'm using ModalBottomSheetLayout
@Composable
fun BottomSheetWrapper() {
ModalBottomSheetLayout(
sheetState = sheetState,
sheetContent = {
Spacer(modifier = Modifier.height(1.dp))
content()
},
modifier = modifier,
content = {}
)}
and I want to trigger some logic when user dismisses the bottomsheet (back press or touch outside of the bottomsheet). In another words I need something similar to onCancel event of BottomSheetDialogFragment.
What would be the best way to do it with ModalBottomsheetLayout?
You can use the
sheetStateobject to observe the state changes of theModalBottomSheetLayoutand trigger the required logic when the bottom sheet is dismissed by the user. ThesheetStateobject contains anisExpandedproperty that can be used to check whether the bottom sheet is expanded or not.To listen for the bottom sheet dismissal event, you can add a
DisposableEffectcomposable that will execute a callback function when the bottom sheet is dismissed. TheDisposableEffectcomposable is used to perform a side effect when a composable is added or removed from the composition.In this implementation, the
BottomSheetWrappercomposable takes anonDismisslambda as a parameter that will be called when the bottom sheet is dismissed. We use theDisposableEffectcomposable to add and remove aModalBottomSheetCallbackthat will listen for the bottom sheet dismissal event. When the bottom sheet is dismissed, theonDismisslambda is called.