-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
todos_overview_page.dart
135 lines (129 loc) · 4.68 KB
/
todos_overview_page.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_todos/edit_todo/view/edit_todo_page.dart';
import 'package:flutter_todos/l10n/l10n.dart';
import 'package:flutter_todos/todos_overview/todos_overview.dart';
import 'package:todos_repository/todos_repository.dart';
class TodosOverviewPage extends StatelessWidget {
const TodosOverviewPage({super.key});
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (context) => TodosOverviewBloc(
todosRepository: context.read<TodosRepository>(),
)..add(const TodosOverviewSubscriptionRequested()),
child: const TodosOverviewView(),
);
}
}
class TodosOverviewView extends StatelessWidget {
const TodosOverviewView({super.key});
@override
Widget build(BuildContext context) {
final l10n = context.l10n;
return Scaffold(
appBar: AppBar(
title: Text(l10n.todosOverviewAppBarTitle),
actions: const [
TodosOverviewFilterButton(),
TodosOverviewOptionsButton(),
],
),
body: MultiBlocListener(
listeners: [
BlocListener<TodosOverviewBloc, TodosOverviewState>(
listenWhen: (previous, current) =>
previous.status != current.status,
listener: (context, state) {
if (state.status == TodosOverviewStatus.failure) {
ScaffoldMessenger.of(context)
..hideCurrentSnackBar()
..showSnackBar(
SnackBar(
content: Text(l10n.todosOverviewErrorSnackbarText),
),
);
}
},
),
BlocListener<TodosOverviewBloc, TodosOverviewState>(
listenWhen: (previous, current) =>
previous.lastDeletedTodo != current.lastDeletedTodo &&
current.lastDeletedTodo != null,
listener: (context, state) {
final deletedTodo = state.lastDeletedTodo!;
final messenger = ScaffoldMessenger.of(context);
messenger
..hideCurrentSnackBar()
..showSnackBar(
SnackBar(
content: Text(
l10n.todosOverviewTodoDeletedSnackbarText(
deletedTodo.title,
),
),
action: SnackBarAction(
label: l10n.todosOverviewUndoDeletionButtonText,
onPressed: () {
messenger.hideCurrentSnackBar();
context
.read<TodosOverviewBloc>()
.add(const TodosOverviewUndoDeletionRequested());
},
),
),
);
},
),
],
child: BlocBuilder<TodosOverviewBloc, TodosOverviewState>(
builder: (context, state) {
if (state.todos.isEmpty) {
if (state.status == TodosOverviewStatus.loading) {
return const Center(child: CupertinoActivityIndicator());
} else if (state.status != TodosOverviewStatus.success) {
return const SizedBox();
} else {
return Center(
child: Text(
l10n.todosOverviewEmptyText,
style: Theme.of(context).textTheme.bodySmall,
),
);
}
}
return CupertinoScrollbar(
child: ListView(
children: [
for (final todo in state.filteredTodos)
TodoListTile(
todo: todo,
onToggleCompleted: (isCompleted) {
context.read<TodosOverviewBloc>().add(
TodosOverviewTodoCompletionToggled(
todo: todo,
isCompleted: isCompleted,
),
);
},
onDismissed: (_) {
context
.read<TodosOverviewBloc>()
.add(TodosOverviewTodoDeleted(todo));
},
onTap: () {
Navigator.of(context).push(
EditTodoPage.route(initialTodo: todo),
);
},
),
],
),
);
},
),
),
);
}
}