Flutter Mini Project: Todo App
In this project you will build a complete, small todo app step by step, using what you learned in this course: a Dart model class, a StatefulWidget with setState, a TextField, a ListView.builder, CheckboxListTile, swipe-to-delete and a SnackBar with undo. At the end you'll have about 150 lines of code you fully understand.
The finished app has an input field with an Add button, a list of todos you can tick off, a "done" counter in the app bar, and swipe-to-delete with UNDO — you'll see it in step 5.
Step 1 — Create the project
flutter create todo_app
cd todo_appCreating project todo_app...
Resolving dependencies in `todo_app`...
Got dependencies in `todo_app`.
Wrote 129 files.
All done!
You can find general documentation for Flutter at: https://docs.flutter.dev/
Open lib/main.dart, delete everything, and follow along.
Step 2 — The model (pure Dart)
Keep data and logic separate from widgets. A Todo is immutable; changing it means creating a modified copy with copyWith. This part is plain Dart, so you can run and test it right here:
class Todo {
const Todo({required this.id, required this.title, this.done = false});
final int id;
final String title;
final bool done;
Todo copyWith({String? title, bool? done}) =>
Todo(id: id, title: title ?? this.title, done: done ?? this.done);
@override
String toString() => '${done ? '[x]' : '[ ]'} $title';
}
void main() {
var todos = <Todo>[
const Todo(id: 1, title: 'Learn Dart', done: true),
const Todo(id: 2, title: 'Build the todo app'),
];
// Add a todo
todos = [...todos, const Todo(id: 3, title: 'Publish it')];
// Toggle todo #2
todos = [
for (final t in todos) t.id == 2 ? t.copyWith(done: !t.done) : t,
];
// Remove todo #1
todos = todos.where((t) => t.id != 1).toList();
todos.forEach(print);
final doneCount = todos.where((t) => t.done).length;
print('$doneCount / ${todos.length} done');
}[x] Build the todo app
[ ] Publish it
1 / 2 done
Tip: Creating new lists instead of mutating the old one (
[...todos, newTodo]) makes state changes easy to follow and plays nicely withsetStateand state-management packages.
Step 3 — App shell
main starts a MaterialApp with a Material 3 theme. The home screen is a StatefulWidget because the list changes.
import 'package:flutter/material.dart';
void main() => runApp(const TodoApp());
class TodoApp extends StatelessWidget {
const TodoApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Todos',
debugShowCheckedModeBanner: false,
theme: ThemeData(colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple)),
home: const TodoScreen(),
);
}
}
class TodoScreen extends StatefulWidget {
const TodoScreen({super.key});
@override
State<TodoScreen> createState() => _TodoScreenState();
}Step 4 — State and actions
The State holds the list, a TextEditingController for the input, and a counter for ids. Each action wraps its change in setState.
class _TodoScreenState extends State<TodoScreen> {
final _controller = TextEditingController();
List<Todo> _todos = [];
int _nextId = 1;
@override
void dispose() {
_controller.dispose();
super.dispose();
}
void _add() {
final title = _controller.text.trim();
if (title.isEmpty) return;
setState(() {
_todos = [..._todos, Todo(id: _nextId++, title: title)];
});
_controller.clear();
debugPrint('Added "$title"');
}
void _toggle(Todo todo) {
setState(() {
_todos = [for (final t in _todos) t.id == todo.id ? t.copyWith(done: !t.done) : t];
});
}
void _remove(Todo todo) {
final index = _todos.indexOf(todo);
setState(() => _todos = [..._todos]..removeAt(index));
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text('Deleted "${todo.title}"'),
action: SnackBarAction(
label: 'UNDO',
onPressed: () => setState(() => _todos = [..._todos]..insert(index, todo)),
),
));
}
// build() comes in step 5
}Added "Learn Dart"
Added "Build the todo app"
Added "Publish it"
Step 5 — The UI
The build method combines an input row with the list. Empty state, done counter, strike-through text and swipe-to-delete with Dismissible make it feel like a real app.
@override
Widget build(BuildContext context) {
final doneCount = _todos.where((t) => t.done).length;
return Scaffold(
appBar: AppBar(
title: const Text('My Todos'),
backgroundColor: Theme.of(context).colorScheme.primaryContainer,
actions: [
Center(child: Text('$doneCount / ${_todos.length} done')),
const SizedBox(width: 16),
],
),
body: Column(
children: [
Padding(
padding: const EdgeInsets.all(12),
child: Row(
children: [
Expanded(
child: TextField(
controller: _controller,
decoration: const InputDecoration(
hintText: 'What needs doing?',
border: OutlineInputBorder(),
),
onSubmitted: (_) => _add(),
),
),
const SizedBox(width: 8),
FilledButton(onPressed: _add, child: const Text('Add')),
],
),
),
Expanded(
child: _todos.isEmpty
? const Center(child: Text('Nothing to do yet 🎉'))
: ListView.builder(
itemCount: _todos.length,
itemBuilder: (context, i) {
final todo = _todos[i];
return Dismissible(
key: ValueKey(todo.id),
background: Container(color: Colors.red.shade100),
onDismissed: (_) => _remove(todo),
child: CheckboxListTile(
controlAffinity: ListTileControlAffinity.leading,
value: todo.done,
onChanged: (_) => _toggle(todo),
title: Text(
todo.title,
style: TextStyle(
decoration: todo.done ? TextDecoration.lineThrough : null,
color: todo.done ? Colors.grey : null,
),
),
),
);
},
),
),
],
),
);
}Dismissible requires a unique, stable key — that's why each todo has an id. Using the list index as a key would break as soon as items move.
Step 6 — Test it
Add test/todo_test.dart to make sure adding and toggling work:
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:todo_app/main.dart';
void main() {
testWidgets('adds and completes a todo', (tester) async {
await tester.pumpWidget(const TodoApp());
expect(find.text('Nothing to do yet 🎉'), findsOneWidget);
await tester.enterText(find.byType(TextField), 'Water plants');
await tester.tap(find.text('Add'));
await tester.pump();
expect(find.text('Water plants'), findsOneWidget);
expect(find.text('0 / 1 done'), findsOneWidget);
await tester.tap(find.byType(Checkbox));
await tester.pump();
expect(find.text('1 / 1 done'), findsOneWidget);
});
}00:03 +1: All tests passed!
Ideas to extend it
| Feature | What you'll use |
|---|---|
| Save todos between launches | shared_preferences + jsonEncode/jsonDecode |
| Edit a todo | showDialog with a TextField, then copyWith(title: …) |
| Filter All / Active / Done | SegmentedButton + an enum + where |
| Share state across screens | Move the list into a ChangeNotifier with provider |
| Due dates | showDatePicker and a DateTime? field |
Note: This project deliberately keeps state in one
StatefulWidget. When you add a second screen, that's the moment to move_todosinto aChangeNotifieras shown in the State Management lesson.
Test yourself
Why does each Todo have an id?
- Dart requires it
- To give
Dismissiblea unique, stable key and to find items when toggling - To sort alphabetically
- To make the class const
What does copyWith(done: true) return?
- The same object, modified
- A new Todo with
donechanged and other fields copied - A boolean
- null
Where is the TextEditingController disposed?
- In build()
- In the State's dispose()
- In main()
- It is never disposed