Last active
July 30, 2021 11:02
-
-
Save IsmailAlamKhan/2b07b10b275608cbd2ae38d6dc10e38d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Material App', | |
home: Home(), | |
); | |
} | |
} | |
class Home extends StatefulWidget { | |
const Home({ | |
Key? key, | |
}) : super(key: key); | |
@override | |
_HomeState createState() => _HomeState(); | |
} | |
class _HomeState extends State<Home> { | |
final list = <String>[]; | |
@override | |
void initState() { | |
super.initState(); | |
list.addAll( | |
List.generate(10, (index) => 'Item ${index + 1}', growable: true)); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar(title: Text('Material App Bar')), | |
body: _List( | |
list: list, | |
onDelete: (index) => setState(() => list.removeAt(index)), | |
), | |
floatingActionButton: FloatingActionButton( | |
onPressed: () { | |
setState(() { | |
list.add('Item ${list.length + 1}'); | |
}); | |
}, | |
child: Icon(Icons.add), | |
), | |
); | |
} | |
} | |
class _List extends StatefulWidget { | |
_List({ | |
Key? key, | |
required this.list, | |
required this.onDelete, | |
}) : super(key: key); | |
final List<String> list; | |
final ValueChanged<int> onDelete; | |
@override | |
__ListState createState() => __ListState(); | |
} | |
class __ListState extends State<_List> with TickerProviderStateMixin { | |
final animationControllers = <AnimationController>[]; | |
@override | |
void initState() { | |
super.initState(); | |
initControllers(widget.list.length); | |
} | |
void initControllers(int length) { | |
animationControllers.clear(); | |
for (var i = 0; i < length; i++) { | |
animationControllers.add( | |
AnimationController( | |
vsync: this, | |
value: 1, | |
duration: const Duration(milliseconds: 500), | |
), | |
); | |
animationControllers[i].forward(); | |
} | |
} | |
@override | |
void didUpdateWidget(_List oldWidget) { | |
print(oldWidget.list.length); | |
if (animationControllers.length != widget.list.length) { | |
animationControllers.add( | |
AnimationController( | |
vsync: this, | |
duration: const Duration(milliseconds: 500), | |
), | |
); | |
animationControllers.last.forward(); | |
} | |
super.didUpdateWidget(oldWidget); | |
} | |
Future<void> onDelete(int index) async { | |
await animationControllers[index].reverse(); | |
animationControllers[index].dispose(); | |
widget.onDelete(index); | |
animationControllers.removeAt(index); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return ListView.builder( | |
itemCount: widget.list.length, | |
itemBuilder: (context, index) { | |
return SizeTransition( | |
sizeFactor: animationControllers[index], | |
child: ListTile( | |
title: Text(widget.list[index]), | |
trailing: IconButton( | |
onPressed: () => onDelete(index), | |
icon: Icon(Icons.delete), | |
), | |
), | |
); | |
}, | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment