Search code examples
androidandroid-jetpack-composeandroid-koin

How to set half expanded height for bottomsheet using BottomSheetScaffold in compose?


I need to show my bottomsheet first in collapsed state. And on swipping bottomsheet up, it should first fix in half height of screen first. Again on swipping up , it should expand to max height of screen. Same during collapsing. First from max height to half height, then to peek height(the height of bottomsheet which will be visible in collapsed state). Is there any way to achieve it using BottomSheetScaffold?


Solution

  • I started write that solution for you. You can beautify it

    enum class ExpandedType {
        HALF, FULL, COLLAPSED
    }
    
      @Composable
    private fun BottomSheet() {
        val configuration = LocalConfiguration.current
        val screenHeight = configuration.screenHeightDp
        var expandedType by remember {
            mutableStateOf(ExpandedType.COLLAPSED)
        }
        val height by animateIntAsState(
            when (expandedType) {
                ExpandedType.HALF -> screenHeight / 2
                ExpandedType.FULL -> screenHeight
                ExpandedType.COLLAPSED -> 70
            }
        )
        val bottomSheetScaffoldState = rememberBottomSheetScaffoldState(
            bottomSheetState = BottomSheetState(BottomSheetValue.Collapsed)
        )
        BottomSheetScaffold(
            scaffoldState = bottomSheetScaffoldState,
            sheetElevation = 8.dp,
            sheetShape = RoundedCornerShape(
                bottomStart = 0.dp,
                bottomEnd = 0.dp,
                topStart = 12.dp,
                topEnd = 12.dp
            ),
            sheetContent = {
                var isUpdated = false
                Box(
                    Modifier
                        .fillMaxWidth()
                        .height(height.dp)
                        .pointerInput(Unit) {
                            detectVerticalDragGestures(
                                onVerticalDrag = { change, dragAmount ->
                                    change.consume()
                                    if (!isUpdated) {
                                        expandedType = when {
                                            dragAmount < 0 && expandedType == ExpandedType.COLLAPSED -> {
                                                ExpandedType.HALF
                                            }
                                            dragAmount < 0 && expandedType == ExpandedType.HALF -> {
                                                ExpandedType.FULL
                                            }
                                            dragAmount > 0 && expandedType == ExpandedType.FULL -> {
                                                ExpandedType.HALF
                                            }
                                            dragAmount > 0 && expandedType == ExpandedType.HALF -> {
                                                ExpandedType.COLLAPSED
                                            }
                                            else -> {
                                                ExpandedType.FULL
                                            }
                                        }
                                        isUpdated = true
                                    }
                                },
                                onDragEnd = {
                                    isUpdated = false
                                }
                            )
                        }
                        .background(Color.Red)
                )
            },
            sheetPeekHeight = height.dp
        ) {
            Box(
                Modifier
                    .fillMaxSize()
                    .background(Color.Black)
            )
        }
    }