Thuta Learning
Flutter
IntermediateMobile DevelopmentBeginner

Buttons

FlutterLesson 1220 min

What you'll walk away with

  • Explain the widget, state, and lifecycle behavior of Buttons
  • Test responsive, failure, and interaction cases
  • Write accessible, testable, and performant Flutter

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.

Mental model
  1. input and state
  2. widget build and layout
  3. rendered Flutter UI

Explain the widget, state, and lifecycle behavior of Buttons

Core syntax or pattern

dart
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

  1. input and stateColumn(

    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.

  2. 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.

  3. 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.

Execution flow
  1. input and state
  2. widget build and layout
  3. 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.

dart
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'),
    ),
  ],
)
You should see
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
dart
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

What best describes the core idea of Buttons?

Quick quiz 2 / 3

Which mistake should you avoid first?

Quick quiz 3 / 3

How should this be applied in a real project?

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

Easy traps

  • Setting onPressed: null disables the button. If you want to attach a function, use the form onPressed: () { ... }.
  • Assuming one screen's sample output proves every rebuild, overflow, accessibility, async, and lifecycle path.
  • Doing expensive work in build(), leaking controllers or subscriptions, or calling setState over an unnecessarily large subtree.

Hands-on Exercise

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.

You'll know it worked when: Two buttons, Save and Cancel, appear, and tapping each one prints the corresponding message to the debug console.

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Buttons | Thuta Learning