Created
October 19, 2023 05:57
-
-
Save kishan-vadoliya/98d0aff5865557afa3420aa053f68184 to your computer and use it in GitHub Desktop.
ModalSheetWithAnchor
This file contains 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
// https://developersbreach.com/modal-bottom-sheet-jetpack-compose-android/ | |
@Composable | |
fun BottomSheetWithAnchor() { | |
val sheetState = rememberBottomSheetState(initialValue = BottomSheetValue.Collapsed) | |
val scope = rememberCoroutineScope() | |
val sheetScaffoldState = rememberBottomSheetScaffoldState( | |
bottomSheetState = sheetState | |
) | |
BottomSheetScaffold( | |
scaffoldState = sheetScaffoldState, | |
sheetElevation = 0.dp, | |
sheetBackgroundColor = Color.Transparent, | |
sheetPeekHeight = 49.dp, | |
sheetContent = { | |
Column( | |
horizontalAlignment = Alignment.CenterHorizontally, | |
verticalArrangement = Arrangement.Center | |
) { | |
IconButton(onClick = { | |
scope.launch { | |
if (sheetState.isCollapsed) { | |
sheetState.expand() | |
} else if (sheetState.isExpanded) { | |
sheetState.collapse() | |
} | |
} | |
}) { | |
val icon = if (sheetState.isExpanded) { | |
Icons.Filled.KeyboardArrowDown | |
} else { | |
Icons.Filled.KeyboardArrowUp | |
} | |
Icon( | |
imageVector = icon, | |
contentDescription = "Icon button" | |
) | |
} | |
BottomSheetContent() | |
} | |
} | |
) | |
} | |
@Composable | |
fun BottomSheetContent() { | |
Surface( | |
modifier = Modifier.height(300.dp), | |
color = Color(0xff7353ba) | |
) { | |
Column( | |
modifier = Modifier.fillMaxSize(), | |
horizontalAlignment = Alignment.CenterHorizontally | |
) { | |
Text( | |
text = "Modal Bottom Sheet", | |
fontSize = 20.sp, | |
modifier = Modifier.padding(10.dp), | |
color = Color.White | |
) | |
Divider( | |
modifier = Modifier.padding(5.dp), | |
color = Color.White | |
) | |
Text( | |
text = stringResource(R.string.greeting), | |
fontSize = 15.sp, | |
fontStyle = FontStyle.Italic, | |
color = Color.White, | |
modifier = Modifier.padding(10.dp) | |
) | |
} | |
} | |
} | |
// Animating the sheet | |
val sheetState = rememberModalBottomSheetState( | |
initialValue = ModalBottomSheetValue.Hidden, | |
animationSpec = spring( | |
dampingRatio = Spring.DampingRatioHighBouncy | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment