-
Notifications
You must be signed in to change notification settings - Fork 2
/
task_list.dart
115 lines (108 loc) · 3.14 KB
/
task_list.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import 'package:taskshare/widgets/widgets.dart';
import '../tasks_bloc.dart';
import '../tasks_bloc_provider.dart';
import 'task_animated_list.dart';
import 'task_list_tile.dart';
class TaskList extends StatefulWidget {
const TaskList();
factory TaskList.forDesignTime() => const TaskList();
@override
TaskListState createState() => TaskListState();
}
class TaskListState extends State<TaskList> {
final _listKey = GlobalKey<AnimatedListState>();
TaskAnimatedList _list;
StreamSubscription _subscription;
@override
void initState() {
super.initState();
final bloc = TasksBlocProvider.of(context);
_list = TaskAnimatedList(
listKey: _listKey,
stream: bloc.tasks,
removedItemBuilder: (task, context, animation) {
return TaskListTile(
key: ValueKey(task.id),
task: task,
animation: animation,
onDismissed: null,
);
},
);
_subscription = bloc.taskOperations.listen((operation) {
final l10n = L10N.of(context);
final task = operation.task;
final type = operation.type;
String title;
switch (type) {
case TaskOperationType.checked:
title = l10n.snackTaskDone(task.title);
break;
case TaskOperationType.deleted:
title = l10n.snackTaskDeleted(task.title);
break;
case TaskOperationType.add:
case TaskOperationType.updated:
return;
}
Scaffold.of(context).showSnackBar(
SnackBar(
content: Text(
title,
),
action: SnackBarAction(
label: l10n.buttonUndo,
textColor:
(Theme.of(context).accentColor as MaterialAccentColor)[100],
onPressed: () {
switch (type) {
case TaskOperationType.checked:
task.doneTime = null;
bloc.taskOperation.add(TaskOperation(
task: task, type: TaskOperationType.updated));
break;
case TaskOperationType.deleted:
bloc.taskOperation.add(
TaskOperation(task: task, type: TaskOperationType.add));
break;
case TaskOperationType.add:
case TaskOperationType.updated:
break;
}
},
),
),
);
});
}
@override
Widget build(BuildContext context) {
return AnimatedList(
key: _listKey,
initialItemCount: _list.length,
itemBuilder: (context, index, animation) {
final task = _list[index];
return TaskListTile(
key: ValueKey(task.id),
task: task,
animation: animation,
onDismissed: (_) {
final bloc = TasksBlocProvider.of(context);
_list.remove(task, skipAnimation: true);
bloc.taskOperation.add(
TaskOperation(
task: task,
type: TaskOperationType.deleted,
),
);
},
);
},
);
}
@override
void dispose() {
_subscription.cancel();
super.dispose();
}
}