wwwdata / implicitly_animated_reorderable_list

Fork of the discontinued plugin to continue maintaining it
MIT License
32 stars 22 forks source link

"separated" builder #25

Closed Maatteogekko closed 1 year ago

Maatteogekko commented 1 year ago

I was looking for a way to use SliverImplicitlyAnimatedList like a SliverList.separated. This is not possible with the current API, but looking at the code it should be easy to allow it.

I was thinking to allow passing a delegateBuilder to CustomSliverAnimatedList and use it here. Then, for my case at least, I would be able to pass a delegate like this:

class SliverChildSeparatedBuilderDelegate extends SliverChildBuilderDelegate {
  SliverChildSeparatedBuilderDelegate({
    required NullableIndexedWidgetBuilder itemBuilder,
    ChildIndexGetter? findChildIndexCallback,
    required NullableIndexedWidgetBuilder separatorBuilder,
    int? itemCount,
    bool addAutomaticKeepAlives = true,
    bool addRepaintBoundaries = true,
    bool addSemanticIndexes = true,
  }) : super(
          (context, index) {
            final itemIndex = index ~/ 2;
            final Widget? widget;
            if (index.isEven) {
              widget = itemBuilder(context, itemIndex);
            } else {
              widget = separatorBuilder(context, itemIndex);
              // ignore: prefer_asserts_with_message , we use FlutterError
              assert(() {
                if (widget == null) {
                  throw FlutterError('separatorBuilder cannot return null.');
                }

                return true;
              }());
            }

            return widget;
          },
          findChildIndexCallback: findChildIndexCallback,
          childCount: itemCount == null ? null : math.max(0, itemCount * 2 - 1),
          addAutomaticKeepAlives: addAutomaticKeepAlives,
          addRepaintBoundaries: addRepaintBoundaries,
          addSemanticIndexes: addSemanticIndexes,
          semanticIndexCallback: (_, index) {
            return index.isEven ? index ~/ 2 : null;
          },
        );
}

Then we could also easily have SliverImplicitlyAnimatedList.separated() and ImplicitlyAnimatedList.separated().

I don't see why this should not be possible, but also haven't tested it yet. I could make a PR for this.

wwwdata commented 1 year ago

sounds good. Go for it and open a PR :) Any contribution is welcome 👍

wwwdata commented 1 year ago

Thanks a lot again. It's merged in and I released 0.5.1 which has the changes.