Project: To-Do List App
What We Build
A simple, useful to-do app with these features: add tasks, tick them as done, swipe to delete, filter by All, Active and Done, and keep the tasks after the app closes.
Step 1: Create the Project and Add Packages
bash
flutter create todo_app
cd todo_app
flutter pub add provider shared_preferences
Step 2: The Model
dart
// lib/models/todo.dart
class Todo {
final String id;
final String title;
final bool done;
const Todo({required this.id, required this.title, this.done = false});
Todo copyWith({String? title, bool? done}) =>
Todo(id: id, title: title ?? this.title, done: done ?? this.done);
Map<String, dynamic> toJson() => {'id': id, 'title': title, 'done': done};
factory Todo.fromJson(Map<String, dynamic> json) => Todo(
id: json['id'] as String,
title: json['title'] as String,
done: json['done'] as bool? ?? false,
);
}
Step 3: The State (ChangeNotifier)
dart
// lib/state/todo_provider.dart
import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../models/todo.dart';
enum TodoFilter { all, active, done }
class TodoProvider extends ChangeNotifier {
static const _key = 'todos';
final List<Todo> _todos = [];
TodoFilter _filter = TodoFilter.all;
TodoFilter get filter => _filter;
List<Todo> get visible => switch (_filter) {
TodoFilter.all => List.unmodifiable(_todos),
TodoFilter.active => _todos.where((t) => !t.done).toList(),
TodoFilter.done => _todos.where((t) => t.done).toList(),
};
int get remaining => _todos.where((t) => !t.done).length;
Future<void> load() async {
final prefs = await SharedPreferences.getInstance();
final text = prefs.getString(_key);
if (text != null) {
final list = jsonDecode(text) as List<dynamic>;
_todos
..clear()
..addAll(list.map((e) => Todo.fromJson(e as Map<String, dynamic>)));
notifyListeners();
}
}
Future<void> _save() async {
final prefs = await SharedPreferences.getInstance();
await prefs.setString(_key, jsonEncode(_todos.map((t) => t.toJson()).toList()));
}
void add(String title) {
final text = title.trim();
if (text.isEmpty) return;
_todos.insert(0, Todo(id: DateTime.now().microsecondsSinceEpoch.toString(), title: text));
notifyListeners();
_save();
}
void toggle(String id) {
final i = _todos.indexWhere((t) => t.id == id);
if (i == -1) return;
_todos[i] = _todos[i].copyWith(done: !_todos[i].done);
notifyListeners();
_save();
}
void remove(String id) {
_todos.removeWhere((t) => t.id == id);
notifyListeners();
_save();
}
void clearDone() {
_todos.removeWhere((t) => t.done);
notifyListeners();
_save();
}
void setFilter(TodoFilter f) {
_filter = f;
notifyListeners();
}
}
Step 4: main.dart
dart
// lib/main.dart
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'state/todo_provider.dart';
import 'pages/home_page.dart';
void main() {
runApp(
ChangeNotifierProvider(
create: (_) => TodoProvider()..load(),
child: const TodoApp(),
),
);
}
class TodoApp extends StatelessWidget {
const TodoApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'My Tasks',
debugShowCheckedModeBanner: false,
theme: ThemeData(colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple)),
darkTheme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple, brightness: Brightness.dark),
),
home: const HomePage(),
);
}
}
Step 5: The Home Page
dart
// lib/pages/home_page.dart
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../state/todo_provider.dart';
class HomePage extends StatelessWidget {
const HomePage({super.key});
Future<void> _showAddDialog(BuildContext context) async {
final controller = TextEditingController();
final provider = context.read<TodoProvider>();
await showDialog<void>(
context: context,
builder: (ctx) => AlertDialog(
title: const Text('New task'),
content: TextField(
controller: controller,
autofocus: true,
decoration: const InputDecoration(hintText: 'What needs to be done?'),
onSubmitted: (value) {
provider.add(value);
Navigator.pop(ctx);
},
),
actions: [
TextButton(onPressed: () => Navigator.pop(ctx), child: const Text('Cancel')),
FilledButton(
onPressed: () {
provider.add(controller.text);
Navigator.pop(ctx);
},
child: const Text('Add'),
),
],
),
);
controller.dispose();
}
@override
Widget build(BuildContext context) {
final todos = context.watch<TodoProvider>();
final items = todos.visible;
return Scaffold(
appBar: AppBar(
title: Text('My Tasks (${todos.remaining} left)'),
actions: [
IconButton(
tooltip: 'Clear completed',
icon: const Icon(Icons.delete_sweep_outlined),
onPressed: () => context.read<TodoProvider>().clearDone(),
),
],
),
body: Column(
children: [
Padding(
padding: const EdgeInsets.all(12),
child: SegmentedButton<TodoFilter>(
segments: const [
ButtonSegment(value: TodoFilter.all, label: Text('All')),
ButtonSegment(value: TodoFilter.active, label: Text('Active')),
ButtonSegment(value: TodoFilter.done, label: Text('Done')),
],
selected: {todos.filter},
onSelectionChanged: (s) => context.read<TodoProvider>().setFilter(s.first),
),
),
Expanded(
child: items.isEmpty
? const Center(child: Text('No tasks here. Tap + to add one.'))
: ListView.builder(
itemCount: items.length,
itemBuilder: (context, i) {
final todo = items[i];
return Dismissible(
key: ValueKey(todo.id),
direction: DismissDirection.endToStart,
background: Container(
color: Colors.red,
alignment: Alignment.centerRight,
padding: const EdgeInsets.only(right: 20),
child: const Icon(Icons.delete, color: Colors.white),
),
onDismissed: (_) => context.read<TodoProvider>().remove(todo.id),
child: CheckboxListTile(
value: todo.done,
onChanged: (_) => context.read<TodoProvider>().toggle(todo.id),
title: Text(
todo.title,
style: TextStyle(
decoration: todo.done ? TextDecoration.lineThrough : null,
color: todo.done ? Theme.of(context).disabledColor : null,
),
),
),
);
},
),
),
],
),
floatingActionButton: FloatingActionButton(
onPressed: () => _showAddDialog(context),
child: const Icon(Icons.add),
),
);
}
}
What You Practiced
- Model with
copyWithand JSON - ChangeNotifier plus Provider for shared state
shared_preferencesfor savingDismissiblefor swipe to delete (it needs a unique key)SegmentedButtonfor filters (Material 3)
Challenges
- Edit a task by tapping it
- Add a due date with
showDatePicker - Replace shared_preferences with SQLite
- Write unit tests for
TodoProvider
Conclusion
You built a real app with state, saved data and a clean structure. Notice how the widgets only display and call methods, while TodoProvider holds the logic.
Quick Summary: Model, ChangeNotifier provider, shared_preferences saving, Dismissible deletion and SegmentedButton filters.

