-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanimatedlist.dart
81 lines (76 loc) · 2.29 KB
/
animatedlist.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
import 'package:flutter/material.dart';
class AnimatedListWidget extends StatefulWidget {
const AnimatedListWidget({super.key});
@override
State<AnimatedListWidget> createState() => _AnimatedListWidgetState();
}
class _AnimatedListWidgetState extends State<AnimatedListWidget>
with TickerProviderStateMixin {
final _items = [];
final GlobalKey<AnimatedListState> _key = GlobalKey();
void _addItem() {
_items.insert(0, "item ${_items.length + 1}");
_key.currentState!.insertItem(0, duration: const Duration(seconds: 1));
}
void _removeItem(int index) {
_key.currentState!.removeItem(index,
duration: const Duration(milliseconds: 300), (_, animation) {
return SizeTransition(
sizeFactor: animation,
child: const Card(
margin: EdgeInsets.all(10),
color: Colors.red,
child: ListTile(
title: Text('Deleted', style: TextStyle(fontSize: 24)),
)),
);
});
_items.removeAt(index);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Widget Name"),
),
body: Column(
children: [
const SizedBox(
height: 10,
),
IconButton(
onPressed: _addItem,
icon: const Icon(Icons.add),
),
Expanded(
child: AnimatedList(
key: _key,
initialItemCount: 0,
padding: const EdgeInsets.all(0),
itemBuilder: ((context, index, animation) {
return SizeTransition(
sizeFactor: animation,
key: UniqueKey(),
child: Card(
margin: const EdgeInsets.all(10),
color: Colors.orange,
child: ListTile(
title: Text(
_items[index],
style: const TextStyle(fontSize: 24),
),
trailing: IconButton(
onPressed: (() {
_removeItem(index);
}),
icon: const Icon(Icons.delete)),
),
),
);
}),
))
],
),
);
}
}