Sub topic 2
Handling Presses and Disabling Buttons
This chapter assumes basic Dart functions and closures, Flutter widget trees, StatefulWidget, setState, and layout widgets such as Row, Column, and Padding.
A button turns a pointer action into an application action. The button calls its onPressed callback when the user activates it. The callback can update state, submit data, navigate, or start another operation.
Choosing the button callback
Material buttons use a nullable callback:
| `onPressed` value | Button behavior |
|---|---|
| A function | The button is enabled and responds to activation. |
| `null` | The button is disabled and uses disabled styling. |
Flutter determines the enabled state from the callback itself. You do not need a separate enabled property.
import 'package:flutter/material.dart';
class CounterButton extends StatefulWidget {
const CounterButton({super.key});
@override
State<CounterButton> createState() => _CounterButtonState();
}
class _CounterButtonState extends State<CounterButton> {
int _count = 0;
@override
Widget build(BuildContext context) {
return FilledButton(
onPressed: () => setState(() => _count++),
child: Text('Pressed $_count times'),
);
}
}The callback is a closure. It captures the _count field and increases it when the button is pressed. Calling setState tells Flutter that the state changed and that the widget should rebuild. The new value then appears in the Text widget.
Disabling a button from state
Usually, the enabled state depends on application data. For example, a form might enable its submit button only when the user has entered valid information.
ElevatedButton(
onPressed: _canSubmit ? _submit : null,
child: const Text('Submit'),
)This conditional expression gives Flutter either the _submit function or null. When _canSubmit changes, call setState so Flutter rebuilds the button.
class SavePanel extends StatefulWidget {
const SavePanel({super.key});
@override
State<SavePanel> createState() => _SavePanelState();
}
class _SavePanelState extends State<SavePanel> {
bool _canSubmit = false;
bool _saving = false;
Future<void> _submit() async {
setState(() => _saving = true);
await Future<void>.delayed(const Duration(seconds: 1));
if (!mounted) return;
setState(() {
_saving = false;
_canSubmit = false;
});
}
@override
Widget build(BuildContext context) {
final canPress = _canSubmit && !_saving;
return FilledButton(
onPressed: canPress ? _submit : null,
child: Text(_saving ? 'Saving...' : 'Save'),
);
}
}Here, _saving prevents a second press while the asynchronous operation is running. The mounted check prevents a later state update if the widget was removed before the delay completed.
A press has a clear state flow
Avoid changing state directly in build. The build method should describe the current UI. Event callbacks such as onPressed are the appropriate place to request changes.
Exercise
How would you disable a button while _loading is true?
Show answer
Pass _loading ? null : _submit to onPressed, and call setState when _loading changes.