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

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

Implementation

SliverExpandableChildDelegate(
    {required this.sectionList,
    required this.itemBuilder,
    this.controller,
    this.separatorBuilder,
    this.headerBuilder,
    this.sectionBuilder,
    this.sticky = true,
    this.removeItemsOnCollapsed = true,
    bool addAutomaticKeepAlives = true,
    bool addRepaintBoundaries = true,
    bool addSemanticIndexes = true})
    : assert(headerBuilder == null || sectionBuilder == null),
      sectionRealIndexes = _buildSectionRealIndexes(sectionList) {
  if (controller == null) {
    controller = ExpandableListController();
  }
  if (separatorBuilder == null) {
    delegate = SliverChildBuilderDelegate(
      (BuildContext context, int index) {
        int sectionIndex = index;
        S section = sectionList[sectionIndex];
        int sectionRealIndex = sectionRealIndexes[sectionIndex];
        int itemRealIndex = sectionRealIndex;

        bool hasChildren =
            ((removeItemsOnCollapsed && !section.isSectionExpanded()) ||
                section.getItems() == null);
        var children = hasChildren
            ? <Widget>[]
            : List.generate(
                section.getItems()!.length,
                (i) =>
                    itemBuilder(context, sectionIndex, i, ++itemRealIndex));
        var containerInfo = ExpandableSectionContainerInfo(
          separated: false,
          listIndex: index,
          sectionIndex: sectionIndex,
          sectionRealIndexes: sectionRealIndexes,
          sticky: sticky,
          controller: controller,
          header: null,
          content: Column(
            children: children,
          ),
          children: children,
        );
        Widget? container = sectionBuilder != null
            ? sectionBuilder!(context, containerInfo)
            : null;
        if (container == null) {
          containerInfo
            ..header =
                headerBuilder!(context, sectionIndex, sectionRealIndex);
          container = ExpandableSectionContainer(
            info: containerInfo,
          );
        }
        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);
          int itemRealIndex = sectionRealIndex;

          bool hasChildren =
              ((removeItemsOnCollapsed && !section.isSectionExpanded()) ||
                  section.getItems() == null);
          var children = hasChildren
              ? <Widget>[]
              : List.generate(
                  sectionChildCount,
                  (i) => i.isEven
                      ? itemBuilder(
                          context, sectionIndex, i ~/ 2, ++itemRealIndex)
                      : separatorBuilder!(context, false, itemRealIndex - 1));

          var containerInfo = ExpandableSectionContainerInfo(
            separated: true,
            listIndex: index,
            sectionIndex: sectionIndex,
            sectionRealIndexes: sectionRealIndexes,
            sticky: sticky,
            controller: controller,
            header: null,
            children: children,
            content: Column(
              children: children,
            ),
          );
          Widget? container = sectionBuilder != null
              ? sectionBuilder!(context, containerInfo)
              : null;
          if (container == null) {
            containerInfo
              ..header =
                  headerBuilder!(context, sectionIndex, sectionRealIndex);
            container = ExpandableSectionContainer(
              info: containerInfo,
            );
          }
          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;
      },
    );
  }
}