Created
April 9, 2021 20:44
-
-
Save VB10/a22b8f94761d75e5a63f44f94d4da4d3 to your computer and use it in GitHub Desktop.
Flutter Lazy Load Extension for ListView
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
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