I am creating my first decent app on compose and I need your help with SheetState to make ModalBottomSheet. I want this modal bottom sheet to be only on hidden and partially expanded state.
val hasExpandedState: Boolean
get() = swipeableState.hasAnchorForValue(Expanded)
SheetState class has such a variable hasExpandedState, which as I understood could help me, but it is actually a value, so it's value couldn't be changed in my code. How to make ModalBottomSheet only in such states, is this hasExpandedState needed?
hasExpandedState can not be reassigned
The expanded state can be disabled by modifying the
confirmValueChange
parameter of the ModalBottomSheet state. The following code allows the sheet value to be set only to a hidden or partially expanded.The
partialExpand()
function will animate the ModalBottomSheet to a partially expanded state when invoked. Keep in mind that this function is a suspend function, which means that it has to be invoked inside a coroutine context. It can be done by launching a coroutine and calling the function.The
hide()
function will animate the ModalBottomSheet to a hidden state when invoked. This function is also a suspend function and it requires a coroutine context. Also, it's important to update the state which is associated with the visibility of the ModalBottomSheet composable, by wrapping it inside aninvokeOnCompletion
block, which will be executed as soon as thesheetState.hide()
execution is completed. It's recommended to include it sincehide()
function animates the state.Here is an example of a ModalBottomSheet implementation:
I highly recommend checking out the Official Documentation.