Last active
July 9, 2023 12:08
-
-
Save VB10/d6ec6326d22b2d07b72239739b4d739b to your computer and use it in GitHub Desktop.
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 { | |
// 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