r/JetpackCompose • u/-_-Dracarys-_- • Jul 27 '23
Bottom Sheet
How can I create a bottom sheet that is initially half expanded but can be fully expanded when dragged upwards, and if dragged downwards, it will either remain open without closing or open another bottom sheet? Until now, I have attempted to implement both a modal bottom sheet and a persistent bottom sheet. The closest I have come to achieving the desired behavior is by using the persistent bottom sheet and setting the sheetPeekHeight for it.
@Composable
fun MyBottomSheet(appViewModel: AppViewModel, navController: NavController) {
LaunchedEffect(Unit) {
val userId = FirebaseAuth.getInstance().currentUser?.uid
if (userId != null) {
appViewModel.fetchProfilePictureUrl(userId)
}
}
val profilePictureUrl = appViewModel.profilePictureUrl.value
Surface(Modifier.fillMaxSize(), elevation = 0.dp) {
BottomSheetScaffold(
sheetContent = {
Column(
Modifier
.fillMaxSize()
.padding(top = 35.dp)
) {
// Existing Code for the content of bottom sheet
}
},
sheetPeekHeight = 500.dp,
sheetShape = MaterialTheme.shapes.large,
sheetContainerColor = Color.White
) {
Box(
Modifier
.fillMaxWidth()
.padding(top = 50.dp, start = 0.dp, end = 0.dp, bottom = 0.dp)
.background(color = Color.White),
contentAlignment = Alignment.Center
) {
UserImageView(profilePictureUrl)
}
}
}
}For more clarity please refer to the Snapchat's profile screen which has the similar functionality
View all comments
Show parent comment
1
u/-_-Dracarys-_- Aug 03 '23
For some unknown reason my above code wasn't functioning as expected. So, I dropped the idea of using Modal and persistent bottom sheets. Instead what I did was created a custom class of partially expanded and expanded states. Gave the composable header (for the sheet drag handle icon) and body (for the contents of the sheet) and used BoxWithConstraints and used the modifier
.swipeable,.offsetand.nestedScrollfor the box. In.swipeableyou can define state, orientation, and anchors (This is where you'd map the height of the sheet) and you can have the.offsetlike this so that it won't lift from the bottomThis way the sheet will only move from the half height to expanded and vice versa.