John O'Reilly
11/27/2020, 6:33 PMBottomDrawerLayout
? If I'm reading following correctly it seems to be 0.5 * max height (minValue is set to 0 just before this)?
val openValue = if (isLandscape) minValue else lerp(
minValue,
maxValue,
BottomDrawerOpenFraction
)
(with BottomDrawerOpenFraction = 0.5f)
Andrew Neal
11/28/2020, 2:33 AMdrawerState = rememberBottomDrawerState(BottomDrawerValue.Open)
.
@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
John O'Reilly
11/28/2020, 9:51 AM