תפריט כפתור הפעולה הצף (FAB)

תפריט של כפתור פעולה צף (FAB) נפתח מ-FAB כדי להציג כמה פעולות קשורות ב-Material 3.

כך הפעולות העיקריות נשארות נגישות, וממשק המשתמש נשאר נקי כי הפעולות המשניות מוסתרות כשלא צריך אותן, במקום חיוג מהיר וכפתורי FAB קטנים שנערמים.

שימוש בתפריט ה-FAB

  • נפתח מלחצן פעולה צף (FAB) כדי להציג שתיים עד שש פעולות קשורות שצפות במסך.
  • התפריט של כפתור הפעולה הצף מופיע בגודל אחיד בכל הגדלים של כפתורי הפעולה הצפים.
  • לא משתמשים בהם עם לחצני FAB מורחבים.
  • תמיכה בקבוצות של צבעים ראשיים, משניים ושלישיים.
שלוש ערכות צבעים לתפריט FAB: ראשי, משני ושלישוני.
תרשים 1. ערכות צבעים של תפריט FAB: ראשי, משני ושלישוני.

דוגמאות

דוגמה לתפריט FAB

כדי להרחיב ולכווץ רשימה של פעולות FloatingActionButtonMenuItem, אפשר להשתמש ב-FloatingActionButtonMenu יחד עם ToggleFloatingActionButton. קטע הקוד הבא מסתיר את כפתור ה-FAB להחלפת מצב כשגוללים למטה ברשימה, ומרחיב את התפריט כשמקישים עליו:

@Composable
fun FloatingActionButtonMenuSample() {
    val listState = rememberLazyListState()
    val fabVisible by remember { derivedStateOf { listState.firstVisibleItemIndex == 0 } }

    Box {
        LazyColumn(state = listState, modifier = Modifier.fillMaxSize()) {
            for (index in 0 until 100) {
                item { Text(text = "List item - $index", modifier = Modifier.padding(24.dp)) }
            }
        }

        val items =
            listOf(
                AppIcons.Message to "Reply",
                AppIcons.People to "Reply all",
                AppIcons.Contacts to "Forward",
                AppIcons.Snooze to "Snooze",
                AppIcons.Archive to "Archive",
                AppIcons.Label to "Label",
            )

        var fabMenuExpanded by rememberSaveable { mutableStateOf(false) }

        FloatingActionButtonMenu(
            modifier = Modifier.align(Alignment.BottomEnd),
            expanded = fabMenuExpanded,
            button = {
                ToggleFloatingActionButton(
                    modifier =
                        Modifier.semantics {
                            traversalIndex = -1f
                            stateDescription = if (fabMenuExpanded) "Expanded" else "Collapsed"
                            contentDescription = "Toggle menu"
                        }
                            .animateFloatingActionButton(
                                visible = fabVisible || fabMenuExpanded,
                                alignment = Alignment.BottomEnd,
                            ),
                    checked = fabMenuExpanded,
                    onCheckedChange = { fabMenuExpanded = !fabMenuExpanded },
                ) {
                    val imageVector by remember {
                        derivedStateOf { if (checkedProgress > 0.5f) AppIcons.Close else AppIcons.Add }
                    }
                    Icon(
                        painter = rememberVectorPainter(imageVector),
                        contentDescription = null,
                        modifier = Modifier.animateIcon({ checkedProgress }),
                    )
                }
            },
        ) {
            items.forEach { item ->
                FloatingActionButtonMenuItem(
                    onClick = { fabMenuExpanded = false },
                    icon = { Icon(painter = rememberVectorPainter(item.first), contentDescription = null) },
                    text = { Text(text = item.second) },
                )
            }
        }
    }
}