Last active
May 20, 2020 18:15
-
-
Save e200/73550ae54350c4a2f26dc313cb297f41 to your computer and use it in GitHub Desktop.
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
int _page = 1; | |
int _limit = 10; | |
ScrollController _scrollController; | |
@override | |
void initState() { | |
_setupScrollController(); | |
_fetch(); | |
super.initState(); | |
} | |
void _setupScrollController() { | |
_scrollController = ScrollController(); | |
_scrollController.addListener(_scrollListener); | |
} | |
void _scrollListener() { | |
if (_scrollController.offset >= | |
_scrollController.position.maxScrollExtent) { | |
_page++; | |
_fetch(); | |
} | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('Posts: ${_posts.length}'), | |
), | |
body: Builder( | |
builder: (context) { | |
if (_posts.isEmpty) { | |
return Center( | |
child: CircularProgressIndicator(), | |
); | |
} | |
return ListView.builder( | |
controller: _scrollController, | |
itemBuilder: (BuildContext context, int index) { | |
if (index == _posts.length) { | |
return ListTile( | |
title: Row( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
CircularProgressIndicator(), | |
], | |
), | |
); | |
} else { | |
final _post = _posts[index]; | |
return ListTile( | |
title: Text( | |
_post['title'], | |
), | |
subtitle: Text( | |
_post['body'], | |
), | |
); | |
} | |
}, | |
itemCount: _posts.length + 1, | |
); | |
}, | |
), | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment