ursus
03/23/2024, 9:19 PMSnippet
I want to show a bottom sheet with material3, works for the most part.
However, I need to hoist my state all the way to ViewModel
because.. reasons.
But now when viewModel sets the state to false
, then modal hides instantly.
Is there a way for it to be dismissed with animation?
// If I swap bottom sheet for AlertDialog
then everything works as expected, so I don't think my usage of the API is not unreasonableStylianos Gakis
03/24/2024, 12:50 AMursus
03/24/2024, 2:13 AMif
anyways, having a plain if
statement control it is from the official sample docs here https://developer.android.com/jetpack/compose/components/bottom-sheets
any idea as how to do what you're suggesting?Stylianos Gakis
03/24/2024, 12:34 PMursus
03/24/2024, 1:40 PM@Composable
fun Screen() {
val showBottomSheet by viewModel.showBottomSheet.collectAsState()
if (showBottomSheet) {
val sheetState = rememberModalBottomSheetState()
ModalBottomSheet(
onDismissRequest = {
viewModel.dismissBottomSheet()
},
sheetState = sheetState,
) {
...
}
}
class MyViewModel {
val _showBottomSheet = MutableStateFlow<Boolean>(false)
val showBottomSheet: Flow<Boolean> get() = _showBottomSheet
init {
scope.launch {
delay(5_000)
_showBottomSheet.value = true <--------------------
delay(5_000)
_showBottomSheet.value = false <--------------------
}
}
fun dismissBottomSheet() {
_showBottomSheet.value = false
}
}
here you goursus
03/24/2024, 2:10 PMsheetState.hide/show
?ursus
03/24/2024, 2:11 PMStylianos Gakis
03/24/2024, 6:15 PMshowBottomSheet
and depending on what it is show or hide accordingly.
Then make sure to keep the state object and then composable call itself outside of an if statement so that it's always present in composition.
That can be a decent way to start with this, and you can adjust accordingly if you get it to work.ursus
03/24/2024, 8:06 PMursus
03/24/2024, 8:07 PMColumn {
var showModal by remember { mutableStateOf(false) }
PrimaryPositiveButton(
modifier = Modifier.fillMaxWidth(),
text = "Open"
) {
Log.d("Default", "Show")
showModal = true
}
val sheetState = rememberModalBottomSheetState()
LaunchedEffect(key1 = showModal) {
Log.d("Default", "x=$showModal")
if (showModal) {
sheetState.show()
} else {
sheetState.hide()
}
}
ModalBottomSheet(
sheetState = sheetState,
onDismissRequest = { showModal = false }
) {
Text("Hello")
PrimaryPositiveButton(
modifier = Modifier.fillMaxWidth(),
text = "Click"
) {
Log.d("Default", "Close")
showModal = false
}
}
}
and when I hit the button in the sheet, I see "Close" printed, but no recomposition is happening