/
todo.dart
67 lines (58 loc) · 1.42 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
import 'package:meta/meta.dart';
import 'package:state_notifier/state_notifier.dart';
import 'package:uuid/uuid.dart';
var _uuid = Uuid();
/// A read-only description of a todo-item
class Todo {
Todo({
this.description,
this.completed = false,
String id,
}) : id = id ?? _uuid.v4();
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 StateNotifier<List<Todo>> {
TodoList([List<Todo> initialTodos]) : super(initialTodos ?? []);
void add(String description) {
state = [
...state,
Todo(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();
}
}