I couldn't fix the unwanted recomposition. What happens is whenever it recomposes, it goes back to the initialValue: SheetValue.
In my case i'm using rememberStandardBottomSheetState
which has a default value of initialValue: SheetValue = PartiallyExpanded
so everytime it recomposes my bottomSheet "minimises" ...
My work around is to set my initial value to a saveable state and updated it whenever my state changes so when it recomposes the bottomSheet keeps its state.
var sheetState by remember { mutableStateOf(SheetValue.PartiallyExpanded) }
If your bottomSheet can only change via "dragging" you can intercept the states changes via confirmValueChange
found in rememberStandardBottomSheetState
in my case i could also expand() / "minimise"() via the dragHandle onClicks. You can take inspiration from my code :
@Composable
private fun StickyBottomSheet(
content: @Composable ColumnScope.() -> Unit,
title: String? = null,
onStateChange: (BottomSheetState) -> Unit = {}
){
val scope = rememberCoroutineScope()
var sheetState by remember { mutableStateOf(SheetValue.PartiallyExpanded) }
val state = rememberStandardBottomSheetState(
initialValue = sheetState,
confirmValueChange = { sheetValue ->
sheetState = sheetValue
when (sheetValue) {
SheetValue.Hidden -> false
SheetValue.Expanded -> {
onStateChange(BottomSheetState.Expanded)
true
}
SheetValue.PartiallyExpanded -> {
onStateChange(BottomSheetState.Minimised)
true
}
}
}
)
val scaffoldState = rememberBottomSheetScaffoldState(bottomSheetState = state)
BottomSheetScaffold(
sheetContent = content,
scaffoldState = scaffoldState,
sheetShadowElevation = 8.dp,
sheetDragHandle = {
DragHandle(
title = title,
onClick = {
scope.launch {
if (scaffoldState.bottomSheetState.currentValue == SheetValue.PartiallyExpanded) {
sheetState = SheetValue.Expanded
scaffoldState.bottomSheetState.expand()
onStateChange(BottomSheetState.Expanded)
} else {
scaffoldState.bottomSheetState.show()
onStateChange(BottomSheetState.Dismissed)
}
}
},
expanded = scaffoldState.bottomSheetState.currentValue == SheetValue.Expanded
)
}
) {
}
}
@Composable
private fun DragHandle(title: String?, onClick: () -> Unit = {}, expanded: Boolean){
Surface(
modifier = Modifier.height(56.dp)
) {
AnimatedVisibility(visible = expanded, enter = fadeIn(), exit = fadeOut()) {
Box(modifier = Modifier.fillMaxWidth()) {
Box(
Modifier
.padding(top = 16.dp)
.size(width = 32.dp, height = 4.dp)
.background(wjColorScheme.outline)
.align(Alignment.TopCenter)
)
IconButton(
modifier = Modifier.align(Alignment.CenterEnd),
onClick = { onClick() },
content = { Icon(R.drawable.ic_close_24 )})
}
}
AnimatedVisibility(!expanded, enter = fadeIn(), exit = fadeOut()) {
Row(modifier = Modifier.clickable { onClick() }
) {
ListItem(
leadingContent = { Icon(R.drawable.ic_add_24) },
text2 = title
)
}
}
}
}
LaunchedEffect
– Olsewski