Last active
March 3, 2020 15:50
-
-
Save rrousselGit/5b818817181ff72266f1e0ce610a1458 to your computer and use it in GitHub Desktop.
SliderIterableChildDelegate
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
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