Skip to content

Instantly share code, notes, and snippets.

@mertceyhan
Last active April 13, 2022 10:13
Show Gist options
  • Save mertceyhan/5f92c2b9a68646419202706e9ddca26a to your computer and use it in GitHub Desktop.
Save mertceyhan/5f92c2b9a68646419202706e9ddca26a to your computer and use it in GitHub Desktop.
@ExperimentalMaterialApi
@Composable
fun OverlayBottomSheetScaffold(
sheetContent: @Composable ColumnScope.() -> Unit,
scaffoldState: BottomSheetScaffoldState = rememberBottomSheetScaffoldState(),
sheetPeekHeight: Dp = 0.dp,
maxOverlayAlpha: Float = 0.7f,
overlayColor: Color = Color.Black,
cancelable: Boolean = true,
content: @Composable (PaddingValues) -> Unit,
) {
BottomSheetScaffold(
sheetContent = sheetContent,
scaffoldState = scaffoldState,
sheetPeekHeight = sheetPeekHeight
) {
Box {
content(it)
val bottomSheetState = scaffoldState.bottomSheetState
val bottomSheetProgress = bottomSheetState.progress
val coroutineScope = rememberCoroutineScope()
if ((bottomSheetProgress.to == BottomSheetValue.Expanded) or (bottomSheetState.currentValue == BottomSheetValue.Expanded)) {
val fraction = bottomSheetProgress.fraction
val alpha = min(fraction, maxOverlayAlpha)
val alphaDifference = maxOverlayAlpha.minus(alpha)
Box(
modifier = Modifier
.fillMaxSize()
.alpha(if (bottomSheetProgress.to == BottomSheetValue.Collapsed) alphaDifference else maxOverlayAlpha)
.background(color = overlayColor)
.clickable(
enabled = cancelable,
interactionSource = remember { MutableInteractionSource() },
indication = null
) {
if (bottomSheetState.isExpanded) {
coroutineScope.launch {
scaffoldState.bottomSheetState.collapse()
}
}
}
)
}
}
}
}
@ExperimentalMaterialApi
@Composable
fun SampleScreen() {
val scaffoldState = rememberBottomSheetScaffoldState()
val coroutineScope = rememberCoroutineScope()
OverlayBottomSheetScaffold(
sheetContent = {
Column(modifier = Modifier.padding(16.dp)) {
Box(modifier = Modifier.fillMaxWidth(), contentAlignment = Alignment.Center) {
Box(
modifier = Modifier
.height(5.dp)
.width(35.dp)
.clip(RoundedCornerShape(percent = 50))
.background(Color.Gray)
)
}
Spacer(modifier = Modifier.height(16.dp))
Text(text = "Lorem ipsum dolor sit amet", style = MaterialTheme.typography.h6)
Spacer(modifier = Modifier.height(16.dp))
Text(text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.")
}
},
scaffoldState = scaffoldState
) {
Column(
modifier = Modifier
.fillMaxSize()
.padding(16.dp),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Button(
modifier = Modifier.fillMaxWidth(),
onClick = {
coroutineScope.launch {
scaffoldState.bottomSheetState.expand()
}
}) {
Text("Expand")
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment