androidx.compose.material3

FloatingActionButtonMenuSince 1.10

Composable function

A FAB menu opens from a floating action button and shows two to six related actions, like "Email", "Share" and "Contact".

It has three pieces:

  1. FloatingActionButtonMenu holds everything. It opens while expanded is true.
  2. A ToggleFloatingActionButton in the button slot. It morphs between closed and open.
  3. One FloatingActionButtonMenuItem per action.
An open FAB menuExpressive theme
An open FAB menu
var expanded by remember { mutableStateOf(true) }
Box(Modifier.size(280.dp, 340.dp), contentAlignment = Alignment.BottomEnd) {
    FloatingActionButtonMenu(
        expanded = expanded,
        button = {
            ToggleFloatingActionButton(checked = expanded, onCheckedChange = { expanded = it }) {
                val icon by remember { derivedStateOf { if (checkedProgress > 0.5f) Icons.Filled.Close else Icons.Filled.Add } }
                Icon(
                    painter = rememberVectorPainter(icon),
                    contentDescription = if (expanded) "Close menu" else "Open menu",
                    modifier = Modifier.animateIcon({ checkedProgress }),
                )
            }
        },
    ) {
        FloatingActionButtonMenuItem(onClick = {}, icon = { Icon(Icons.Filled.Email, null) }, text = { Text("Email") })
        FloatingActionButtonMenuItem(onClick = {}, icon = { Icon(Icons.Filled.Share, null) }, text = { Text("Share") })
        FloatingActionButtonMenuItem(onClick = {}, icon = { Icon(Icons.Filled.Person, null) }, text = { Text("Contact") })
    }
}

Inside ToggleFloatingActionButton, checkedProgress goes from 0 (closed) to 1 (open). Use it to switch the icon, and Modifier.animateIcon { checkedProgress } to animate its color and size.

Parameters

@Composable
fun FloatingActionButtonMenu(
    expanded: Boolean,
    button: @Composable () -> Unit,
    modifier: Modifier = Modifier,
    horizontalAlignment: Alignment.Horizontal = Alignment.End,
    content: @Composable FloatingActionButtonMenuScope.() -> Unit,
)

expanded: BooleanRequired

Whether the menu is open. Pass your state here.

button: @Composable () -> UnitRequired

The button that opens and closes the menu. Use a ToggleFloatingActionButton.

modifier: ModifierOptional

Default: Modifier

Changes size, padding, or position. See the Modifier guide.

horizontalAlignment: Alignment.HorizontalOptional

Default: Alignment.End

Where the items line up: at the end (default), the start or the center.

content: @Composable FloatingActionButtonMenuScope.() -> UnitRequired

The actions. Add one FloatingActionButtonMenuItem each.

Parts