Skip to content

Instantly share code, notes, and snippets.

@MrNtlu
Last active December 6, 2022 14:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MrNtlu/3d27beff31a3de025171dd0e90e3c3a1 to your computer and use it in GitHub Desktop.
Save MrNtlu/3d27beff31a3de025171dd0e90e3c3a1 to your computer and use it in GitHub Desktop.
Bottom Sheet Activity Full Code
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
JetpackComposeBottomSheetTheme {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colors.background
) {
BottomSheetLayout()
}
}
}
}
}
@SuppressLint("UnusedMaterialScaffoldPaddingParameter")
@OptIn(ExperimentalMaterialApi::class)
@Composable
fun BottomSheetLayout() {
val coroutineScope = rememberCoroutineScope()
val modalSheetState = rememberModalBottomSheetState(
initialValue = ModalBottomSheetValue.Hidden,
confirmStateChange = { it != ModalBottomSheetValue.HalfExpanded },
skipHalfExpanded = true
)
var isSheetFullScreen by remember { mutableStateOf(false) }
val roundedCornerRadius = if (isSheetFullScreen) 0.dp else 12.dp
val modifier = if (isSheetFullScreen)
Modifier
.fillMaxSize()
else
Modifier.fillMaxWidth()
BackHandler(modalSheetState.isVisible) {
coroutineScope.launch { modalSheetState.hide() }
}
ModalBottomSheetLayout(
sheetState = modalSheetState,
sheetShape = RoundedCornerShape(topStart = roundedCornerRadius, topEnd = roundedCornerRadius),
sheetContent = {
Column(
modifier = modifier,
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center,
) {
Button(
onClick = {
isSheetFullScreen = !isSheetFullScreen
}
) {
Text(text = "Toggle Sheet Fullscreen")
}
Button(
onClick = {
coroutineScope.launch { modalSheetState.hide() }
}
) {
Text(text = "Hide Sheet")
}
}
}
) {
Scaffold {
Box(
modifier = Modifier
.fillMaxSize(),
contentAlignment = Alignment.Center,
) {
Button(
onClick = {
coroutineScope.launch {
if (modalSheetState.isVisible)
modalSheetState.hide()
else
modalSheetState.animateTo(ModalBottomSheetValue.Expanded)
}
},
) {
Text(text = "Open Sheet")
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment