Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SliverChildBuilderDelegate - reverse method

Is there a reverse option for a SliverList like there is for ListView.builder?

I can see that the CustomScrollView has a reverse method but that doesn't help for what I'm looking for.

like image 433
Tom O'Sullivan Avatar asked Jun 02 '19 11:06

Tom O'Sullivan


1 Answers

Since SliverList does not have a a reverse option or parameter, you'll have to calculate the reversed index by hand inside the SliverChildDelegate that's used to create the children. This would only be possible if you know the length of the list you are building.

Given you have a List, you can calculate the reversed index from SliverChildDelegate.builder's index.

(BuildContext context, int index) {
  final reversedIndex = items.length - index - 1;
  ...
}

Here is what it looks with a SliverChildBuilderDelegate:

SliverList(
  delegate: SliverChildBuilderDelegate(
    (BuildContext context, int index) {
      final reversedIndex = items.length - index - 1;
      return MyWidget(items[reversedIndex]);
    },
    childCount: items.length,
  ),
);
like image 66
Apealed Avatar answered Nov 03 '22 05:11

Apealed