Skip to content

Instantly share code, notes, and snippets.

@angiesasmita
Created January 20, 2023 09:57
/**
* Below is a workaround to avoid crash when bottom sheet is open in landscape mode and switched
* to portrait mode https://issuetracker.google.com/182882364
* To be implemented with: androidx.compose.material:material:1.4.0-alpha04
*/
@OptIn(ExperimentalMaterialApi::class)
@Composable
fun ModalBottomSheet(
modifier: Modifier = Modifier,
) {
val configuration = LocalConfiguration.current
// Saving previous value before any config change
var previousModalBottomSheetValue by rememberSaveable { mutableStateOf(Hidden) }
// This value is not backed by state as will deliberately only changed during recomposition
val modalBottomSheetValue =
if (previousModalBottomSheetValue == Hidden) {
Hidden
} else {
// This is to avoid HalfExpanded state is used in portrait mode (which causes crash
// as HalfExpended state doesn't exist in portrait mode)
if (configuration.orientation == ORIENTATION_LANDSCAPE) {
HalfExpanded
} else {
Expanded
}
}
val sheetState = rememberModalBottomSheetState(modalBottomSheetValue)
// [previousModalBottomSheetValue] is updated for every [sheetState] changes
LaunchedEffect(sheetState) {
snapshotFlow { sheetState.currentValue }
.collect { state ->
previousModalBottomSheetValue = state
}
}
ModalBottomSheetLayout(
sheetState = sheetState,
sheetContent = {},
content = {},
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment