Skip to content

Instantly share code, notes, and snippets.

@rafdls
Last active April 23, 2020 00:35
Show Gist options
  • Save rafdls/decb0fd7b7de30464d6796b2efc8cdb1 to your computer and use it in GitHub Desktop.
Save rafdls/decb0fd7b7de30464d6796b2efc8cdb1 to your computer and use it in GitHub Desktop.
Flutter list animation
class ListScreen extends StatelessWidget {
final DataSource _dataSource = DataSource();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('List Animation', style: TextStyle(fontWeight: FontWeight.bold)),
),
body: FutureBuilder<List<String>>(
initialData: [],
future: _dataSource.getCountriesFuture(),
builder: (context, snapshot) {
final List<String> countries = snapshot.data;
return LiveList(
itemCount: countries.length,
itemBuilder: (context, index, animation) {
return FadeTransition(
opacity: animation,
child: SlideTransition(
position: Tween<Offset>(
begin: Offset(0, 0.3),
end: Offset.zero,
).animate(animation),
child: ListItem(
title: countries[index],
),
),
);
},
);
},
),
);
}
}
LiveSliverList.options(
options: LiveOptions(
delay: Duration.zero,
reAnimateOnVisibility: false,
showItemDuration: Duration(milliseconds: 500),
showItemInterval: Duration(milliseconds: 50),
),
controller: _scrollController,
itemCount: countries.length,
itemBuilder: (context, index, animation) {
return FadeTransition(
opacity: animation,
child: SlideTransition(
position: Tween<Offset>(
begin: Offset(0, 0.3),
end: Offset.zero,
).animate(animation),
child: ListItem(
title: countries[index],
),
),
);
},
);
class SliverListScreen extends StatefulWidget {
@override
_SliverListScreenState createState() => _SliverListScreenState();
}
class _SliverListScreenState extends State<SliverListScreen> {
final DataSource _dataSource = DataSource();
ScrollController _scrollController;
@override
void initState() {
_scrollController = ScrollController();
super.initState();
}
@override
void dispose() {
_scrollController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: CustomScrollView(
controller: _scrollController,
slivers: <Widget>[
SliverAppBar(
expandedHeight: 120,
flexibleSpace: FlexibleSpaceBar(
title: Text('List Animation', style: TextStyle(fontWeight: FontWeight.bold)),
)),
FutureBuilder<List<String>>(
initialData: [],
future: _dataSource.getCountriesFuture(),
builder: (context, snapshot) {
final List<String> countries = snapshot.data;
return LiveSliverList(
controller: _scrollController,
itemCount: countries.length,
itemBuilder: (context, index, animation) {
return FadeTransition(
opacity: animation,
child: SlideTransition(
position: Tween<Offset>(
begin: Offset(0, 0.3),
end: Offset.zero,
).animate(animation),
child: ListItem(
title: countries[index],
),
),
);
},
);
},
),
],
),
);
}
}
dependencies:
flutter:
sdk: flutter
auto_animated: ^2.0.2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment