Jetpack Compose Basics - Looking for an alternative to menus or dialogues? Try the Modal Bottom Sheet
Bottom Sheet is component used as supplementary surface anchored to bottom of screen. This article will be about exact type of Bottom Sheet - Modal Bottom Sheet and how to use it in Jetpack Compose.
It's an alternative to menus or dialogs, it has to be dismissed (either by swipe or by some other user interaction) to interact with underlaying content of screen. In Jetpack Compose, bottom sheet is implemented via ModalBottomSheet composable.
How to create Bottom Sheet in Jetpack Compose?
ModalBottomSheet has 3 possible state defined through sheetState parameter of type ModalBottomSheetValue. Possible values are Hidden, Expanded or HalfExpanded. Default value in sheet state is Hidden, own state can be remembered like this
ModalBottomSheet, as usual in Jetpack Compose, has many parameters for customisation, two of them are mandatory.
- sheetContent - composable, containing content of bottom sheet
- content - composable, containing content of screen behind the bottom sheet. This content is overlayed by scrimColor when bottom sheet is in Expanded state
- sheetState - controls state of ModalBottomSheet
- modifier - used for changes from the caller side
- sheetShape - shape of bottom sheet e.g. *RoundedCornerShape*(topStart = 24.*dp*, topEnd = 24.*dp*)
- sheetElevation - elevation of bottom sheet in dp
- sheetBackgroundColor - background color of bottom sheet, defaultly set to surface color
- sheetContentColor - content color contrasting on background color, default value is computed using contentColorFor function
- scrimColor - color of overlay applied on content when bottom sheet is visible
How to open ModalBottomSheet on Button click?
There are show() and hide() functions ready for animated transition of ModalBottomSheet which can be called on modalBottomSheetState. These are suspend functions, because animation run as coroutine, scope or LaunchedEffect is needed to use them.
How to disable Half Expanded state in ModalBottomSheet?
In some cases, HalfExpanded state is not desirable and luckily there is a way to disable it by returning false in confirmStateChange lambda.
More articles from the Jetpack Compose Basics series:
- Learn how to use Snackbar properly
- Try Scaffold an put together several material components with the right layout!
- How to load images from Bitmap, Vector, Painter or from URL with Coil
- How to use text field composables to meet the Material design specification
- How to use Backdrop Scaffold composable
- How to use and create own CompositionLocal