Sub topic 3
Styling Buttons with `styleFrom` and `ButtonStyle`
This chapter assumes that you can write basic Dart functions, build StatelessWidget and StatefulWidget trees, use setState, and arrange widgets with Row, Column, Stack, and Padding.
Flutter Material buttons use a ButtonStyle object to control their appearance. A style can define the background color, text color, padding, shape, minimum size, and other visual properties. The best way to choose a styling approach depends on how much control you need.
| Approach | Best for | Main benefit |
|---|---|---|
| `styleFrom` | Common, fixed button styling | Short and readable |
| `ButtonStyle` | Precise or state-dependent styling | Direct control over each property |
Using `styleFrom` for common designs
Each Material button family provides a styleFrom helper. It converts familiar arguments into a ButtonStyle, so you do not need to construct every style property manually.
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: StyleExample()));
}
class StyleExample extends StatelessWidget {
const StyleExample({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Button styles')),
body: Center(
child: 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'),
),
),
);
}
}backgroundColor controls the button surface. foregroundColor controls content such as text and icons. padding adds space inside the button. shape changes the outline and its corner radius.
Keep the button family in the helper name. For example, use OutlinedButton.styleFrom for an OutlinedButton. This makes the code easier to read and lets Flutter apply defaults appropriate to that button family.
Constructing a `ButtonStyle` directly
Use ButtonStyle when you want to set properties individually or reuse a style across several buttons.
final primaryStyle = ButtonStyle(
backgroundColor: WidgetStatePropertyAll<Color>(Colors.indigo),
foregroundColor: WidgetStatePropertyAll<Color>(Colors.white),
padding: WidgetStatePropertyAll<EdgeInsets>(
const EdgeInsets.symmetric(horizontal: 20, vertical: 12),
),
shape: WidgetStatePropertyAll<OutlinedBorder>(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
),
);A WidgetStateProperty can provide a value for different interaction states, such as pressed, hovered, focused, or disabled. WidgetStatePropertyAll supplies the same value in every state. For example, the backgroundColor above remains indigo while the button is enabled and pressed.
A direct style is useful when a design system defines shared values:
FilledButton(
style: primaryStyle,
onPressed: () {},
child: const Text('Continue'),
)You can also provide a state-aware property with WidgetStateProperty.resolveWith. That callback receives the current set of states and returns the value to use. Keep this logic focused on visual state. The button's onPressed callback should still determine whether the action is enabled.
Choosing between the two approaches
Start with styleFrom for a one-off button or a simple fixed design. Choose ButtonStyle when you need state-dependent values, shared style constants, or properties that styleFrom does not expose clearly.
Exercise
Which approach should you choose for a button that needs a teal background, white text, padding, and rounded corners that never change?
Show answer
Use the button family's styleFrom helper, such as FilledButton.styleFrom, because the appearance is fixed and the helper expresses these common properties concisely.