Flutter Buttons
Buttons are how users tell your app to do something. Material 3 in Flutter offers a family of button widgets with different levels of emphasis. They all share the same core API: an onPressed callback and a child.
The Material button family
import 'package:flutter/material.dart';
void main() => runApp(const MaterialApp(home: Scaffold(body: Center(child: ButtonGallery()))));
class ButtonGallery extends StatelessWidget {
const ButtonGallery({super.key});
@override
Widget build(BuildContext context) {
return Column(
mainAxisSize: MainAxisSize.min,
children: [
FilledButton(onPressed: () {}, child: const Text('Filled')),
FilledButton.tonal(onPressed: () {}, child: const Text('Filled tonal')),
ElevatedButton(onPressed: () {}, child: const Text('Elevated')),
OutlinedButton(onPressed: () {}, child: const Text('Outlined')),
TextButton(onPressed: () {}, child: const Text('Text')),
const FilledButton(onPressed: null, child: Text('Disabled')),
],
);
}
}| Button | Emphasis | Typical use |
|---|---|---|
FilledButton |
Highest | The main action: Save, Buy, Continue |
FilledButton.tonal |
High | Important but secondary actions |
ElevatedButton |
Medium | Actions that need separation from a patterned background |
OutlinedButton |
Medium | Secondary actions: Cancel, Back |
TextButton |
Low | Dialog actions, inline links |
IconButton |
Low | Toolbar actions with just an icon |
FloatingActionButton |
High | The primary action of a screen |
Note: Passing
nulltoonPresseddisables a button: it greys out and ignores taps. Use this instead of hiding the button when an action isn't available yet.
Buttons with icons
Each button type has an .icon constructor that places an icon before the label:
import 'package:flutter/material.dart';
void main() => runApp(MaterialApp(
home: Scaffold(
body: Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
FilledButton.icon(
onPressed: () {},
icon: const Icon(Icons.download),
label: const Text('Download'),
),
const SizedBox(width: 8),
IconButton(
onPressed: () {},
icon: const Icon(Icons.share),
tooltip: 'Share',
),
IconButton.filledTonal(
onPressed: () {},
icon: const Icon(Icons.bookmark_border),
),
],
),
),
),
));Handling presses and updating state
Buttons usually change some state. Here a stateful widget tracks a quantity, and debugPrint logs each press:
import 'package:flutter/material.dart';
void main() => runApp(const MaterialApp(home: Scaffold(body: Center(child: QuantityPicker()))));
class QuantityPicker extends StatefulWidget {
const QuantityPicker({super.key});
@override
State<QuantityPicker> createState() => _QuantityPickerState();
}
class _QuantityPickerState extends State<QuantityPicker> {
int _qty = 1;
void _change(int delta) {
setState(() => _qty += delta);
debugPrint('Quantity is now $_qty');
}
@override
Widget build(BuildContext context) {
return Row(
mainAxisSize: MainAxisSize.min,
children: [
IconButton.outlined(
onPressed: _qty > 1 ? () => _change(-1) : null, // disabled at 1
icon: const Icon(Icons.remove),
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Text('$_qty', style: const TextStyle(fontSize: 24)),
),
IconButton.outlined(
onPressed: () => _change(1),
icon: const Icon(Icons.add),
),
],
);
}
}Quantity is now 2
Quantity is now 3
Styling buttons
Use the styleFrom helper of each button class to customise colours, padding and shape:
ElevatedButton(
onPressed: () {},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.green,
foregroundColor: Colors.white, // text and icon colour
padding: const EdgeInsets.symmetric(horizontal: 32, vertical: 16),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
textStyle: const TextStyle(fontSize: 18),
),
child: const Text('Check out'),
)Tip: To restyle every button of a kind across the app, set it once in the theme, e.g.
ThemeData(filledButtonTheme: FilledButtonThemeData(style: ...)), instead of repeatingstyle:everywhere.
Making anything tappable
Not every tappable thing is a button. Wrap any widget in InkWell (Material ripple effect) or GestureDetector (no visual feedback, supports double-tap, long-press, drag…):
InkWell(
onTap: () => debugPrint('Card tapped'),
onLongPress: () => debugPrint('Long press'),
child: const Padding(padding: EdgeInsets.all(16), child: Text('Tap me')),
)Test yourself
How do you disable an ElevatedButton?
- enabled: false
- onPressed: null
- disabled: true
- Wrap it in IgnorePointer only
Which button has the highest emphasis for a screen's main action in Material 3?
- TextButton
- OutlinedButton
- FilledButton
- IconButton
Which helper customises the colours and shape of a single ElevatedButton?
- ElevatedButton.styleFrom(...)
- ButtonStyle.copy(...)
- ThemeData.button(...)
- ElevatedButton.icon(...)