Skip to content

Instantly share code, notes, and snippets.

@alashow
Created January 17, 2023 22:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alashow/9fc15509fd1ba4744a1400b5feff7019 to your computer and use it in GitHub Desktop.
Save alashow/9fc15509fd1ba4744a1400b5feff7019 to your computer and use it in GitHub Desktop.
/**
* 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