import 'package:animated_list_example/models/Person.dart';
import 'package:flutter/material.dart';
class MainListScreen extends StatefulWidget {
const MainListScreen({super.key});
@override
State<MainListScreen> createState() => _MainListScreenState();
}
class _MainListScreenState extends State<MainListScreen> {
final GlobalKey<AnimatedListState> _listKey = GlobalKey<AnimatedListState>();
List<Person> people = [];
TextEditingController nameController = TextEditingController();
TextEditingController designationController = TextEditingController();
void _addPerson(personName, personDesignation) {
people.add(
Person(personName, personDesignation),
);
_listKey.currentState!.insertItem(
people.length - 1,
duration: const Duration(milliseconds: 1000),
);
}
void _removePerson(int index) {
_listKey.currentState!.removeItem(
index,
(context, animation) => SizeTransition(
sizeFactor: animation,
child: const Card(
margin: EdgeInsets.all(10),
color: Colors.red,
child: ListTile(
title: Text(
"Deleted",
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
),
),
),
duration: const Duration(milliseconds: 500),
);
people.removeAt(index);
}
@override
void initState() {
WidgetsBinding.instance.addPostFrameCallback((_) {
_addPerson('Manasi Konde', 'Web Developer');
_addPerson('Rohan Chaudhary', 'Software Engineer');
_addPerson('Vishal Chumbalkar', 'Software Engineer');
_addPerson('Pranav Deshapande', 'Data Scientist');
_addPerson('Jayesh Shinde', 'DevOps Engineer');
});
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('GeeksForGeeks'),
),
body: AnimatedList(
key: _listKey,
initialItemCount: 0,
itemBuilder: (context, index, animation) {
return SizeTransition(
key: UniqueKey(),
sizeFactor: animation,
child: Card(
margin: const EdgeInsets.all(8.0),
child: ListTile(
tileColor: Colors.grey[200],
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
title: Text(
people[index].name,
style: const TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Colors.green,
),
),
subtitle: Text(
people[index].designation,
style: const TextStyle(
fontSize: 15,
fontWeight: FontWeight.bold,
color: Colors.grey,
),
),
trailing: IconButton(
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all<Color>(Colors.black12),
shape: MaterialStateProperty.all<RoundedRectangleBorder>(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
),
),
icon: const Icon(Icons.delete, color: Colors.red),
onPressed: () {
_removePerson(index);
},
),
),
),
);
},
),
floatingActionButton: FloatingActionButton(
backgroundColor: Colors.white,
onPressed: () {
//pop up
_showDialog();
},
child: const Icon(Icons.add, color: Colors.green),
),
);
}
// pop up dialog for adding new person
_showDialog() {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Add Person'),
content: Column(
children: <Widget>[
TextField(
controller: nameController,
decoration: InputDecoration(hintText: "Enter Name"),
),
TextField(
controller: designationController,
decoration: InputDecoration(hintText: "Enter Designation"),
),
],
),
actions: <Widget>[
ElevatedButton(
onPressed: () {
_addPerson(nameController.text, designationController.text);
Navigator.of(context).pop();
},
child: Text('Add'),
),
],
);
},
);
}
}