FloatingActionButton class
A Material Design floating action button.
A floating action button is a circular icon button that hovers over content to promote a primary action in the application. Floating action buttons are most commonly used in the Scaffold.floatingActionButton field.
Learn more about FloatingActionButton on the Flutter YouTube channel.
Use at most a single floating action button per screen. Floating action buttons should be used for positive actions such as "create", "share", or "navigate". (If more than one floating action button is used within a Route, then make sure that each button has a unique heroTag, otherwise an exception will be thrown.)
If the onPressed callback is null, then the button will be disabled and will not react to touch. It is highly discouraged to disable a floating action button as there is no indication to the user that the button is disabled. Consider changing the backgroundColor if disabling the floating action button.
This example shows a FloatingActionButton in its usual position within a Scaffold. Pressing the button cycles it through a few variations in its foregroundColor, backgroundColor, and shape. The button automatically animates its segue from one set of visual parameters to another.
To see it in action, copy and run this code snippet on DartPad.
import 'package:material_ui/material_ui.dart';
/// Flutter code sample for [FloatingActionButton].
void main() {
runApp(const FloatingActionButtonExampleApp());
}
class FloatingActionButtonExampleApp extends StatelessWidget {
const FloatingActionButtonExampleApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(home: FloatingActionButtonExample());
}
}
class FloatingActionButtonExample extends StatefulWidget {
const FloatingActionButtonExample({super.key});
@override
State<FloatingActionButtonExample> createState() =>
_FloatingActionButtonExampleState();
}
class _FloatingActionButtonExampleState
extends State<FloatingActionButtonExample> {
// The FAB's foregroundColor, backgroundColor, and shape
static const List<(Color?, Color? background, ShapeBorder?)> customizations =
<(Color?, Color?, ShapeBorder?)>[
(null, null, null), // The FAB uses its default for null parameters.
(null, Colors.green, null),
(Colors.white, Colors.green, null),
(Colors.white, Colors.green, CircleBorder()),
];
int index = 0; // Selects the customization.
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('FloatingActionButton Sample')),
body: const Center(child: Text('Press the button below!')),
floatingActionButton: FloatingActionButton(
onPressed: () {
setState(() {
index = (index + 1) % customizations.length;
});
},
foregroundColor: customizations[index].$1,
backgroundColor: customizations[index].$2,
shape: customizations[index].$3,
child: const Icon(Icons.navigation),
),
);
}
}
This sample shows all the variants of FloatingActionButton widget as described in: https://m3.material.io/components/floating-action-button/overview.
To see it in action, copy and run this code snippet on DartPad.
import 'package:material_ui/material_ui.dart';
/// Flutter code sample for [FloatingActionButton].
void main() => runApp(const FloatingActionButtonExampleApp());
class FloatingActionButtonExampleApp extends StatelessWidget {
const FloatingActionButtonExampleApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(home: FloatingActionButtonExample());
}
}
class FloatingActionButtonExample extends StatelessWidget {
const FloatingActionButtonExample({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('FloatingActionButton Sample')),
body: Center(
child: Column(
mainAxisAlignment: .spaceEvenly,
children: <Widget>[
Row(
mainAxisAlignment: .center,
children: <Widget>[
const Text('Small'),
const SizedBox(width: 16),
// An example of the small floating action button.
//
// https://m3.material.io/components/floating-action-button/specs#669a1be8-7271-48cb-a74d-dd502d73bda4
FloatingActionButton.small(
heroTag: 'small',
onPressed: () {
// Add your onPressed code here!
},
child: const Icon(Icons.add),
),
],
),
Row(
mainAxisAlignment: .center,
children: <Widget>[
const Text('Regular'),
const SizedBox(width: 16),
// An example of the regular floating action button.
//
// https://m3.material.io/components/floating-action-button/specs#71504201-7bd1-423d-8bb7-07e0291743e5
FloatingActionButton(
heroTag: 'regular',
onPressed: () {
// Add your onPressed code here!
},
child: const Icon(Icons.add),
),
],
),
Row(
mainAxisAlignment: .center,
children: <Widget>[
const Text('Large'),
const SizedBox(width: 16),
// An example of the large floating action button.
//
// https://m3.material.io/components/floating-action-button/specs#9d7d3d6a-bab7-47cb-be32-5596fbd660fe
FloatingActionButton.large(
heroTag: 'large',
onPressed: () {
// Add your onPressed code here!
},
child: const Icon(Icons.add),
),
],
),
Row(
mainAxisAlignment: .center,
children: <Widget>[
const Text('Extended'),
const SizedBox(width: 16),
// An example of the extended floating action button.
//
// https://m3.material.io/components/extended-fab/specs#686cb8af-87c9-48e8-a3e1-db9da6f6c69b
FloatingActionButton.extended(
heroTag: 'extended',
onPressed: () {
// Add your onPressed code here!
},
label: const Text('Add'),
icon: const Icon(Icons.add),
),
],
),
],
),
),
);
}
}
This sample shows FloatingActionButton with additional color mappings as described in: https://m3.material.io/components/floating-action-button/overview.
To see it in action, copy and run this code snippet on DartPad.
import 'package:material_ui/material_ui.dart';
/// Flutter code sample for [FloatingActionButton].
void main() => runApp(const FloatingActionButtonExampleApp());
class FloatingActionButtonExampleApp extends StatelessWidget {
const FloatingActionButtonExampleApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(home: FloatingActionButtonExample());
}
}
class FloatingActionButtonExample extends StatelessWidget {
const FloatingActionButtonExample({super.key});
@override
Widget build(BuildContext context) {
final ColorScheme colorScheme = Theme.of(context).colorScheme;
Widget titleBox(String title) {
return DecoratedBox(
decoration: BoxDecoration(
color: colorScheme.inverseSurface,
borderRadius: .circular(4),
),
child: Padding(
padding: const .symmetric(horizontal: 8, vertical: 4),
child: Text(
title,
style: TextStyle(color: colorScheme.onInverseSurface),
),
),
);
}
return Scaffold(
appBar: AppBar(title: const Text('FAB Additional Color Mappings')),
body: Center(
child: Row(
mainAxisAlignment: .spaceEvenly,
children: <Widget>[
// Surface color mapping.
Column(
mainAxisSize: .min,
children: <Widget>[
FloatingActionButton.large(
foregroundColor: colorScheme.primary,
backgroundColor: colorScheme.surface,
onPressed: () {
// Add your onPressed code here!
},
child: const Icon(Icons.edit_outlined),
),
const SizedBox(height: 20),
titleBox('Surface'),
],
),
// Secondary color mapping.
Column(
mainAxisSize: .min,
children: <Widget>[
FloatingActionButton.large(
foregroundColor: colorScheme.onSecondaryContainer,
backgroundColor: colorScheme.secondaryContainer,
onPressed: () {
// Add your onPressed code here!
},
child: const Icon(Icons.edit_outlined),
),
const SizedBox(height: 20),
titleBox('Secondary'),
],
),
// Tertiary color mapping.
Column(
mainAxisSize: .min,
children: <Widget>[
FloatingActionButton.large(
foregroundColor: colorScheme.onTertiaryContainer,
backgroundColor: colorScheme.tertiaryContainer,
onPressed: () {
// Add your onPressed code here!
},
child: const Icon(Icons.edit_outlined),
),
const SizedBox(height: 20),
titleBox('Tertiary'),
],
),
],
),
),
);
}
}
See also:
- Scaffold, in which floating action buttons typically live.
- ElevatedButton, a filled button whose material elevates when pressed.
- material.io/design/components/buttons-floating-action-button.html
- m3.material.io/components/floating-action-button
- Inheritance
-
- Object
- DiagnosticableTree
- Widget
- StatelessWidget
- FloatingActionButton
Constructors
- FloatingActionButton({Key? key, Widget? child, String? tooltip, Color? foregroundColor, Color? backgroundColor, Color? focusColor, Color? hoverColor, Color? splashColor, Object? heroTag = const _DefaultHeroTag(), double? elevation, double? focusElevation, double? hoverElevation, double? highlightElevation, double? disabledElevation, required VoidCallback? onPressed, MouseCursor? mouseCursor, bool mini = false, ShapeBorder? shape, Clip clipBehavior = Clip.none, FocusNode? focusNode, bool autofocus = false, MaterialTapTargetSize? materialTapTargetSize, bool isExtended = false, bool? enableFeedback})
-
Creates a circular floating action button.
const
- FloatingActionButton.extended({Key? key, String? tooltip, Color? foregroundColor, Color? backgroundColor, Color? focusColor, Color? hoverColor, Object? heroTag = const _DefaultHeroTag(), double? elevation, double? focusElevation, double? hoverElevation, Color? splashColor, double? highlightElevation, double? disabledElevation, required VoidCallback? onPressed, MouseCursor? mouseCursor, ShapeBorder? shape, bool isExtended = true, MaterialTapTargetSize? materialTapTargetSize, Clip clipBehavior = Clip.none, FocusNode? focusNode, bool autofocus = false, double? extendedIconLabelSpacing, EdgeInsetsGeometry? extendedPadding, TextStyle? extendedTextStyle, Widget? icon, required Widget label, bool? enableFeedback})
-
Creates a wider StadiumBorder-shaped floating action button with
an optional
iconand alabel.const - FloatingActionButton.large({Key? key, Widget? child, String? tooltip, Color? foregroundColor, Color? backgroundColor, Color? focusColor, Color? hoverColor, Color? splashColor, Object? heroTag = const _DefaultHeroTag(), double? elevation, double? focusElevation, double? hoverElevation, double? highlightElevation, double? disabledElevation, required VoidCallback? onPressed, MouseCursor? mouseCursor, ShapeBorder? shape, Clip clipBehavior = Clip.none, FocusNode? focusNode, bool autofocus = false, MaterialTapTargetSize? materialTapTargetSize, bool? enableFeedback})
-
Creates a large circular floating action button.
const
- FloatingActionButton.small({Key? key, Widget? child, String? tooltip, Color? foregroundColor, Color? backgroundColor, Color? focusColor, Color? hoverColor, Color? splashColor, Object? heroTag = const _DefaultHeroTag(), double? elevation, double? focusElevation, double? hoverElevation, double? highlightElevation, double? disabledElevation, required VoidCallback? onPressed, MouseCursor? mouseCursor, ShapeBorder? shape, Clip clipBehavior = Clip.none, FocusNode? focusNode, bool autofocus = false, MaterialTapTargetSize? materialTapTargetSize, bool? enableFeedback})
-
Creates a small circular floating action button.
const
Properties
- autofocus → bool
-
True if this widget will be selected as the initial focus when no other
node in its scope is currently focused.
final
- backgroundColor → Color?
-
The button's background color.
final
- child → Widget?
-
The widget below this widget in the tree.
final
- clipBehavior → Clip
-
The content will be clipped (or not) according to this option.
final
- disabledElevation → double?
-
The z-coordinate at which to place this button when the button is disabled
(onPressed is null).
final
- elevation → double?
-
The z-coordinate at which to place this button relative to its parent.
final
- enableFeedback → bool?
-
Whether detected gestures should provide acoustic and/or haptic feedback.
final
- extendedIconLabelSpacing → double?
-
The spacing between the icon and the label for an extended
FloatingActionButton.
final
- extendedPadding → EdgeInsetsGeometry?
-
The padding for an extended FloatingActionButton's content.
final
- extendedTextStyle → TextStyle?
-
The text style for an extended FloatingActionButton's label.
final
- focusColor → Color?
-
The color to use for filling the button when the button has input focus.
final
- focusElevation → double?
-
The z-coordinate at which to place this button relative to its parent when
the button has the input focus.
final
- focusNode → FocusNode?
-
An optional focus node to use as the focus node for this widget.
final
- foregroundColor → Color?
-
The default foreground color for icons and text within the button.
final
- hashCode → int
-
The hash code for this object.
no setterinherited
- heroTag → Object?
-
The tag to apply to the button's Hero widget.
final
- highlightElevation → double?
-
The z-coordinate at which to place this button relative to its parent when
the user is touching the button.
final
- hoverColor → Color?
-
The color to use for filling the button when the button has a pointer
hovering over it.
final
- hoverElevation → double?
-
The z-coordinate at which to place this button relative to its parent when
the button is enabled and has a pointer hovering over it.
final
- isExtended → bool
-
True if this is an "extended" floating action button.
final
- key → Key?
-
Controls how one widget replaces another widget in the tree.
finalinherited
- materialTapTargetSize → MaterialTapTargetSize?
-
Configures the minimum size of the tap target.
final
- mini → bool
-
Controls the size of this button.
final
- mouseCursor → MouseCursor?
-
The cursor for a mouse pointer when it enters or is hovering over the
button.
final
- onPressed → VoidCallback?
-
The callback that is called when the button is tapped or otherwise activated.
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- shape → ShapeBorder?
-
The shape of the button's Material.
final
- splashColor → Color?
-
The splash color for this FloatingActionButton's InkWell.
final
- tooltip → String?
-
Text that describes the action that will occur when the button is pressed.
final
Methods
-
build(
BuildContext context) → Widget -
Describes the part of the user interface represented by this widget.
override
-
createElement(
) → StatelessElement -
Creates a StatelessElement to manage this widget's location in the tree.
inherited
-
debugDescribeChildren(
) → List< DiagnosticsNode> -
Returns a list of DiagnosticsNode objects describing this node's
children.
inherited
-
debugFillProperties(
DiagnosticPropertiesBuilder properties) → void -
Add additional properties associated with the node.
override
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toDiagnosticsNode(
{String? name, DiagnosticsTreeStyle? style}) → DiagnosticsNode -
Returns a debug representation of the object that is used by debugging
tools and by DiagnosticsNode.toStringDeep.
inherited
-
toString(
{DiagnosticLevel minLevel = DiagnosticLevel.info}) → String -
A string representation of this object.
inherited
-
toStringDeep(
{String prefixLineOne = '', String? prefixOtherLines, DiagnosticLevel minLevel = DiagnosticLevel.debug, int wrapWidth = 65}) → String -
Returns a string representation of this node and its descendants.
inherited
-
toStringShallow(
{String joiner = ', ', DiagnosticLevel minLevel = DiagnosticLevel.debug}) → String -
Returns a one-line detailed description of the object.
inherited
-
toStringShort(
) → String -
A short, textual description of this widget.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited