-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanimated_list_page.dart
115 lines (105 loc) · 3.02 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import 'package:flutter/material.dart';
import 'package:flutter_animations/app_drawer.dart';
import 'package:flutter_animations/core/duration_items.dart';
class AnimatedListPage extends StatefulWidget {
const AnimatedListPage({Key? key}) : super(key: key);
@override
State<AnimatedListPage> createState() => _AnimatedListPageState();
}
class _AnimatedListPageState extends State<AnimatedListPage> {
final GlobalKey<AnimatedListState> _listKey = GlobalKey();
final Tween<Offset> _offset =
Tween(begin: const Offset(1, 0), end: Offset.zero);
List<String> listItems = [
"Item 1",
"Item 2",
"Item 3",
"Item 4",
"Item 5",
"Item 6",
];
void removeFunction(int index) {
listItems.removeAt(index);
_listKey.currentState?.removeItem(index, (context, animation) {
return SlideTransition(
position: animation.drive(_offset),
child: ItemCard(
item: listItems[index],
index: index,
removeFunction: removeFunction,
),
);
}, duration: const DurationItems.durationLow());
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("AnimatedList (Implicit)"),
),
drawer: const AppDrawer(),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () {
setState(() {
listItems.add("hello");
_listKey.currentState?.insertItem(listItems.length - 1,
duration: const DurationItems.durationLow());
});
},
child: const Text("Add Item to End"),
),
const SizedBox(
height: 20,
),
Expanded(
child: AnimatedList(
key: _listKey,
initialItemCount: listItems.length,
itemBuilder: (context, index, animation) {
return SlideTransition(
position: animation.drive(_offset),
child: ItemCard(
item: listItems[index],
index: index,
removeFunction: removeFunction,
),
);
},
),
),
],
),
),
);
}
}
class ItemCard extends StatelessWidget {
const ItemCard({
required this.item,
required this.index,
required this.removeFunction,
super.key,
});
final String item;
final int index;
final Function(int index) removeFunction;
@override
Widget build(BuildContext context) {
return Card(
child: ListTile(
leading: const Icon(Icons.star),
title: Text(item),
trailing: IconButton(
onPressed: () {
removeFunction(index);
},
icon: const Icon(Icons.delete),
),
),
);
}
}