The FAB represents the most important action on a screen.
@OptIn(ExperimentalMaterial3ExpressiveApi::class, ExperimentalMaterial3Api::class)
@Preview
@Composable
fun AnimatedFloatingActionButtonSample() {
val listState = rememberLazyListState()
// The FAB is initially shown. Upon scrolling past the first item we hide the FAB by using a
// remembered derived state to minimize unnecessary compositions.
val fabVisible by remember { derivedStateOf { listState.firstVisibleItemIndex == 0 } }
Scaffold(
floatingActionButton = {
// A FAB should have a tooltip associated with it.
TooltipBox(
positionProvider =
TooltipDefaults.rememberTooltipPositionProvider(TooltipAnchorPosition.Above),
tooltip = { PlainTooltip { Text("Localized description") } },
state = rememberTooltipState(),
) {
MediumFloatingActionButton(
modifier =
Modifier.animateFloatingActionButton(
visible = fabVisible,
alignment = Alignment.BottomEnd,
),
onClick = { /* do something */ },
) {
Icon(
Icons.Filled.Add,
contentDescription = "Localized description",
modifier = Modifier.size(FloatingActionButtonDefaults.MediumIconSize),
)
}
}
},
floatingActionButtonPosition = FabPosition.End,
) {
LazyColumn(state = listState, modifier = Modifier.fillMaxSize()) {
for (index in 0 until 100) {
item { Text(text = "List item - $index", modifier = Modifier.padding(24.dp)) }
}
}
}
}
@OptIn(ExperimentalMaterial3ExpressiveApi::class, ExperimentalMaterial3Api::class)
@Preview
@Composable
fun MediumFloatingActionButtonSample() {
// A FAB should have a tooltip associated with it.
TooltipBox(
positionProvider =
TooltipDefaults.rememberTooltipPositionProvider(TooltipAnchorPosition.Above),
tooltip = { PlainTooltip { Text("Localized description") } },
state = rememberTooltipState(),
) {
MediumFloatingActionButton(onClick = { /* do something */ }) {
Icon(
Icons.Filled.Add,
contentDescription = "Localized description",
modifier = Modifier.size(FloatingActionButtonDefaults.MediumIconSize),
)
}
}
}