Explain it simply
Buttons, in plain language
A button is the doorway that lets user actions into your app. Flutter offers several button types: ElevatedButton, TextButton, OutlinedButton, and IconButton. The most important part of making a button work is its onPressed callback. Without onPressed, the button becomes disabled.
Problem → Why → Solution
The problem — why this concept matters
Setting onPressed: null disables the button. If you want to attach a function, use the form onPressed: () { ... }.
A common mistake
Assuming one screen's sample output proves every rebuild, overflow, accessibility, async, and lifecycle path.
Core concept — understand how it works
For Buttons, define the source of truth, validation, focus, controller disposal, and minimal rebuild boundary. Keep ephemeral state local and place shared app state in a testable controller or model.
- input and state
- widget build and layout
- rendered Flutter UI
Explain the widget, state, and lifecycle behavior of Buttons
Core syntax or pattern
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () {
print('Primary action clicked');
},
child: const Text('Save'),
),
TextButton(
onPressed: () {
print('Secondary action clicked');
},
child: const Text('Cancel'),
),
],
)For Buttons, define the source of truth, validation, focus, controller disposal, and minimal rebuild boundary. Keep ephemeral state local and place shared app state in a testable controller or model.
Read the code step by step
- input and state
Column(A button is the doorway that lets user actions into your app. Flutter offers several button types: ElevatedButton, TextButton, OutlinedButton, and IconButton. The most important part of making a button work is its onPressed callback. Without onPressed, the button becomes disabled.
- widget build and layout
),For Buttons, define the source of truth, validation, focus, controller disposal, and minimal rebuild boundary. Keep ephemeral state local and place shared app state in a testable controller or model.
- rendered Flutter UI
)Run and test an original Buttons example with small and large screens, text scaling, empty/loading/error states, rapid interaction, navigation restoration, and disposal cases. Use flutter analyze, widget tests, and profile mode where relevant.
- input and state
- widget build and layout
- rendered Flutter UI
Success
Explain the widget, state, and lifecycle behavior of Buttons
Failure
Setting onPressed: null disables the button. If you want to attach a function, use the form onPressed: () { ... }.
A button is the doorway that lets user actions into your app. Flutter offers several button types: ElevatedButton, TextButton, OutlinedButton, and IconButton. The most important part of making a button work is its onPressed callback. Without onPressed, the button becomes disabled.
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () {
print('Primary action clicked');
},
child: const Text('Save'),
),
TextButton(
onPressed: () {
print('Secondary action clicked');
},
child: const Text('Cancel'),
),
],
)Two buttons, Save and Cancel, appear, and tapping each one prints the corresponding message to the debug console.What's Next
Next, learn about GestureDetector, which lets you make even non-button widgets tappable.
Real-world usage
How to apply it in a real project
Run and test an original Buttons example with small and large screens, text scaling, empty/loading/error states, rapid interaction, navigation restoration, and disposal cases. Use flutter analyze, widget tests, and profile mode where relevant.
Mini challenge
Build it yourself
Run and test an original Buttons example with small and large screens, text scaling, empty/loading/error states, rapid interaction, navigation restoration, and disposal cases. Use flutter analyze, widget tests, and profile mode where relevant.
- Rebuild the example without copying it
- Test a normal input and an edge case
- Predict the compiler or output result before running it
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () {
print('Primary action clicked');
},
child: const Text('Save'),
),
TextButton(
onPressed: () {
print('Secondary action clicked');
},
child: const Text('Cancel'),
),
],
)Expected: Explain the widget, state, and lifecycle behavior of Buttons
Show hint
Run and test an original Buttons example with small and large screens, text scaling, empty/loading/error states, rapid interaction, navigation restoration, and disposal cases. Use flutter analyze, widget tests, and profile mode where relevant.
Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- input and state → widget build and layout → rendered Flutter UI
- For Buttons, define the source of truth, validation, focus, controller disposal, and minimal rebuild boundary. Keep ephemeral state local and place shared app state in a testable controller or model.
- Setting onPressed: null disables the button. If you want to attach a function, use the form onPressed: () { ... }.
- Run and test an original Buttons example with small and large screens, text scaling, empty/loading/error states, rapid interaction, navigation restoration, and disposal cases. Use flutter analyze, widget tests, and profile mode where relevant.
Flutter documentation — Flutter