Sub topic 1
The Material button widgets: Elevated, Filled, Outlined and Text
Prerequisites
This chapter assumes that you know basic Dart syntax, functions and closures. You should also be able to build StatelessWidget and StatefulWidget trees, use setState, and arrange widgets with Row, Column, Padding and related layout widgets.
A Material button is a widget that presents an action and invokes a callback when the user activates it. Flutter provides several button families. Choose one based on the importance of the action and the visual weight it should have.
| Button | Usual purpose | Visual weight |
|---|---|---|
| `FilledButton` | The main action on a page | High |
| `ElevatedButton` | An action that needs some separation from its background | Medium |
| `OutlinedButton` | A secondary action beside a filled action | Medium-low |
| `TextButton` | A low-emphasis action | Low |
Choosing a button family
Use one prominent filled button for the primary action, such as saving a form or confirming a purchase. Use an outlined or text button for alternatives, such as cancelling or viewing more details. An elevated button can help an action stand out from a flat surface, but avoid using many elevated buttons in the same area.
The child is usually a Text widget, but it can contain an icon and text. The onPressed callback receives no arguments. Flutter calls it after the button recognizes an activation, such as a tap or keyboard action.
flowchart TD
A[User needs an action] --> B{Primary action?}
B -->|Yes| C[FilledButton]
B -->|No| D{Needs a visible border?}
D -->|Yes| E[OutlinedButton]
D -->|No| F{Needs separation from the surface?}
F -->|Yes| G[ElevatedButton]
F -->|No| H[TextButton]The following complete app shows all four families. Each callback writes a message to the screen.
import 'package:flutter/material.dart';
void main() {
runApp(const ButtonFamiliesApp());
}
class ButtonFamiliesApp extends StatefulWidget {
const ButtonFamiliesApp({super.key});
@override
State<ButtonFamiliesApp> createState() => _ButtonFamiliesAppState();
}
class _ButtonFamiliesAppState extends State<ButtonFamiliesApp> {
String _message = 'Choose an action';
void _showMessage(String message) {
setState(() => _message = message);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Button families')),
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(_message),
const SizedBox(height: 16),
FilledButton(
onPressed: () => _showMessage('Saved'),
child: const Text('Save'),
),
OutlinedButton(
onPressed: () => _showMessage('Editing cancelled'),
child: const Text('Cancel'),
),
ElevatedButton(
onPressed: () => _showMessage('Details opened'),
child: const Text('View details'),
),
TextButton(
onPressed: () => _showMessage('Help opened'),
child: const Text('Help'),
),
],
),
),
),
);
}
}The button family communicates priority, but the callback supplies the behavior. Keep that behavior focused. For example, a button can call a method that validates a form, saves data, or navigates to another screen.
> callout note > Prefer one clear primary button in a group. Several buttons with equal visual emphasis make the next action harder to identify.
Common mistakes
Do not use a button with an empty callback when the action is unfinished. A callback such as () {} makes the control appear usable but gives no result. Also avoid choosing a button only because its name sounds appropriate. Check how it looks beside neighboring controls and whether its emphasis matches the action.
Exercise
Which button is usually the best choice for the single main action on a form, and which button is suitable for a low-emphasis alternative?
Show answer
Use a FilledButton for the main action and a TextButton for the low-emphasis alternative.