Thuta Learning
Flutter
ExercisesMobile DevelopmentBeginner

Exercise: Widget, Layout & State Warm-up

FlutterLesson 3040 min

What you'll walk away with

  • Explain the widget, state, and lifecycle behavior of Exercise: Widget, Layout & State Warm-up
  • Test responsive, failure, and interaction cases
  • Write accessible, testable, and performant Flutter

Explain it simply

Exercise: Widget, Layout & State Warm-up, in plain language

This lesson doesn't teach anything new. It's a chance to combine the Column/Row layout, ElevatedButton, GestureDetector, TextField, StatefulWidget, and setState() from the Basic and Intermediate chapters and practice them on your own. Each task has you actually write out a widget tree and watch state changes show up on the UI in real time. Just copying code won't cut it here — you need to think through the logic yourself and fill it in. So resist the urge to peek at the answer right away and give it a real try first.

Problem → Why → Solution

The problem — why this concept matters

Forgetting to call setState() and mutating the state variable directly, so the UI never updates

A common mistake

Not giving children a width/height limit inside a Row/Column, and skipping Expanded/Flexible, which triggers a RenderFlex overflow error

Core concept — understand how it works

Build Exercise: Widget, Layout & State Warm-up in feature slices and verify it with unit, widget, and integration tests. Use measurable acceptance criteria for build modes, frame time, rebuild count, accessibility, and deployment configuration.

Mental model
  1. requirement
  2. reason and implement
  3. test and explain

Explain the widget, state, and lifecycle behavior of Exercise: Widget, Layout & State Warm-up

Core syntax or pattern

dart
// Task 1 skeleton - Counter with limit
class CounterBox extends StatefulWidget {
  const CounterBox({super.key});
  @override
  State<CounterBox> createState() => _CounterBoxState();
}

class _CounterBoxState extends State<CounterBox> {
  int count = 0;

  void increment() {
    setState(() {
      count++;
    });
  }

  void decrement() {
    setState(() {
      if (count > 0) count--;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Text('$count', style: const TextStyle(fontSize: 32)),
        Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ElevatedButton(onPressed: decrement, child: const Text('-')),
            const SizedBox(width: 12),
            ElevatedButton(onPressed: increment, child: const Text('+')),
          ],
        ),
      ],
    );
  }
}

// Task 3 hint - color cycle
final colors = [Colors.blue, Colors.green, Colors.orange, Colors.purple];
int colorIndex = 0;
// GestureDetector onTap: setState(() { colorIndex = (colorIndex + 1) % colors.length; });

Build Exercise: Widget, Layout & State Warm-up in feature slices and verify it with unit, widget, and integration tests. Use measurable acceptance criteria for build modes, frame time, rebuild count, accessibility, and deployment configuration.

Read the code step by step

  1. requirement// Task 1 skeleton - Counter with limit

    This lesson doesn't teach anything new. It's a chance to combine the Column/Row layout, ElevatedButton, GestureDetector, TextField, StatefulWidget, and setState() from the Basic and Intermediate chapters and practice them on your own. Each task has you actually write out a widget tree and watch state changes show up on the UI in real time. Just copying code won't cut it here — you need to think through the logic yourself and fill it in. So resist the urge to peek at the answer right away and give it a real try first.

  2. reason and implementreturn Column(

    Build Exercise: Widget, Layout & State Warm-up in feature slices and verify it with unit, widget, and integration tests. Use measurable acceptance criteria for build modes, frame time, rebuild count, accessibility, and deployment configuration.

  3. test and explain// GestureDetector onTap: setState(() { colorIndex = ...

    Run and test an original Exercise: Widget, Layout & State Warm-up 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. requirement
  2. reason and implement
  3. test and explain

Success

Explain the widget, state, and lifecycle behavior of Exercise: Widget, Layout & State Warm-up

Failure

Forgetting to call setState() and mutating the state variable directly, so the UI never updates

Quick, think about this for a second

This lesson doesn't teach anything new. It's a chance to combine the Column/Row layout, ElevatedButton, GestureDetector, TextField, StatefulWidget, and setState() from the Basic and Intermediate chapters and practice them on your own. Each task has you actually write out a widget tree and watch state changes show up on the UI in real time. Just copying code won't cut it here — you need to think through the logic yourself and fill it in. So resist the urge to peek at the answer right away and give it a real try first.

Exercises

Task 1: Build a counter app with StatefulWidget, adding both an increment button and a decrement button (make sure the count never goes below 0). Task 2: Combine Row and Column to lay out a profile card (an avatar circle, a name Text, and a description Text), using CrossAxisAlignment and MainAxisAlignment to keep everything aligned. Task 3: Use GestureDetector so that tapping a Container box cycles its background color through a color list. Task 4: Pair a TextField with a 'Show' button so that pressing the button displays whatever text the user typed into a Text widget below, using setState().

Code Example

dart
// Task 1 skeleton - Counter with limit
class CounterBox extends StatefulWidget {
  const CounterBox({super.key});
  @override
  State<CounterBox> createState() => _CounterBoxState();
}

class _CounterBoxState extends State<CounterBox> {
  int count = 0;

  void increment() {
    setState(() {
      count++;
    });
  }

  void decrement() {
    setState(() {
      if (count > 0) count--;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Text('$count', style: const TextStyle(fontSize: 32)),
        Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ElevatedButton(onPressed: decrement, child: const Text('-')),
            const SizedBox(width: 12),
            ElevatedButton(onPressed: increment, child: const Text('+')),
          ],
        ),
      ],
    );
  }
}

// Task 3 hint - color cycle
final colors = [Colors.blue, Colors.green, Colors.orange, Colors.purple];
int colorIndex = 0;
// GestureDetector onTap: setState(() { colorIndex = (colorIndex + 1) % colors.length; });
You should see
The counter widget works with two +/- buttons and never drops below 0, and the profile card, color-cycle box, and TextField+Show button tasks all update the UI correctly every time setState() is called.

5-Minute Challenge

Once you've finished Task 4, add one more condition: if the TextField is empty when 'Show' is pressed, display a warning Text that says 'Please enter something' (5 minutes).

A Quick Word of Caution

Before checking the solutions for any task, read the error message yourself and try to debug it. Learning to read errors is one of the most important skills in learning Flutter.

Real-world usage

How to apply it in a real project

Run and test an original Exercise: Widget, Layout & State Warm-up 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 Exercise: Widget, Layout & State Warm-up 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
// Task 1 skeleton - Counter with limit
class CounterBox extends StatefulWidget {
  const CounterBox({super.key});
  @override
  State<CounterBox> createState() => _CounterBoxState();
}

class _CounterBoxState extends State<CounterBox> {
  int count = 0;

  void increment() {
    setState(() {
      count++;
    });
  }

  void decrement() {
    setState(() {
      if (count > 0) count--;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Text('$count', style: const TextStyle(fontSize: 32)),
        Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ElevatedButton(onPressed: decrement, child: const Text('-')),
            const SizedBox(width: 12),
            ElevatedButton(onPressed: increment, child: const Text('+')),
          ],
        ),
      ],
    );
  }
}

// Task 3 hint - color cycle
final colors = [Colors.blue, Colors.green, Colors.orange, Colors.purple];
int colorIndex = 0;
// GestureDetector onTap: setState(() { colorIndex = (colorIndex + 1) % colors.length; });

Expected: Explain the widget, state, and lifecycle behavior of Exercise: Widget, Layout & State Warm-up

Show hint

Run and test an original Exercise: Widget, Layout & State Warm-up 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 Exercise: Widget, Layout & State Warm-up?

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

  • requirement → reason and implement → test and explain
  • Build Exercise: Widget, Layout & State Warm-up in feature slices and verify it with unit, widget, and integration tests. Use measurable acceptance criteria for build modes, frame time, rebuild count, accessibility, and deployment configuration.
  • Forgetting to call setState() and mutating the state variable directly, so the UI never updates
  • Run and test an original Exercise: Widget, Layout & State Warm-up 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

  • Forgetting to call setState() and mutating the state variable directly, so the UI never updates
  • Not giving children a width/height limit inside a Row/Column, and skipping Expanded/Flexible, which triggers a RenderFlex overflow error
  • Assuming one screen's sample output proves every rebuild, overflow, accessibility, async, and lifecycle path.

Hands-on Exercise

Run and test an original Exercise: Widget, Layout & State Warm-up 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: The counter widget works with two +/- buttons and never drops below 0, and the profile card, color-cycle box, and TextField+Show button tasks all update the UI correctly every time setState() is called.

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.

Exercise: Widget, Layout & State Warm-up | Thuta Learning