Last active
December 6, 2022 14:50
-
-
Save MrNtlu/3d27beff31a3de025171dd0e90e3c3a1 to your computer and use it in GitHub Desktop.
Bottom Sheet Activity Full Code
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
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