Skip to content

Instantly share code, notes, and snippets.

@VB10
Created April 9, 2021 20:44
Show Gist options
  • Save VB10/a22b8f94761d75e5a63f44f94d4da4d3 to your computer and use it in GitHub Desktop.
Save VB10/a22b8f94761d75e5a63f44f94d4da4d3 to your computer and use it in GitHub Desktop.
Flutter Lazy Load Extension for ListView
extension ListViewExtension on ListView {
Widget onLazyLoads(Future<void> Function() onPagingLoad, {Widget? itemLoadWidget}) {
final delegate = childrenDelegate as SliverChildBuilderDelegate;
final itemCount = delegate.childCount ?? 0;
return NotificationListener<ScrollNotification>(
onNotification: (ScrollNotification notification) {
if (notification.metrics.pixels == notification.metrics.maxScrollExtent) {
onPagingLoad();
}
return true;
},
child: ListView.builder(
itemCount: itemCount,
itemBuilder: (context, index) {
if (index == (itemCount - 1)) itemLoadWidget ?? Center(child: CircularProgressIndicator());
return delegate.builder(context, index)!;
},
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment