Sub topic 4
Styling Buttons by State with `WidgetStateProperty`
Assume you know basic Dart, widget trees, StatefulWidget, setState, and Flutter layout widgets. This chapter focuses on making a button change appearance as the user interacts with it.
A button can have several interaction states:
| State | Meaning |
|---|---|
| enabled | The button can be activated |
| disabled | Its `onPressed` callback is `null` |
| pressed | A pointer or keyboard is activating it |
| hovered | A mouse pointer is over it |
| focused | Keyboard or accessibility focus is on it |
A fixed color works for simple designs, but interactive controls usually need different colors for these states. WidgetStateProperty is a value that Flutter resolves using the widget's current states.
Use `resolveWith` for state-dependent values
Pass a WidgetStateProperty to a ButtonStyle. Its callback receives a set of WidgetState values and returns the value for that combination.
import 'package:flutter/material.dart';
class ContinueButton extends StatelessWidget {
const ContinueButton({super.key});
@override
Widget build(BuildContext context) {
return FilledButton(
style: ButtonStyle(
backgroundColor:
WidgetStateProperty.resolveWith<Color?>((states) {
if (states.contains(WidgetState.disabled)) {
return Colors.grey.shade300;
}
if (states.contains(WidgetState.pressed)) {
return Colors.teal.shade800;
}
if (states.contains(WidgetState.hovered)) {
return Colors.teal.shade600;
}
if (states.contains(WidgetState.focused)) {
return Colors.teal.shade700;
}
return Colors.teal;
}),
foregroundColor:
WidgetStateProperty.resolveWith<Color?>((states) {
return states.contains(WidgetState.disabled)
? Colors.grey.shade600
: Colors.white;
}),
),
onPressed: () {},
child: const Text('Continue'),
);
}
}The callback checks states in a deliberate order. A button can be both hovered and pressed, so the pressed appearance should usually take priority. The final return is the normal enabled appearance.
Returning null tells the button to use its default theme value for that state. This is useful when you want to customize only one condition.
FilledButton(
style: ButtonStyle(
backgroundColor:
WidgetStateProperty.resolveWith<Color?>((states) {
if (states.contains(WidgetState.pressed)) {
return Colors.indigo.shade800;
}
return null;
}),
),
onPressed: () {},
child: const Text('Open'),
)Use constant values when a property does not vary
WidgetStatePropertyAll supplies one value for every state:
FilledButton(
style: const ButtonStyle(
minimumSize: WidgetStatePropertyAll(Size(160, 48)),
),
onPressed: () {},
child: const Text('Save'),
)Use styleFrom for straightforward, fixed styling. Use ButtonStyle with resolveWith when the appearance depends on interaction.
Common errors
- Checking only
pressedand forgetting the disabled state. - Returning a non-null color for disabled buttons while expecting Flutter's disabled theme colors.
- Using a stateful callback that changes external data. A resolver should calculate a value from
states, not callsetState. - Applying a style to a different button type than the one being rendered.
FilledButton,OutlinedButton, andTextButtoneach have their own defaults.
Exercise
Which state should you check before the default case when a button must remain visibly inactive?
Show answer
Check WidgetState.disabled, and ensure the button receives onPressed: null.