Fix bottom dialog sheet nested scrolling

This commit is contained in:
MM20 2022-07-31 15:38:19 +02:00
parent 7af9a44bfa
commit a0cec95aa7
No known key found for this signature in database
GPG Key ID: 0B61A8F2DEAFA389

View File

@ -57,11 +57,12 @@ fun BottomSheetDialog(
object : NestedScrollConnection { object : NestedScrollConnection {
override fun onPreScroll(available: Offset, source: NestedScrollSource): Offset { override fun onPreScroll(available: Offset, source: NestedScrollSource): Offset {
if (available.y > 0) { val delta = available.toFloat()
return super.onPreScroll(available, source) return if (delta < 0 && source == NestedScrollSource.Drag) {
swipeState.performDrag(delta).toOffset()
} else {
Offset.Zero
} }
val c = swipeState.performDrag(available.y)
return Offset(0f, c)
} }
override fun onPostScroll( override fun onPostScroll(
@ -69,31 +70,32 @@ fun BottomSheetDialog(
available: Offset, available: Offset,
source: NestedScrollSource source: NestedScrollSource
): Offset { ): Offset {
if (available.y < 0) { return if (source == NestedScrollSource.Drag) {
return super.onPreScroll(available, source) swipeState.performDrag(available.toFloat()).toOffset()
} else {
Offset.Zero
} }
val c = swipeState.performDrag(available.y)
return Offset(0f, c)
} }
override suspend fun onPreFling(available: Velocity): Velocity { override suspend fun onPreFling(available: Velocity): Velocity {
if (available.y > 0) { val toFling = Offset(available.x, available.y).toFloat()
return super.onPreFling(available) return if (toFling < 0 && swipeState.offset.value > 0) {
swipeState.performFling(velocity = toFling)
// since we go to the anchor with tween settling, consume all for the best UX
available
} else {
Velocity.Zero
} }
swipeState.performFling(available.y)
return available.copy(x = 0f)
} }
override suspend fun onPostFling( override suspend fun onPostFling(consumed: Velocity, available: Velocity): Velocity {
consumed: Velocity, swipeState.performFling(velocity = Offset(available.x, available.y).toFloat())
available: Velocity return available
): Velocity {
if (available.y < 0) {
return super.onPostFling(consumed, available)
}
swipeState.performFling(available.y)
return available.copy(x = 0f)
} }
private fun Float.toOffset(): Offset = Offset(0f, this)
private fun Offset.toFloat(): Float = this.y
} }
} }