-
-
Notifications
You must be signed in to change notification settings - Fork 909
/
todo.dart
76 lines (67 loc) · 1.64 KB
/
todo.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import 'package:flutter/foundation.dart' show immutable;
import 'package:riverpod/riverpod.dart';
import 'package:uuid/uuid.dart';
const _uuid = Uuid();
/// A read-only description of a todo-item
@immutable
class Todo {
const Todo({
required this.description,
required this.id,
this.completed = false,
});
final String id;
final String description;
final bool completed;
@override
String toString() {
return 'Todo(description: $description, completed: $completed)';
}
}
/// An object that controls a list of [Todo].
class TodoList extends Notifier<List<Todo>> {
@override
List<Todo> build() => [
const Todo(id: 'todo-0', description: 'Buy cookies'),
const Todo(id: 'todo-1', description: 'Star Riverpod'),
const Todo(id: 'todo-2', description: 'Have a walk'),
];
void add(String description) {
state = [
...state,
Todo(
id: _uuid.v4(),
description: description,
),
];
}
void toggle(String id) {
state = [
for (final todo in state)
if (todo.id == id)
Todo(
id: todo.id,
completed: !todo.completed,
description: todo.description,
)
else
todo,
];
}
void edit({required String id, required String description}) {
state = [
for (final todo in state)
if (todo.id == id)
Todo(
id: todo.id,
completed: todo.completed,
description: description,
)
else
todo,
];
}
void remove(Todo target) {
state = state.where((todo) => todo.id != target.id).toList();
}
}