SliverExpandableChildDelegate<T, S extends ExpandableListSection<T>> constructor

SliverExpandableChildDelegate<T, S extends ExpandableListSection<T>>({
  1. required List<S> sectionList,
  2. required ExpandableItemBuilder itemBuilder,
  3. ExpandableListController? controller,
  4. ExpandableSeparatorBuilder? separatorBuilder,
  5. ExpandableHeaderBuilder? headerBuilder,
  6. ExpandableSectionBuilder? sectionBuilder,
  7. bool sticky = true,
  8. bool overlapsContent = false,
  9. bool removeItemsOnCollapsed = true,
  10. bool addAutomaticKeepAlives = true,
  11. bool addRepaintBoundaries = true,
  12. bool addSemanticIndexes = true,
})

Implementation

SliverExpandableChildDelegate(
    {required this.sectionList,
    required this.itemBuilder,
    this.controller,
    this.separatorBuilder,
    this.headerBuilder,
    this.sectionBuilder,
    this.sticky = true,
    this.overlapsContent = false,
    this.removeItemsOnCollapsed = true,
    bool addAutomaticKeepAlives = true,
    bool addRepaintBoundaries = true,
    bool addSemanticIndexes = true})
    : assert(
        (headerBuilder != null && sectionBuilder == null) ||
            (headerBuilder == null && sectionBuilder != null),
        'You must specify either headerBuilder or sectionBuilder.',
      ),
      sectionRealIndexes = _buildSectionRealIndexes(sectionList) {
  controller ??= ExpandableListController();
  if (separatorBuilder == null) {
    delegate = SliverChildBuilderDelegate(
      (BuildContext context, int index) {
        int sectionIndex = index;
        S section = sectionList[sectionIndex];
        int sectionRealIndex = sectionRealIndexes[sectionIndex];

        int sectionChildCount = section.getItems()?.length ?? 0;
        if (!section.isSectionExpanded()) {
          sectionChildCount = 0;
        }
        var childBuilderDelegate = SliverChildBuilderDelegate(
            (context, i) => itemBuilder(
                context, sectionIndex, i, sectionRealIndex + i + 1),
            childCount: sectionChildCount);
        var containerInfo = ExpandableSectionContainerInfo(
          separated: false,
          listIndex: index,
          sectionIndex: sectionIndex,
          sectionRealIndexes: sectionRealIndexes,
          sticky: sticky,
          overlapsContent: overlapsContent,
          controller: controller!,
          header: Container(),
          content: Container(),
          childDelegate: childBuilderDelegate,
        );
        Widget? container = sectionBuilder != null
            ? sectionBuilder!(context, containerInfo)
            : null;
        if (container == null) {
          containerInfo
            ..header = headerBuilder!(context, sectionIndex, sectionRealIndex)
            ..content = buildDefaultContent(context, containerInfo);
          container = ExpandableSectionContainer(
            info: containerInfo,
          );
        }
        assert(containerInfo.header != null);
        assert(containerInfo.content != null);
        return container;
      },
      childCount: sectionList.length,
      addAutomaticKeepAlives: addAutomaticKeepAlives,
      addRepaintBoundaries: addRepaintBoundaries,
      addSemanticIndexes: addSemanticIndexes,
    );
  } else {
    delegate = SliverChildBuilderDelegate(
      (BuildContext context, int index) {
        final int sectionIndex = index ~/ 2;
        Widget itemView;
        S section = sectionList[sectionIndex];
        int sectionRealIndex = sectionRealIndexes[sectionIndex];
        if (index.isEven) {
          int sectionChildCount =
              _computeSemanticChildCount(section.getItems()?.length ?? 0);
          if (!section.isSectionExpanded()) {
            sectionChildCount = 0;
          }
          var childBuilderDelegate = SliverChildBuilderDelegate((context, i) {
            int itemRealIndex = sectionRealIndex + (i ~/ 2) + 1;
            if (i.isEven) {
              return itemBuilder(
                  context, sectionIndex, i ~/ 2, itemRealIndex);
            } else {
              return separatorBuilder!(context, false, itemRealIndex);
            }
          }, childCount: sectionChildCount);
          var containerInfo = ExpandableSectionContainerInfo(
            separated: true,
            listIndex: index,
            sectionIndex: sectionIndex,
            sectionRealIndexes: sectionRealIndexes,
            sticky: sticky,
            overlapsContent: overlapsContent,
            controller: controller!,
            header: Container(),
            content: Container(),
            childDelegate: childBuilderDelegate,
          );
          Widget? container = sectionBuilder != null
              ? sectionBuilder!(context, containerInfo)
              : null;
          if (container == null) {
            containerInfo
              ..header =
                  headerBuilder!(context, sectionIndex, sectionRealIndex)
              ..content = buildDefaultContent(context, containerInfo);
            container = ExpandableSectionContainer(
              info: containerInfo,
            );
          }
          assert(containerInfo.header != null);
          assert(containerInfo.content != null);
          return container;
        } else {
          itemView = separatorBuilder!(context, true,
              sectionIndex + (section.getItems()?.length ?? 0));
        }
        return itemView;
      },
      childCount: _computeSemanticChildCount(sectionList.length),
      addAutomaticKeepAlives: addAutomaticKeepAlives,
      addRepaintBoundaries: addRepaintBoundaries,
      addSemanticIndexes: addSemanticIndexes,
      semanticIndexCallback: (Widget _, int index) {
        return index.isEven ? index ~/ 2 : null;
      },
    );
  }
}