androidkotlinandroid-jetpack-composebottom-sheetvertical-scrolling

How do I enable vertical scrolling inside the compose modalBottomSheet and disable the event that the bottomSheet closes?


    val sheetState = rememberModalBottomSheetState(
        skipPartiallyExpanded = true,
        confirmValueChange = {
            true
        }
    )

    ModalBottomSheet(
        sheetState = sheetState,
        content = {
            Box(
                modifier = Modifier
                    .fillMaxSize()
                    .verticalScroll(rememberScrollState())
                    .padding(10.dp)
            ) {
                content()
            }
        }
    )

Vertical scrolling is possible when the code is written as follows, but there is a problem that the bottom sheet closes when scrolling to the bottom

How do I make sure the bottom sheet doesn't close and allow vertical scrolling?? Blocking the drag event itself won't close the bottom sheet to the bottom scroll, but you can't scroll vertically either. I didn't find a way to do this.


Solution

  • You can consider using a BottomSheetScaffold. It offers a parameter called sheetSwipeEnabled, where you can set whether you can expand or collapse the BottomSheet by swiping. This attribute seems not to be available with a ModalBottomSheet.

    You can try the following code:

    val scaffoldState = rememberBottomSheetScaffoldState()
    val scrolLState = rememberScrollState()
    
    val endReached by remember {
        derivedStateOf {
            scrollState.value == scrollState.maxValue
        }
    }
    
    BottomSheetScaffold(
        scaffoldState = scaffoldState,
        sheetPeekHeight = 128.dp,
        sheetSwipeEnabled = !scaffoldState.bottomSheetState.hasExpandedState || endReached,
        sheetContent = {
            Box(
                Modifier.fillMaxWidth().height(128.dp),
                contentAlignment = Alignment.Center
            ) {
                Text("Swipe up to expand sheet")
            }
            Box(
                modifier = Modifier
                    .fillMaxSize()
                    .verticalScroll(scrollState)
                    .padding(10.dp)
            ) {
                content()
            }
        }
    ) { innerPadding ->
        // some Scaffold content
    }
    

    This should result in the following behavior:

    You can programmatically expand or hide the BottomSheet by calling

    scaffoldState.bottomSheetState.expand() 
    scaffoldState.bottomSheetState.hide()