Is it possible to animate the dismissal of a modal...
# compose
c
Is it possible to animate the dismissal of a modal bottom sheet when using
popBackStack()
? (or if the user clicks the back button, or clicks on something that calls
navigate()
) Currently the animation only plays if the user clicks on the scrim or drags the sheet down.
It seems I could do this if I created my own
ModalBottomSheetState
and
BottomSheetNavigator
but I have the notion that’s not recommended
Or is it fine to do this?
Copy code
val modalBottomSheetState = rememberModalBottomSheetState(
      initialValue = ModalBottomSheetValue.Hidden,
      skipHalfExpanded = true,
    )
    val bottomSheetNavigator = remember(modalBottomSheetState) {
      BottomSheetNavigator(sheetState = modalBottomSheetState)
    }
Then I could just call
hide()
on the sheet state
j
When popping the bottom sheet destination off the back stack, ideally, we would animate regularly. There's an issue in the upstream Material component though where most of the time, the
hide
call will be cancelled when we update the sheet's content to be empty. Using
snapTo
means that the state will pretty much instantly snap to the requested anchor, resulting in no animation. Ideally, we'd be hiding the sheet first and then changing the content; I'll try to put up a PR. There's also an open issue in the Accompanist repo 🙂
If you just call
hide
there's a good chance that the navigator and the sheet state get out of sync. That constructor will be made internal soon, too.