AnimatedList Widget
Project Preview
- Interested in seeing the live demo? Click here to explore.
- Want to view the source code? Visit the project on GitHub.
Code Snippet
import 'package:flutter/material.dart';
class AnimatedListWidget extends StatefulWidget {
const AnimatedListWidget({super.key});
@override
State<AnimatedListWidget> createState() => _AnimatedListWidgetState();
}
class _AnimatedListWidgetState extends State<AnimatedListWidget> {
final _items = [];
final GlobalKey<AnimatedListState> _key = GlobalKey();
void _addItem() {
_items.insert(0, "Items ${_items.length + 1}");
_key.currentState!.insertItem(
0,
duration: const Duration(seconds: 1),
);
}
void _removeItem(int index) {
_key.currentState!.removeItem(
index,
(_, animation) {
return SizeTransition(
sizeFactor: animation,
child: const Card(
margin: EdgeInsets.all(10),
color: Colors.red,
child: ListTile(
title: Text(
'Deleted',
style: TextStyle(
fontSize: 24,
),
),
),
),
);
},
duration: const Duration(milliseconds: 300),
);
_items.removeAt(index);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("AnimatedList Widget"),
),
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(10.0),
itemBuilder: (context, index, animation) {
return SizeTransition(
key: UniqueKey(),
sizeFactor: animation,
child: Card(
margin: const EdgeInsets.all(10.0),
color: Colors.orangeAccent,
child: ListTile(
title: Text(
_items[index],
style: const TextStyle(
fontSize: 24,
),
),
trailing: IconButton(
icon: const Icon(Icons.delete),
onPressed: () {
_removeItem(index);
},
),
),
),
);
}),
),
],
),
);
}
}