Skip to content

Instantly share code, notes, and snippets.

@VB10
Last active July 9, 2023 12:08
Show Gist options
  • Save VB10/d6ec6326d22b2d07b72239739b4d739b to your computer and use it in GitHub Desktop.
Save VB10/d6ec6326d22b2d07b72239739b4d739b to your computer and use it in GitHub Desktop.
extension ListViewExtension on ListView {
// It will notify to each paging load
// You should control the state for your widget
Widget onLazyLoads(Future<void> Function() onPagingLoad, {Widget? itemLoadWidget}) {
if (childrenDelegate is SliverChildListDelegate) throw Exception('You should use list view builder ');
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) {
final childBuilder = delegate.builder(context, index) ?? const SizedBox.shrink();
if (index == itemCount - 1) {
return Column(
children: [childBuilder, itemLoadWidget ?? const Center(child: CircularProgressIndicator())],
);
}
return childBuilder;
},
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment