Tim Malseed
09/15/2021, 12:04 PMBottomSheet
that sits above the BottomAppBar
. As the sheet is expanded, I'd like the BottomAppBar
to scroll off screen.
I've managed to sort of achieve this by tying the `BottomAppBar's`y offset to the bottom sheet's offset amount (derived from BottomSheetState
direction & progress:
val bottomSheetScaffoldState = rememberBottomSheetScaffoldState()
Scaffold (
bottomBar = {
BottomAppBar(Modifier.offset(y = 56.dp * (1f - bottomSheetState.offsetFraction()))
}
) { padding ->
BottomSheetScaffold(Modifier.padding(bottom = 56.dp * bottomSheetState.offsetFraction()))
}
The problem is that the bottom sheet scroll/fling events now seem to be sort of interrupted. I guess having the padding change out from underneath it while it's scrolling causes issues with the size calculations or something along those lines.
Just wondering if anyone can recommend an approach for this?Tim Malseed
09/15/2021, 12:04 PMTim Malseed
09/15/2021, 12:08 PM@OptIn(ExperimentalMaterialApi::class)
@Composable
fun Root() {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colors.background
) {
val bottomSheetScaffoldState = rememberBottomSheetScaffoldState()
Scaffold(
bottomBar = {
BottomAppBar(
Modifier.offset(y = 56.dp * (1f - bottomSheetScaffoldState.bottomSheetState.offsetFraction()))
) {
AppBottomNavigation(
modifier = Modifier,
selected = false
) {
}
}
}) {
BottomSheetScaffold(
modifier = Modifier.padding(bottom = 56.dp * (bottomSheetScaffoldState.bottomSheetState.offsetFraction())),
sheetContent = {
Box(
Modifier
.fillMaxSize()
.background(MaterialColors.primary)
)
},
scaffoldState = bottomSheetScaffoldState
) { padding ->
Box(
Modifier
.fillMaxSize()
.background(Color.Green.copy(alpha = 0.5f))
)
}
}
}
}
@OptIn(ExperimentalMaterialApi::class)
fun BottomSheetState.offsetFraction(): Float {
var offsetFraction = progress.fraction
if (direction == 0f) {
offsetFraction = if (isExpanded) 0f else 1f
}
if (direction < 0) {
offsetFraction = 1f - offsetFraction
}
return offsetFraction
}
Zach Klippenstein (he/him) [MOD]
09/15/2021, 3:31 PMgraphicsLayer
-based offset instead of a layout-based offset?Colton Idle
09/16/2021, 1:04 AMColton Idle
09/16/2021, 1:07 AMTim Malseed
09/16/2021, 1:36 AMgraphicsLayer
, and yeah that sounds like exactly the same problem!Tim Malseed
09/16/2021, 7:29 AMBottomSheetScaffold's
offset (so it moves down as the sheet scrolls up), and to modify the BottomSheetScaffold.content{}
offset in the opposite direction. By modifying offset, rather than padding, you don't affect the size of the bottom sheet, and so it's not recomposed as you scroll, so scroll/fling events are not interrupted.Tim Malseed
09/16/2021, 7:29 AM@Composable
fun Root() {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colors.background
) {
val bottomSheetScaffoldState = rememberBottomSheetScaffoldState()
Scaffold(
bottomBar = {
BottomAppBar(
modifier = Modifier
.offset(y = 56.dp * (1f - bottomSheetScaffoldState.bottomSheetState.offsetFraction())),
backgroundColor = MaterialColors.background
) {
AppBottomNavigation(
modifier = Modifier,
selected = false
) {
}
}
}) {
BottomSheetScaffold(
modifier = Modifier
.offset(y = -(56.dp * (bottomSheetScaffoldState.bottomSheetState.offsetFraction()))),
sheetContent = {
Box(
Modifier
.fillMaxSize()
.background(MaterialColors.primary)
)
},
scaffoldState = bottomSheetScaffoldState
) { padding ->
Box(
Modifier
.padding(padding)
.offset(y = 56.dp * (bottomSheetScaffoldState.bottomSheetState.offsetFraction()))
) {
Library()
}
}
}
}
}
Tim Malseed
09/16/2021, 7:29 AMColton Idle
09/16/2021, 11:16 PM