אם רוצים להטמיע bottom sheet, אפשר להשתמש ב-composable ModalBottomSheet
.
אפשר להשתמש ב-content
slot, שמשתמש ב-ColumnScope
כדי להציג את התוכן של הגיליון בעמודה:
ModalBottomSheet(onDismissRequest = { /* Executed when the sheet is dismissed */ }) { // Sheet content }
שליטה במצב הגיליון באופן פרוגרמטי
כדי להרחיב ולכווץ את הגיליון באופן פרוגרמטי, משתמשים ב-SheetState
. אפשר להשתמש ב-rememberSheetState
כדי ליצור מופע של SheetState
שצריך להעביר ל-ModalBottomSheet
עם הפרמטר sheetState
. SheetState
מספק גישה לפונקציות show
ו-hide
, וגם לנכסים שקשורים למצב הנוכחי של הגיליון. הפונקציות האלה להשהיה דורשות CoroutineScope
– לדוגמה, שימוש ב-rememberCoroutineScope
– ואפשר לקרוא להן בתגובה לאירועי ממשק משתמש. חשוב להקפיד להסיר את ModalBottomSheet
מההודעה כשמסתירים את הגיליון התחתון.
val sheetState = rememberModalBottomSheetState() val scope = rememberCoroutineScope() var showBottomSheet by remember { mutableStateOf(false) } Scaffold( floatingActionButton = { ExtendedFloatingActionButton( text = { Text("Show bottom sheet") }, icon = { Icon(Icons.Filled.Add, contentDescription = "") }, onClick = { showBottomSheet = true } ) } ) { contentPadding -> // Screen content if (showBottomSheet) { ModalBottomSheet( onDismissRequest = { showBottomSheet = false }, sheetState = sheetState ) { // Sheet content Button(onClick = { scope.launch { sheetState.hide() }.invokeOnCompletion { if (!sheetState.isVisible) { showBottomSheet = false } } }) { Text("Hide bottom sheet") } } } }