Created
January 17, 2023 22:05
-
-
Save alashow/9fc15509fd1ba4744a1400b5feff7019 to your computer and use it in GitHub Desktop.
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
/** | |
* Returns a [Shape] for BottomSheet's current [sheetState]. | |
* @return gradually un-rounding/rounding shape when [sheetState] is changing to [ModalBottomSheetValue.Expanded] or [ModalBottomSheetValue.HalfExpanded]. | |
*/ | |
@Composable | |
internal fun shape( | |
sheetState: ModalBottomSheetState, | |
radius: Dp = 16.0.dp, | |
): Shape { | |
val offsetFraction by remember { | |
derivedStateOf { | |
val isExpanding = sheetState.progress.to == ModalBottomSheetValue.Expanded | |
val isShrinking = sheetState.progress.from == ModalBottomSheetValue.Expanded && sheetState.progress.to == ModalBottomSheetValue.HalfExpanded | |
when { | |
isExpanding -> 1 - sheetState.progress.fraction | |
isShrinking -> sheetState.progress.fraction * 3 // shrink faster than expanding | |
else -> 1.0f | |
}.coerceAtMost(1.0f) | |
} | |
} | |
return RoundedCornerShape( | |
topStart = radius * offsetFraction, | |
topEnd = radius * offsetFraction, | |
) | |
} | |
@Composable | |
fun rememberBottomSheetStickyFooterOffset(sheetState: ModalBottomSheetState): State<Float> { | |
val stickyFooterOffset = remember { | |
derivedStateOf { | |
val isHiding = sheetState.progress.from == ModalBottomSheetValue.HalfExpanded && sheetState.progress.to == ModalBottomSheetValue.Hidden | |
when { | |
// sheet is hiding, slowly start hiding the sticky footer too | |
isHiding -> -(sheetState.offset.value / (1 + sheetState.progress.fraction)) | |
// otherwise, the footer should have a negative offset to appear sticky as the sheet moves | |
else -> -(sheetState.offset.value) | |
} | |
} | |
} | |
return stickyFooterOffset | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment