To position a Floating Action Button (FAB) in Flutter, you primarily use the Scaffold
widget, which offers dedicated parameters for managing the FAB's placement. The Scaffold
provides both a floatingActionButton
parameter to accept your FloatingActionButton
widget and a floatingActionButtonLocation
parameter to specify its exact position on the screen.
Understanding FAB Positioning with Flutter's Scaffold
Flutter's Scaffold
widget is the foundational layout for implementing the basic Material Design visual layout structure. It's designed to make it straightforward to add common Material Design components, including the FloatingActionButton
.
Essential Scaffold Parameters for FAB
-
floatingActionButton
: This parameter accepts an instance of theFloatingActionButton
widget. This is where you define the button itself, including its icon,onPressed
callback, and any styling.FloatingActionButton( onPressed: () { // Handle button press }, child: const Icon(Icons.add), )
-
floatingActionButtonLocation
: This crucial parameter dictates where yourFloatingActionButton
will appear on the screen. It expects aFloatingActionButtonLocation
object. Flutter provides a rich set of predefined locations that cover most common UI patterns.The
Scaffold
class has afloatingActionButton
parameter in its constructor where you feed theFloatingActionButton
instance. TheScaffold
class also prepares afloatingActionButtonLocation
parameter, where you can specify one of the predefined locations, making it simple to control its position.
Predefined FloatingActionButtonLocation Options
Flutter offers several predefined FloatingActionButtonLocation
constants that you can use directly. These cover standard Material Design placements, typically along the bottom and center or end of the screen.
Location Constant | Description | Common Use Case |
---|---|---|
FloatingActionButtonLocation.endFloat |
Positions the FAB at the bottom-right corner of the screen, floating above other content. This is the default behavior if floatingActionButtonLocation is not specified. |
Standard FAB placement for adding new items or actions. |
FloatingActionButtonLocation.centerFloat |
Centers the FAB horizontally at the bottom of the screen, floating above other content. | When the FAB's action is central to the screen's purpose. |
FloatingActionButtonLocation.endDocked |
Positions the FAB at the bottom-right corner, but it's docked into the BottomAppBar (if present) rather than floating above it. |
Integrated with a BottomAppBar for a cohesive look. |
FloatingActionButtonLocation.centerDocked |
Centers the FAB horizontally at the bottom, docked into the BottomAppBar (if present). Often used with a notch in the BottomAppBar . |
Stylish integration with a BottomAppBar for a prominent central action. |
FloatingActionButtonLocation.miniStartFloat |
Positions a mini FAB (mini: true ) at the bottom-left corner, floating. |
Secondary, less prominent action. |
FloatingActionButtonLocation.miniEndFloat |
Positions a mini FAB (mini: true ) at the bottom-right corner, floating. |
Secondary, less prominent action. |
Practical Examples
Let's look at how to implement these common positioning strategies.
1. Basic End-Float Position (Default)
This places the FAB at the bottom-right of the screen, floating.
import 'package:flutter/material.dart';
class EndFloatFabScreen extends StatelessWidget {
const EndFloatFabScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('End-Float FAB'),
),
body: const Center(
child: Text('Content goes here'),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('FAB pressed!'))
);
},
child: const Icon(Icons.add),
),
// floatingActionButtonLocation defaults to FloatingActionButtonLocation.endFloat
// but you can explicitly set it:
// floatingActionButtonLocation: FloatingActionButtonLocation.endFloat,
);
}
}
2. Center-Docked with a BottomAppBar
For a more integrated look, especially with a BottomAppBar
, you can use centerDocked
or endDocked
. This example uses centerDocked
and shows how the FAB can be "cut" into the app bar.
import 'package:flutter/material.dart';
class CenterDockedFabScreen extends StatelessWidget {
const CenterDockedFabScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Center-Docked FAB'),
),
body: const Center(
child: Text('This is the main content area.'),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Central action triggered!'))
);
},
child: const Icon(Icons.camera_alt),
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
bottomNavigationBar: BottomAppBar(
shape: const CircularNotchedRectangle(), // Creates the notch for the FAB
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
IconButton(icon: const Icon(Icons.menu), onPressed: () {}),
IconButton(icon: const Icon(Icons.search), onPressed: () {}),
const SizedBox(width: 48), // The space for the FAB
IconButton(icon: const Icon(Icons.mail), onPressed: () {}),
IconButton(icon: const Icon(Icons.settings), onPressed: () {}),
],
),
),
);
}
}
Advanced Custom Positioning
While FloatingActionButtonLocation
constants cover most needs, you can create a completely custom FAB position by extending the FloatingActionButtonLocation
class. This allows you to define the exact getOffset
method, which calculates the Offset
for the FAB based on the Scaffold
's dimensions.
For highly custom layouts that might place the FAB outside the Scaffold
's typical bottom area, or interact with other widgets in a non-standard way, you might resort to wrapping your entire Scaffold
in a Stack
widget. This gives you absolute positioning control over all children, including the FAB, but it bypasses Scaffold
's built-in FAB management and requires more manual calculation for responsiveness. However, for a Floating Action Button specifically, sticking to Scaffold
's parameters and custom FloatingActionButtonLocation
is generally the recommended approach for maintainability and Material Design adherence.
Key Considerations
BottomAppBar
Integration: When usingcenterDocked
orendDocked
locations, it's highly recommended to pair them with aBottomAppBar
. TheBottomAppBar
has ashape
property (e.g.,CircularNotchedRectangle
) that can create a visual notch for the FAB to sit in, enhancing the Material Design aesthetic.- Accessibility: Ensure your FAB is easily reachable and doesn't obstruct important content. The standard locations are designed with accessibility in mind.
- Conflict with other widgets: Be mindful of other widgets, like
SnackBar
s, that might appear at the bottom of the screen.Scaffold
typically manages these interactions, pushing the FAB up to prevent overlap.
By leveraging the floatingActionButton
and floatingActionButtonLocation
parameters of the Scaffold
widget, you can effectively position your FloatingActionButton
to create engaging and intuitive user interfaces in Flutter.