Hi everyone, I want to hide a modal bottom sheet b...
# compose
a
Hi everyone, I want to hide a modal bottom sheet based on the result of an API call. For this, I created a boolean named closeBottomSheet in the ViewModel. Additionally, I have another boolean variable named showBottomSheet which integrates the bottom sheet into the composition. If I change the boolean that adds the bottom sheet to the composition(showBottomSheet) in the case of success, the bottom sheet closes without animation. Therefore, I added closeBottomSheet boolean, and based on the state of this boolean, after the animation runs, the boolean that adds it to the composition is set to false in onDismissRequest. Is that the standard way to do something like hiding something? (Since BottomSheetScaffold does not support modal, I cannot use it.)
Copy code
@Composable
fun CustomModalBottomSheet(
    sheetState: SheetState,
    modifier: Modifier = Modifier,
    closeBottomSheet: Boolean = false,
    onDismissRequest: () -> Unit = {},
    content: @Composable ColumnScope.() -> Unit
) {
    LaunchedEffect(key1 = closeBottomSheet) {
        if (closeBottomSheet) {
            this.launch { sheetState.hide() }.invokeOnCompletion {
                if (!sheetState.isVisible) {
                    onDismissRequest()
                }
            }
        }
    }

    ModalBottomSheet(
        onDismissRequest = onDismissRequest,
        modifier = modifier
            .fillMaxWidth(),
        sheetState = sheetState,
        content = {
            content()
        }
    )
}