Last active
October 4, 2021 08:06
-
-
Save NearHuscarl/e5ddb59b4ed745d693f5d9025519d789 to your computer and use it in GitHub Desktop.
57100219/how-to-animate-the-items-rendered-initially-using-animated-list-in-flutter
This file contains 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
// https://gist.github.com/NearHuscarl/e5ddb59b4ed745d693f5d9025519d789 | |
// https://stackoverflow.com/a/59121771/9449426 | |
import 'package:flutter/material.dart'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'YourAwesomeApp', | |
home: PageWithAnimatedList(), | |
); | |
} | |
} | |
class PageWithAnimatedList extends StatefulWidget { | |
@override | |
_PageWithAnimatedListState createState() => _PageWithAnimatedListState(); | |
} | |
class _PageWithAnimatedListState extends State<PageWithAnimatedList> { | |
final _listItems = <Widget>[]; | |
final GlobalKey<AnimatedListState> _listKey = GlobalKey(); | |
@override | |
void initState() { | |
super.initState(); | |
_loadItems(); | |
} | |
void _loadItems() { | |
// fetching data from web api, db... | |
final fetchedList = [ | |
const ListTile( | |
title: Text('Economy'), | |
trailing: Icon(Icons.directions_car), | |
), | |
const ListTile( | |
title: Text('Comfort'), | |
trailing: Icon(Icons.motorcycle), | |
), | |
const ListTile( | |
title: Text('Business'), | |
trailing: Icon(Icons.flight), | |
), | |
]; | |
var future = Future(() {}); | |
for (var i = 0; i < fetchedList.length; i++) { | |
future = future.then((_) { | |
return Future.delayed(const Duration(milliseconds: 100), () { | |
_listItems.add(fetchedList[i]); | |
_listKey.currentState?.insertItem(_listItems.length - 1); | |
}); | |
}); | |
} | |
} | |
void _unloadItems() { | |
var future = Future(() {}); | |
for (var i = _listItems.length - 1; i >= 0; i--) { | |
future = future.then((_) { | |
return Future.delayed(const Duration(milliseconds: 100), () { | |
final deletedItem = _listItems.removeAt(i); | |
_listKey.currentState?.removeItem(i, | |
(BuildContext context, Animation<double> animation) { | |
return SlideTransition( | |
position: CurvedAnimation( | |
curve: Curves.easeOut, | |
parent: animation, | |
).drive((Tween<Offset>( | |
begin: const Offset(1, 0), | |
end: const Offset(0, 0), | |
))), | |
child: deletedItem, | |
); | |
}); | |
}); | |
}); | |
} | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
actions: <Widget>[ | |
IconButton(icon: const Icon(Icons.add), onPressed: _loadItems), | |
IconButton(icon: const Icon(Icons.remove), onPressed: _unloadItems) | |
], | |
), | |
body: AnimatedList( | |
key: _listKey, | |
padding: const EdgeInsets.only(top: 10), | |
initialItemCount: _listItems.length, | |
itemBuilder: (context, index, animation) { | |
return SlideTransition( | |
position: CurvedAnimation( | |
curve: Curves.easeOut, | |
parent: animation, | |
).drive((Tween<Offset>( | |
begin: const Offset(1, 0), | |
end: const Offset(0, 0), | |
))), | |
child: _listItems[index], | |
); | |
}, | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment