Jetpack Compose Bottomsheet with empty sheet content is always expnaded

Issue

I’m trying to achieve modalBottomSheet by BottomSheetScaffold for some custom implementation.

here is my BottomSheetScaffold

BottomSheetScaffold(
    sheetPeekHeight = 0.dp,
    scaffoldState = bottomSheetState,
    sheetBackgroundColor = Color.Transparent,
    backgroundColor = Color.Transparent,
    sheetElevation = 0.dp,
    sheetShape = RoundedCornerShape(topStart = 36.dp, topEnd = 36.dp),
    snackbarHost = snackBarHost,
    sheetContent = { bottomSheet() }) {
    Box(Modifier.fillMaxSize()) {
        val coroutineScope = rememberCoroutineScope()
        sheetContent()
        Scrim(
            color = Primary,
            alpha = bottomSheetState.currentFraction * 0.5f,
            onDismiss = {
                coroutineScope.launch { bottomSheetState.bottomSheetState.collapse() }
            },
            visible = bottomSheetState.bottomSheetState.targetValue != BottomSheetValue.Collapsed && bottomSheetState.bottomSheetState.currentValue != BottomSheetValue.Collapsed
        )
    }
}

When this scaffold is called by some screen, the sheetContent() will be replaced as screen content. My problem here is when bottomSheet() is empty on that screen and thus there is no height, bottom sheet state think it is expanded while I just not put composable inside bottomSheet() and it just fill based on some condition with no default composable. Because of that the Scrim() function will be visible and when I click on it this exception will throw

java.lang.IllegalArgumentException: The target value must have an associated anchor.

Solution

It seems while sheetContent is necessary for BottomSheetScaffold there is no way to deal with empty value because BottomSheetState class that handle’s swiping need anchor to get height and empty value cause’s unexpected result

Answered By – Vahid

Answer Checked By – Katrina (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *