Thuta Learning
Flutter
ProjectsMobile DevelopmentBeginner

Mini Project: Habit Tracker

FlutterLesson 2930 min

What you'll walk away with

  • Explain the widget, state, and lifecycle behavior of Mini Project: Habit Tracker
  • Test responsive, failure, and interaction cases
  • Write accessible, testable, and performant Flutter

Explain it simply

Mini Project: Habit Tracker, in plain language

In this mini project, we'll build a simple Habit Tracker UI. Users can enter a habit name and tap Add, see habits listed, and tap one to increase its completed count. This project lets you practice TextField, Button, List, setState, and basic layout all in one place.

Problem → Why → Solution

The problem — why this concept matters

Putting a ListView inside a Column without Expanded can cause a height constraint error. Consider using Expanded/Flexible for scrollable widgets inside a Column.

A common mistake

Assuming one screen's sample output proves every rebuild, overflow, accessibility, async, and lifecycle path.

Core concept — understand how it works

Build Mini Project: Habit Tracker 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. project requirements
  2. components, state, and services
  3. tested UI

Explain the widget, state, and lifecycle behavior of Mini Project: Habit Tracker

Core syntax or pattern

dart
class HabitTracker extends StatefulWidget {
  const HabitTracker({super.key});

  @override
  State<HabitTracker> createState() => _HabitTrackerState();
}

class _HabitTrackerState extends State<HabitTracker> {
  final controller = TextEditingController();
  final List<Map<String, dynamic>> habits = [];

  void addHabit() {
    final name = controller.text.trim();

    if (name.isEmpty) return;

    setState(() {
      habits.add({'name': name, 'done': 0});
      controller.clear();
    });
  }

  void completeHabit(int index) {
    setState(() {
      habits[index]['done']++;
    });
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Habit Tracker')),
      body: Padding(
        padding: const EdgeInsets.all(16),
        child: Column(
          children: [
            TextField(
              controller: controller,
              decoration: const InputDecoration(
                labelText: 'New habit',
                border: OutlineInputBorder(),
              ),
            ),
            const SizedBox(height: 12),
            ElevatedButton(
              onPressed: addHabit,
              child: const Text('Add Habit'),
            ),
            const SizedBox(height: 20),
            Expanded(
              child: ListView.builder(
                itemCount: habits.length,
                itemBuilder: (context, index) {
                  final habit = habits[index];

                  return Card(
                    child: ListTile(
                      title: Text(habit['name']),
                      subtitle: Text("Completed: ${habit['done']} times"),
                      trailing: const Icon(Icons.check_circle_outline),
                      onTap: () => completeHabit(index),
                    ),
                  );
                },
              ),
            ),
          ],
        ),
      ),
    );
  }
}

Build Mini Project: Habit Tracker 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. project requirementsclass HabitTracker extends StatefulWidget {

    In this mini project, we'll build a simple Habit Tracker UI. Users can enter a habit name and tap Add, see habits listed, and tap one to increase its completed count. This project lets you practice TextField, Button, List, setState, and basic layout all in one place.

  2. components, state, and servicesTextField(

    Build Mini Project: Habit Tracker 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. tested UI}

    Run and test an original Mini Project: Habit Tracker 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. project requirements
  2. components, state, and services
  3. tested UI

Success

Explain the widget, state, and lifecycle behavior of Mini Project: Habit Tracker

Failure

Putting a ListView inside a Column without Expanded can cause a height constraint error. Consider using Expanded/Flexible for scrollable widgets inside a Column.

In this mini project, we'll build a simple Habit Tracker UI. Users can enter a habit name and tap Add, see habits listed, and tap one to increase its completed count. This project lets you practice TextField, Button, List, setState, and basic layout all in one place.

dart
class HabitTracker extends StatefulWidget {
  const HabitTracker({super.key});

  @override
  State<HabitTracker> createState() => _HabitTrackerState();
}

class _HabitTrackerState extends State<HabitTracker> {
  final controller = TextEditingController();
  final List<Map<String, dynamic>> habits = [];

  void addHabit() {
    final name = controller.text.trim();

    if (name.isEmpty) return;

    setState(() {
      habits.add({'name': name, 'done': 0});
      controller.clear();
    });
  }

  void completeHabit(int index) {
    setState(() {
      habits[index]['done']++;
    });
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Habit Tracker')),
      body: Padding(
        padding: const EdgeInsets.all(16),
        child: Column(
          children: [
            TextField(
              controller: controller,
              decoration: const InputDecoration(
                labelText: 'New habit',
                border: OutlineInputBorder(),
              ),
            ),
            const SizedBox(height: 12),
            ElevatedButton(
              onPressed: addHabit,
              child: const Text('Add Habit'),
            ),
            const SizedBox(height: 20),
            Expanded(
              child: ListView.builder(
                itemCount: habits.length,
                itemBuilder: (context, index) {
                  final habit = habits[index];

                  return Card(
                    child: ListTile(
                      title: Text(habit['name']),
                      subtitle: Text("Completed: ${habit['done']} times"),
                      trailing: const Icon(Icons.check_circle_outline),
                      onTap: () => completeHabit(index),
                    ),
                  );
                },
              ),
            ),
          ],
        ),
      ),
    );
  }
}
You should see
The Habit Tracker screen appears, letting you add new habits. Every tap on a habit card increases its Completed count.

Next Steps

You can keep expanding this project by adding a delete button, a completed filter, a local database, notification reminders, or a dark mode toggle.

Real-world usage

How to apply it in a real project

Run and test an original Mini Project: Habit Tracker 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 Mini Project: Habit Tracker 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
class HabitTracker extends StatefulWidget {
  const HabitTracker({super.key});

  @override
  State<HabitTracker> createState() => _HabitTrackerState();
}

class _HabitTrackerState extends State<HabitTracker> {
  final controller = TextEditingController();
  final List<Map<String, dynamic>> habits = [];

  void addHabit() {
    final name = controller.text.trim();

    if (name.isEmpty) return;

    setState(() {
      habits.add({'name': name, 'done': 0});
      controller.clear();
    });
  }

  void completeHabit(int index) {
    setState(() {
      habits[index]['done']++;
    });
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Habit Tracker')),
      body: Padding(
        padding: const EdgeInsets.all(16),
        child: Column(
          children: [
            TextField(
              controller: controller,
              decoration: const InputDecoration(
                labelText: 'New habit',
                border: OutlineInputBorder(),
              ),
            ),
            const SizedBox(height: 12),
            ElevatedButton(
              onPressed: addHabit,
              child: const Text('Add Habit'),
            ),
            const SizedBox(height: 20),
            Expanded(
              child: ListView.builder(
                itemCount: habits.length,
                itemBuilder: (context, index) {
                  final habit = habits[index];

                  return Card(
                    child: ListTile(
                      title: Text(habit['name']),
                      subtitle: Text("Completed: ${habit['done']} times"),
                      trailing: const Icon(Icons.check_circle_outline),
                      onTap: () => completeHabit(index),
                    ),
                  );
                },
              ),
            ),
          ],
        ),
      ),
    );
  }
}

Expected: Explain the widget, state, and lifecycle behavior of Mini Project: Habit Tracker

Show hint

Run and test an original Mini Project: Habit Tracker 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 Mini Project: Habit Tracker?

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

  • project requirements → components, state, and services → tested UI
  • Build Mini Project: Habit Tracker 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.
  • Putting a ListView inside a Column without Expanded can cause a height constraint error. Consider using Expanded/Flexible for scrollable widgets inside a Column.
  • Run and test an original Mini Project: Habit Tracker 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

  • Putting a ListView inside a Column without Expanded can cause a height constraint error. Consider using Expanded/Flexible for scrollable widgets inside a Column.
  • 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 Mini Project: Habit Tracker 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 Habit Tracker screen appears, letting you add new habits. Every tap on a habit card increases its Completed count.

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.

Mini Project: Habit Tracker | Thuta Learning