-
Notifications
You must be signed in to change notification settings - Fork 18
/
main.dart
180 lines (161 loc) · 4.67 KB
/
main.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import 'package:flutter/material.dart';
import 'package:todo_app1/new_todo.dart';
import 'package:todo_app1/todo.dart';
void main() => runApp(Main());
class Main extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'FlutterTodo',
home: Home(),
theme: ThemeData(
primarySwatch: Colors.orange,
),
);
}
}
class Home extends StatefulWidget {
@override
HomeState createState() => HomeState();
}
class HomeState extends State<Home> with TickerProviderStateMixin {
List<Todo> items = new List<Todo>();
GlobalKey<AnimatedListState> animatedListKey = GlobalKey<AnimatedListState>();
AnimationController emptyListController;
@override
void initState() {
emptyListController = AnimationController(
vsync: this,
duration: Duration(milliseconds: 200),
);
emptyListController.forward();
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
'FlutterTodo',
key: Key('main-app-title'),
),
centerTitle: true,
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: () =>goToNewItemView(),
),
body: renderBody()
);
}
Widget renderBody(){
if(items.length > 0){
return buildListView();
}else{
return emptyList();
}
}
Widget emptyList(){
return Center(
child: FadeTransition(
opacity: emptyListController,
child: Text('No items')
)
);
}
Widget buildListView() {
return AnimatedList(
key: animatedListKey,
initialItemCount: items.length,
itemBuilder: (BuildContext context,int index, animation){
return SizeTransition(
sizeFactor: animation,
child: buildItem(items[index], index),
);
},
);
}
Widget buildItem(Todo item, int index){
return Dismissible(
key: Key('${item.hashCode}'),
background: Container(color: Colors.red[700]),
onDismissed: (direction) => removeItemFromList(item, index),
direction: DismissDirection.startToEnd,
child: buildListTile(item, index),
);
}
Widget buildListTile(item, index){
return ListTile(
onTap: () => changeItemCompleteness(item),
onLongPress: () => goToEditItemView(item),
title: Text(
item.title,
key: Key('item-$index'),
style: TextStyle(
color: item.completed ? Colors.grey : Colors.black,
decoration: item.completed ? TextDecoration.lineThrough : null
),
),
trailing: Icon(item.completed
? Icons.check_box
: Icons.check_box_outline_blank,
key: Key('completed-icon-$index'),
),
);
}
void changeItemCompleteness(Todo item){
setState(() {
item.completed = !item.completed;
});
}
void goToNewItemView(){
// Here we are pushing the new view into the Navigator stack. By using a
// MaterialPageRoute we get standard behaviour of a Material app, which will
// show a back button automatically for each platform on the left top corner
Navigator.of(context).push(MaterialPageRoute(builder: (context){
return NewTodoView();
})).then((title){
if(title != null) {
addItem(Todo(title: title));
}
});
}
void addItem(Todo item){
// Insert an item into the top of our list, on index zero
items.insert(0, item);
if(animatedListKey.currentState != null)
animatedListKey.currentState.insertItem(0);
}
void goToEditItemView(Todo item){
// We re-use the NewTodoView and push it to the Navigator stack just like
// before, but now we send the title of the item on the class constructor
// and expect a new title to be returned so that we can edit the item
Navigator.of(context).push(MaterialPageRoute(builder: (context){
return NewTodoView(item: item);
})).then((title){
if(title != null) {
editItem(item, title);
}
});
}
void editItem(Todo item ,String title){
item.title = title;
}
void removeItemFromList(Todo item, int index) {
animatedListKey.currentState.removeItem(index, (context, animation){
return SizedBox(width: 0, height: 0,);
});
deleteItem(item);
}
void deleteItem(Todo item){
// We don't need to search for our item on the list because Dart objects
// are all uniquely identified by a hashcode. This means we just need to
// pass our object on the remove method of the list
items.remove(item);
if(items.isEmpty) {
emptyListController.reset();
setState(() {});
emptyListController.forward();
}
}
}