Sub topic 1
The Material button widgets: Elevated, Filled, Outlined and Text
Flutter's Material library ships several button widgets that share one design: a tappable area, a label, ink feedback, and a disabled state. This chapter explains the four main families (ElevatedButton, FilledButton, OutlinedButton and TextButton), how to choose between them, and the errors that come up most often. Later chapters cover disabling, styling and gestures in more detail.
Prerequisites
This chapter assumes you can do the following:
- Read basic Dart syntax, including functions and closures.
- Build a widget tree with
StatelessWidgetandStatefulWidget. - Call
setStateto update the UI. - Use layout widgets such as
Row,Column,StackandPadding.
The examples assume Flutter 3.x with Material 3 enabled, which is the default for new projects since Flutter 3.16.
What the four button widgets have in common
All four widgets extend ButtonStyleButton. That shared base class gives them the same core API:
onPressedis the callback that runs on a tap. It is the main way to control whether the button is enabled.onLongPressis an optional callback for a long press.childis the content, usually aText, but it can be any widget.styleis aButtonStylethat overrides the defaults.focusNode,autofocusandclipBehaviorcontrol keyboard focus and clipping.
Each widget also has a static styleFrom helper. It builds a ButtonStyle from plain values such as colors and padding. Styling is covered in a later chapter.
Because the API is shared, you can often switch one family for another by changing only the class name. What changes is the default look and the visual weight.
The four families
| Widget | Default look | Visual weight | Typical use |
|---|---|---|---|
| `FilledButton` | Solid fill with the primary color | Highest | The single main action on a screen |
| `ElevatedButton` | Light surface with a shadow | Medium to high | Actions that need to stand out from a busy background |
| `OutlinedButton` | Transparent with a thin border | Medium | Secondary actions next to a primary one |
| `TextButton` | Label only, no fill or border | Lowest | Low-emphasis actions, dialogs and toolbars |
FilledButton
FilledButton was added in Flutter 3.7 for Material 3. It has a solid background in the theme's primary color and a contrasting label. Use it for the one action you most want the user to take, such as "Save" or "Continue".
The following example is a complete counter button. Its label rebuilds each time setState runs.
import 'package:flutter/material.dart';
class CounterButton extends StatefulWidget {
const CounterButton({super.key});
@override
State<CounterButton> createState() => _CounterButtonState();
}
class _CounterButtonState extends State<CounterButton> {
int _count = 0;
@override
Widget build(BuildContext context) {
return FilledButton(
onPressed: () => setState(() => _count++),
child: Text('Pressed $_count times'),
);
}
}The callback passes a closure to setState. The closure changes _count, and Flutter then calls build again, so the Text shows the new value.
FilledButton also has a FilledButton.tonal constructor. It uses a softer fill from the secondary container color. It sits between a filled and an outlined button in emphasis.
ElevatedButton
ElevatedButton has a surface-colored background and a small shadow. In Material 3 the shadow is subtle. The shadow grows slightly when the button is pressed or hovered. Use it when a flat button would get lost, for example on top of an image or a patterned background. On a plain screen, it can look similar to a tonal filled button.
ElevatedButton(
onPressed: _canSubmit ? _submit : null,
child: const Text('Submit'),
)Here _canSubmit is a boolean field and _submit is a method of the surrounding State class. When _canSubmit is false, the button receives null and becomes disabled.
OutlinedButton
OutlinedButton draws a thin border and no fill. It works well as the partner of a filled button. A common pairing is a filled "Save" next to an outlined "Cancel".
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
OutlinedButton(
onPressed: () {},
child: const Text('Cancel'),
),
const SizedBox(width: 12),
FilledButton(
onPressed: () {},
child: const Text('Save'),
),
],
)TextButton
TextButton has no fill and no border, so it shows only a label. The ink splash appears only on press or hover. Use it for the lowest-emphasis actions: "Learn more", dialog buttons, and actions in a toolbar or a card. Because it has no visible boundary, users may not see it as tappable unless the label color and context make that clear.
Choosing the right widget
Pick a button by the importance of the action, not by taste. A useful order of decisions:
- Find the one action the user is most likely to take on the screen. Make it a
FilledButton. - Make the alternatives to it
OutlinedButtonorFilledButton.tonal. - Make the least important actions
TextButton. - Use
ElevatedButtonwhen the button needs depth to be seen against its background.
Avoid putting several FilledButtons side by side. If everything has the highest emphasis, nothing stands out.
Enabled and disabled in one rule
A button is enabled only when onPressed or onLongPress is not null. If both are null, the button is disabled. It then gets the disabled colors from the theme and ignores taps.
FilledButton(
onPressed: null,
child: Text('Not available'),
)Note that a button with onPressed: () {} is enabled, even though the callback does nothing. This is a frequent source of confusion. If a button looks active but does nothing, check whether you passed an empty closure where you meant null. If a button looks grey and you expect it to work, check whether the value is null because of a condition that never became true.
Buttons with icons
Each family has a .icon constructor that lays out an icon and a label together with correct spacing.
FilledButton.icon(
onPressed: () {},
icon: const Icon(Icons.save),
label: const Text('Save'),
)The constructor takes icon and label instead of child. Do not build your own Row for this case. The built-in layout adjusts padding for the icon automatically. Icon-only buttons use a different widget, IconButton, which a later chapter covers.
Sizing and layout behavior
Material buttons have a default minimum size, which is 64 by 40 logical pixels in Material 3, and a minimum tap target of 48 by 48. The tap target grows past the visible shape when the shape is smaller than 48, so small buttons remain easy to hit. This behavior comes from the theme's materialTapTargetSize setting.
A button takes the width its label needs, plus padding. It does not stretch by default. Common ways to change this:
- Wrap the button in a
SizedBoxwith a fixedwidthfor a set size. - Place it in a
ColumnwithcrossAxisAlignment: CrossAxisAlignment.stretchfor full width. - Use
style: FilledButton.styleFrom(minimumSize: const Size.fromHeight(48))for full width inside a parent that gives it bounded width.
A long label wraps or overflows if its parent constrains the width. Inside a Row, wrap the button in Expanded or Flexible to prevent an overflow error.
Common errors
- A disabled button you did not intend. The cause is almost always a
nullcallback. Print or inspect the condition that decides between the callback andnull. - A button with an unbounded width error. A
Rowgives its children unbounded width. A button that usesminimumSize: Size.fromHeight(48)ordouble.infinityas the width fails inside aRow. Wrap it inExpanded. - Calling the function instead of passing it. Writing
onPressed: _submit()runs_submitduringbuildand passes its return value. WriteonPressed: _submitoronPressed: () => _submit(). - No visible response to a tap. Buttons show their ink on the nearest
Materialancestor. Inside aMaterialAppandScaffoldthis works by default. Without one, you see an error or no feedback. - Old widget names in older tutorials.
RaisedButton,FlatButtonandOutlineButtonwere removed. UseElevatedButton,TextButtonandOutlinedButton.
Material 3 and theming
In Material 3, the colors of each button come from the app's ColorScheme. You usually do not set colors on individual buttons. Instead, you set a theme once, and every button of that kind follows it.
MaterialApp(
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.teal),
filledButtonTheme: FilledButtonThemeData(
style: FilledButton.styleFrom(
minimumSize: const Size(88, 48),
),
),
),
home: const Scaffold(body: Center(child: CounterButton())),
)Each family has its own theme class: ElevatedButtonThemeData, FilledButtonThemeData, OutlinedButtonThemeData and TextButtonThemeData. A style set directly on a button overrides the theme style, and the theme style overrides the defaults.
Summary of the key points
- The four families share an API and differ in emphasis.
FilledButtonis for the primary action,OutlinedButtonand tonal buttons are for secondary actions, andTextButtonis for the least important actions.ElevatedButtonadds depth when a button must stand out from its background.- A
nullonPressedand a nullonLongPresstogether disable the button. - Theme classes set shared styling in one place.
Exercise
A form has a "Submit" action, a "Cancel" action and a "Help" link. Which Material button widget would you use for each, and how would you disable "Submit" until the form is valid?
Show answer
Use a FilledButton for "Submit" because it is the primary action, an OutlinedButton for "Cancel" because it is a secondary action, and a TextButton for "Help" because it is the lowest-emphasis action. To disable "Submit", pass null as onPressed while the form is invalid, for example onPressed: isValid ? submit : null, and call setState when the validity changes so the button rebuilds.
Exercise
A teammate writes FilledButton(onPressed: _save(), child: Text('Save')) and the save logic runs every time the screen rebuilds. What is wrong, and what are two correct versions?
Show answer
The parentheses call save during build and pass its return value, usually null, as onPressed. Correct versions are onPressed: save, which passes the function itself, and onPressed: () => _save(), which passes a closure that calls it on tap.