Tom O'Sullivan
Tom O'Sullivan

Reputation: 4036

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.

Upvotes: 8

Views: 2403

Answers (1)

APEALED
APEALED

Reputation: 1663

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,
  ),
);

Upvotes: 2

Related Questions