Sub topic 1
The Material button widgets: Elevated, Filled, Outlined and Text
Prerequisites
You should know Dart functions and closures, how to write a StatefulWidget with setState, and how to use layout widgets such as Column and Padding. This chapter does not cover gestures. Those come later in the tutorial.
The four button widgets
Flutter's Material library provides four button widgets. Each one signals a different level of emphasis.
| Widget | Appearance | Typical use |
|---|---|---|
| `ElevatedButton` | Raised with a shadow | Secondary action on a busy screen |
| `FilledButton` | Solid color, no shadow | Primary action, such as "Save" |
| `OutlinedButton` | Border, no fill | Secondary action with less weight |
| `TextButton` | Label only | Low-emphasis action, such as "Cancel" |
All four share the same constructor parameters: onPressed, style and child.
Handling presses and disabling buttons
onPressed takes a callback that runs when the user taps the button. If you pass null, Flutter disables the button and draws it in the disabled style. The following widget enables its button only after the user checks a box.
import 'package:flutter/material.dart';
class SubmitForm extends StatefulWidget {
const SubmitForm({super.key});
@override
State<SubmitForm> createState() => _SubmitFormState();
}
class _SubmitFormState extends State<SubmitForm> {
bool _agreed = false;
@override
Widget build(BuildContext context) {
return Column(
children: [
CheckboxListTile(
value: _agreed,
onChanged: (v) => setState(() => _agreed = v ?? false),
title: const Text('I agree'),
),
ElevatedButton(
onPressed: _agreed ? () {} : null,
child: const Text('Submit'),
),
],
);
}
}Styling buttons with styleFrom and ButtonStyle
Each button accepts a style parameter of type ButtonStyle. The static styleFrom helper builds one from common properties, so you avoid writing a full ButtonStyle by hand.
FilledButton(
style: FilledButton.styleFrom(
backgroundColor: Colors.teal,
foregroundColor: Colors.white,
padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 14),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
),
onPressed: () {},
child: const Text('Save'),
)Use ButtonStyle directly when you need values that change with the button's state. That is covered in the next section.
Styling by state with WidgetStateProperty
Many style properties accept a WidgetStateProperty, which returns a different value for each combination of states. The states include pressed, hovered, focused and disabled. WidgetStateProperty.resolveWith receives the current set of states and returns the value to use.
FilledButton(
style: ButtonStyle(
backgroundColor: WidgetStateProperty.resolveWith<Color?>((states) {
if (states.contains(WidgetState.disabled)) return Colors.grey;
if (states.contains(WidgetState.pressed)) return Colors.teal.shade700;
return Colors.teal;
}),
),
onPressed: () {},
child: const Text('Continue'),
)Exercise
A TextButton should be disabled while a network request is running. Which property do you change, and what value do you pass?
Show answer
Set onPressed to null while the request runs, and restore the callback when it finishes. Use setState to change a flag that controls the callback.