/
animated_list_page.dart
99 lines (87 loc) · 3.46 KB
/
animated_list_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
import 'package:flutter/material.dart';
class AnimatedListPage extends StatefulWidget {
@override
_AnimatedListPageState createState() => new _AnimatedListPageState();
}
class _AnimatedListPageState extends State<AnimatedListPage> {
final key = GlobalKey<AnimatedListState>();
static final List<UserBean> animatedList = [
UserBean('Monday', 'images/icon_hzw01.jpg'),
UserBean('Tuesday', 'images/icon_hzw02.jpg'),
UserBean('Wednesday', 'images/icon_hzw03.jpg'),
UserBean('Thursday', 'images/icon_hzw01.jpg'),
UserBean('Friday', 'images/icon_hzw02.jpg'),
UserBean('Saturday', 'images/icon_hzw03.jpg'),
UserBean('Sunday', 'images/icon_hzw01.jpg')
];
static final addItem = UserBean('Add Item', 'images/icon_music.png');
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey[100],
appBar: AppBar(title: Text('AnimatedList Page')),
body: AnimatedList(
key: key,
reverse: false,
initialItemCount: animatedList.length,
itemBuilder: (context, index, animation) =>
_buildItem4(animatedList[index], index, animation)),
floatingActionButton: FloatingActionButton(
onPressed: () => _insertItem(animatedList.length, addItem),
child: Icon(Icons.add, color: Colors.white)));
}
_buildItem(item, index, animation) =>
FadeTransition(opacity: animation, child: _itemWid(item, index));
_buildItem2(item, index, animation) => SlideTransition(
position: Tween<Offset>(begin: Offset(-1, 0), end: Offset(0, 0)).animate(
CurvedAnimation(
parent: animation,
curve: Curves.linear,
reverseCurve: Curves.linear)),
child: _itemWid(item, index));
_buildItem3(item, index, animation) => SlideTransition(
position: Tween<Offset>(begin: Offset(0, -1), end: Offset(0, 0)).animate(
CurvedAnimation(
parent: animation,
curve: Curves.linear,
reverseCurve: Curves.linear)),
child: SizeTransition(
axis: Axis.vertical,
sizeFactor: animation,
child: _itemWid(item, index)));
_buildItem4(item, index, animation) => SlideTransition(
position: Tween<Offset>(begin: Offset(0, -1), end: Offset(0, 0))
.animate(animation),
child: FadeTransition(
opacity: animation,
child: SizeTransition(
axis: Axis.vertical,
sizeFactor: animation,
child: _itemWid(item, index))));
_itemWid(item, index) => Container(
margin: EdgeInsets.symmetric(horizontal: 10.0, vertical: 1.0),
child: Card(
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 12.0, vertical: 10.0),
child: Row(children: [
CircleAvatar(backgroundImage: AssetImage(item.avatar)),
SizedBox(width: 15.0),
Expanded(child: Text(item.name)),
GestureDetector(
child: Icon(Icons.clear), onTap: () => _removeItem(index))
]))));
_insertItem(index, item) {
animatedList.insert(index, item);
key.currentState.insertItem(index);
}
_removeItem(index) {
final item = animatedList.removeAt(index);
key.currentState.removeItem(
index, (context, animation) => _buildItem4(item, index, animation));
}
}
class UserBean {
String name;
String avatar;
UserBean(this.name, this.avatar);
}