Skip to content

Instantly share code, notes, and snippets.

@rrousselGit
Last active March 3, 2020 15:50
Show Gist options
  • Save rrousselGit/5b818817181ff72266f1e0ce610a1458 to your computer and use it in GitHub Desktop.
Save rrousselGit/5b818817181ff72266f1e0ce610a1458 to your computer and use it in GitHub Desktop.
SliderIterableChildDelegate
class SliverIterableChildDelegate extends SliverChildDelegate {
SliverIterableChildDelegate(this.children);
final Iterable<Widget> children;
int _lastAccessedIndex;
Iterator<Widget> _lastAccessedIterator;
@override
Widget build(BuildContext context, int index) {
if (_lastAccessedIndex == null || _lastAccessedIndex > index) {
_lastAccessedIndex = -1;
_lastAccessedIterator = children.iterator;
}
while (_lastAccessedIndex < index) {
_lastAccessedIterator.moveNext();
_lastAccessedIndex++;
}
return _lastAccessedIterator.current;
}
@override
bool shouldRebuild(SliverIterableChildDelegate oldDelegate) {
return children != oldDelegate.children
|| _lastAccessedIndex != oldDelegate._lastAccessedIndex
|| _lastAccessedIterator != oldDelegate._lastAccessedIterator;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment