悬浮操作按钮 (FAB) 菜单从 FAB 打开,用于在 Material 3 中显示多个相关操作。
这样既能让用户轻松执行关键操作,又能通过在不需要时隐藏次要操作来保持界面整洁,从而取代快速拨号和堆叠的小型 FAB。
使用 FAB 菜单
- 从 FAB 打开,在屏幕上显示 2 到 6 个相关的悬浮操作。
- 在所有 FAB 尺寸中使用一种 FAB 菜单尺寸。
- 不与扩展 FAB 搭配使用。
- 支持主色、辅色和第三色颜色集。
示例
FAB 菜单示例
将 FloatingActionButtonMenu 与 ToggleFloatingActionButton 配对,以展开和收起 FloatingActionButtonMenuItem 操作列表。以下代码段会在向下滚动列表时隐藏切换 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) }, ) } } } }