https://kotlinlang.org logo
#compose
Title
# compose
j

John O'Reilly

11/27/2020, 6:33 PM
Is there a way to influence the initial height of
BottomDrawerLayout
? If I'm reading following correctly it seems to be 0.5 * max height (minValue is set to 0 just before this)?
Copy code
val openValue = if (isLandscape) minValue else lerp(
            minValue,
            maxValue,
            BottomDrawerOpenFraction
        )
(with
BottomDrawerOpenFraction = 0.5f)
a

Andrew Neal

11/28/2020, 2:33 AM
You can roll your own and pass in whatever values you like, but otherwise you're kinda limited to just making the initial
drawerState = rememberBottomDrawerState(BottomDrawerValue.Open)
.
Copy code
@Composable
@OptIn(ExperimentalMaterialApi::class)
fun BottomDrawerLayout(
    modifier: Modifier = Modifier,
    drawerState: BottomDrawerState = rememberBottomDrawerState(BottomDrawerValue.Open),
    drawerShape: Shape = MaterialTheme.shapes.large,
    drawerElevation: Dp = DrawerConstants.DefaultElevation,
    drawerBackgroundColor: Color = MaterialTheme.colors.surface,
    drawerContentColor: Color = contentColorFor(drawerBackgroundColor),
    drawerOpenFraction: Float = DefaultDrawerOpenFraction,
    scrimColor: Color = DrawerConstants.defaultScrimColor,
    drawerContent: @Composable ColumnScope.() -> Unit,
    bodyContent: @Composable BoxScope.() -> Unit,
) {
    WithConstraints(modifier.fillMaxSize()) {
        val minValue = 0f
        val maxValue = constraints.maxHeight.toFloat()
        val openValue = lerp(minValue, maxValue, drawerOpenFraction)
        val anchors = mapOf(
            minValue to BottomDrawerValue.Expanded,
            maxValue to BottomDrawerValue.Closed,
            openValue to BottomDrawerValue.Open
        )
        Box(
            modifier = Modifier.swipeable(
                state = drawerState,
                anchors = anchors,
                orientation = Orientation.Vertical
            )
        ) {
            bodyContent()
            Canvas(modifier = Modifier.fillMaxSize().tapGestureFilter { drawerState.close() }) {
                drawRect(
                    color = scrimColor,
                    alpha = calculateScrimAlpha(openValue, maxValue, drawerState.offset.value)
                )
            }
            Surface(
                modifier = with(DensityAmbient.current) {
                    Modifier.preferredSizeIn(
                        minWidth = constraints.minWidth.toDp(),
                        minHeight = constraints.minHeight.toDp(),
                        maxWidth = constraints.maxWidth.toDp(),
                        maxHeight = constraints.maxHeight.toDp()
                    )
                }.offsetPx(y = drawerState.offset),
                shape = drawerShape,
                color = drawerBackgroundColor,
                contentColor = drawerContentColor,
                elevation = drawerElevation
            ) {
                Column {
                    drawerContent()
                }
            }
        }
    }
}

private fun calculateScrimAlpha(a: Float, b: Float, pos: Float): Float {
    return 1 - ((pos - a) / (b - a)).coerceIn(0f, 1f)
}

private const val DefaultDrawerOpenFraction = 0.75f
j

John O'Reilly

11/28/2020, 9:51 AM
Thanks @Andrew Neal
3 Views