SliverExpandableChildDelegate<T, S extends ExpandableListSection<T> > constructor
SliverExpandableChildDelegate<T, S extends ExpandableListSection<T> > ({
- required List<
S> sectionList, - required ExpandableItemBuilder itemBuilder,
- ExpandableListController? controller,
- ExpandableSeparatorBuilder? separatorBuilder,
- ExpandableHeaderBuilder? headerBuilder,
- ExpandableSectionBuilder? sectionBuilder,
- bool sticky = true,
- bool overlapsContent = false,
- bool removeItemsOnCollapsed = true,
- bool addAutomaticKeepAlives = true,
- bool addRepaintBoundaries = true,
- 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;
},
);
}
}