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.
- project requirements
- components, state, and services
- tested UI
Explain the widget, state, and lifecycle behavior of Mini Project: Habit Tracker
Core syntax or pattern
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
- project requirements
class 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.
- components, state, and services
TextField(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.
- 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.
- project requirements
- components, state, and services
- 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.
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),
),
);
},
),
),
],
),
),
);
}
}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
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
Quick quiz 2 / 3
Quick quiz 3 / 3
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