Created
September 12, 2021 12:50
-
-
Save arcadefire/7fe138c0ded1a36bee6dd57acdfa3d18 to your computer and use it in GitHub Desktop.
Bottom sheet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum class States { | |
EXPANDED, | |
COLLAPSED | |
} | |
@ExperimentalMaterialApi | |
@Composable | |
fun FullHeightBottomSheet( | |
header: @Composable () -> Unit, | |
body: @Composable () -> Unit | |
) { | |
val swipeableState = rememberSwipeableState(initialValue = States.EXPANDED) | |
val scrollState = rememberScrollState() | |
BoxWithConstraints { | |
val constraintsScope = this | |
val maxHeight = with(LocalDensity.current) { | |
constraintsScope.maxHeight.toPx() | |
} | |
val connection = remember { | |
object : NestedScrollConnection { | |
override fun onPreScroll( | |
available: Offset, | |
source: NestedScrollSource | |
): Offset { | |
val delta = available.y | |
return if (delta < 0) { | |
swipeableState.performDrag(delta).toOffset() | |
} else { | |
Offset.Zero | |
} | |
} | |
override fun onPostScroll( | |
consumed: Offset, | |
available: Offset, | |
source: NestedScrollSource | |
): Offset { | |
val delta = available.y | |
return swipeableState.performDrag(delta).toOffset() | |
} | |
override suspend fun onPreFling(available: Velocity): Velocity { | |
return if (available.y < 0 && scrollState.value == 0) { | |
swipeableState.performFling(available.y) | |
available | |
} else { | |
Velocity.Zero | |
} | |
} | |
override suspend fun onPostFling( | |
consumed: Velocity, | |
available: Velocity | |
): Velocity { | |
swipeableState.performFling(velocity = available.y) | |
return super.onPostFling(consumed, available) | |
} | |
private fun Float.toOffset() = Offset(0f, this) | |
} | |
} | |
Box( | |
Modifier | |
.swipeable( | |
state = swipeableState, | |
orientation = Orientation.Vertical, | |
anchors = mapOf( | |
0f to States.EXPANDED, | |
maxHeight to States.COLLAPSED, | |
) | |
) | |
.nestedScroll(connection) | |
.offset { | |
IntOffset( | |
0, | |
swipeableState.offset.value.roundToInt() | |
) | |
} | |
) { | |
Column( | |
Modifier | |
.fillMaxHeight() | |
.background(Color.White) | |
) { | |
header() | |
Box( | |
Modifier | |
.fillMaxWidth() | |
.verticalScroll(scrollState) | |
) { | |
body() | |
} | |
} | |
} | |
} | |
} |
If you want swipeable
behavior to occur only when interacting with the actual bottom sheet content Modifier.offset()
should be before Modifier.swipeable()
.
Modifier
.offset {
IntOffset(
0,
swipeableState.offset.value.roundToInt()
)
}
.swipeable(
state = swipeableState,
orientation = Orientation.Vertical,
anchors = mapOf(
0f to States.EXPANDED,
maxHeight to States.COLLAPSED,
)
)
.nestedScroll(connection)
This looks great. One issue I've noticed though is that when the bottom sheet is expanded and you scroll the content, it doesn't use momentum scrolling.
If you want
swipeable
behavior to occur only when interacting with the actual bottom sheet contentModifier.offset()
should be beforeModifier.swipeable()
.Modifier .offset { IntOffset( 0, swipeableState.offset.value.roundToInt() ) } .swipeable( state = swipeableState, orientation = Orientation.Vertical, anchors = mapOf( 0f to States.EXPANDED, maxHeight to States.COLLAPSED, ) ) .nestedScroll(connection)
Life Saver. Thank you!!
This is amazing @arcadefire 🪨 ⭐ !!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello, How can I make it so that I can move the bottom sheet only for the area with LazyColumn?