Created
June 9, 2022 01:28
-
-
Save Leeonardoo/799daff12fdbb0ae0d0819892b18f3e9 to your computer and use it in GitHub Desktop.
A quick and bad workaround for a crash from rotating the device when a bottom sheet is too tall using Jetpack Navigation for Compose
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.app | |
import android.os.Bundle | |
import androidx.activity.compose.setContent | |
import androidx.appcompat.app.AppCompatActivity | |
import androidx.compose.animation.ExperimentalAnimationApi | |
import androidx.compose.foundation.layout.* | |
import androidx.compose.material.ExperimentalMaterialApi | |
import androidx.compose.material.ModalBottomSheetValue | |
import androidx.compose.material3.* | |
import androidx.compose.runtime.* | |
import androidx.core.view.WindowCompat | |
import androidx.navigation.plusAssign | |
import com.google.accompanist.navigation.animation.rememberAnimatedNavController | |
import dagger.hilt.android.AndroidEntryPoint | |
@OptIn(ExperimentalAnimationApi::class) | |
@AndroidEntryPoint | |
class MainActivity : AppCompatActivity() { | |
@OptIn(ExperimentalMaterial3Api::class, ExperimentalMaterialApi::class) | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
WindowCompat.setDecorFitsSystemWindows(window, false) | |
setContent { | |
val navController = rememberAnimatedNavController() | |
val bottomSheetState = com.example.app.rememberModalBottomSheetState( | |
initialValue = ModalBottomSheetValue.Hidden, | |
skipHalfExpanded = false | |
) | |
val bottomSheetNavigator = | |
com.example.app.rememberBottomSheetNavigator(bottomSheetState) | |
navController.navigatorProvider += bottomSheetNavigator | |
ExampleAppTheme { | |
val containerColor = MaterialTheme.colorScheme.surface | |
val contentColor = contentColorFor(containerColor) | |
CompositionLocalProvider(LocalContentColor provides contentColor) { | |
// TODO: Replace with M3 ModalBottomSheetLayout when available | |
com.example.app.ModalBottomSheetLayout( | |
bottomSheetNavigator = bottomSheetNavigator, | |
bottomSheetState = bottomSheetState, | |
sheetBackgroundColor = MaterialTheme.colorScheme.surfaceColorAtElevation( | |
DrawerDefaults.ModalDrawerElevation | |
), | |
sheetContentColor = contentColor, | |
sheetShape = SheetShape, | |
// Default scrim color is onSurface which is incorrect in dark theme | |
// https://issuetracker.google.com/issues/183697056 | |
scrimColor = SheetScrimColor | |
) { | |
Scaffold( | |
//Your scaffold params | |
) { paddingValues -> | |
//Your content | |
} | |
} | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.app | |
import androidx.compose.material.* | |
import androidx.compose.runtime.Composable | |
import androidx.compose.ui.Modifier | |
import androidx.compose.ui.graphics.Color | |
import androidx.compose.ui.graphics.Shape | |
import androidx.compose.ui.unit.Dp | |
import com.google.accompanist.navigation.material.BottomSheetNavigator | |
import com.google.accompanist.navigation.material.ExperimentalMaterialNavigationApi | |
@ExperimentalMaterialNavigationApi | |
@OptIn(ExperimentalMaterialApi::class) | |
@Composable | |
fun ModalBottomSheetLayout( | |
bottomSheetNavigator: BottomSheetNavigator, | |
bottomSheetState: ModalBottomSheetState, | |
modifier: Modifier = Modifier, | |
sheetShape: Shape = MaterialTheme.shapes.large, | |
sheetElevation: Dp = ModalBottomSheetDefaults.Elevation, | |
sheetBackgroundColor: Color = MaterialTheme.colors.surface, | |
sheetContentColor: Color = contentColorFor(sheetBackgroundColor), | |
scrimColor: Color = ModalBottomSheetDefaults.scrimColor, | |
content: @Composable () -> Unit | |
) { | |
ModalBottomSheetLayout( | |
sheetState = bottomSheetState, | |
sheetContent = bottomSheetNavigator.sheetContent, | |
modifier = modifier, | |
sheetShape = sheetShape, | |
sheetElevation = sheetElevation, | |
sheetBackgroundColor = sheetBackgroundColor, | |
sheetContentColor = sheetContentColor, | |
scrimColor = scrimColor, | |
content = content | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment