Skip to content

Commit

Permalink
feat: add missing constructors to SliverList (#51)
Browse files Browse the repository at this point in the history
Fixes #49
  • Loading branch information
knopp authored Mar 26, 2024
1 parent 887ef2d commit 97c723e
Showing 1 changed file with 100 additions and 0 deletions.
100 changes: 100 additions & 0 deletions lib/src/super_sliver_list.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import "dart:math" as math;

import "package:flutter/rendering.dart";
import "package:flutter/widgets.dart";
import "package:logging/logging.dart";
Expand Down Expand Up @@ -387,6 +389,104 @@ class SuperSliverList extends SliverMultiBoxAdaptorWidget {
this.layoutKeptAliveChildren = false,
});

/// Creates a SuperSliverList from widget builder.
///
/// See [SliverList.builder] for details.
SuperSliverList.builder({
super.key,
required NullableIndexedWidgetBuilder itemBuilder,
ChildIndexGetter? findChildIndexCallback,
int? itemCount,
this.extentPrecalculationPolicy,
this.listController,
this.extentEstimation,
this.delayPopulatingCacheArea = true,
this.layoutKeptAliveChildren = false,
bool addAutomaticKeepAlives = true,
bool addRepaintBoundaries = true,
bool addSemanticIndexes = true,
}) : super(
delegate: SliverChildBuilderDelegate(
itemBuilder,
findChildIndexCallback: findChildIndexCallback,
childCount: itemCount,
addAutomaticKeepAlives: addAutomaticKeepAlives,
addRepaintBoundaries: addRepaintBoundaries,
addSemanticIndexes: addSemanticIndexes,
),
);

/// Creates a SuperSliverList from widget builder separated by separator
/// widgets.
///
/// See [SliverList.separated] for details.
SuperSliverList.separated({
super.key,
required NullableIndexedWidgetBuilder itemBuilder,
ChildIndexGetter? findChildIndexCallback,
required NullableIndexedWidgetBuilder separatorBuilder,
this.extentPrecalculationPolicy,
this.listController,
this.extentEstimation,
this.delayPopulatingCacheArea = true,
this.layoutKeptAliveChildren = false,
int? itemCount,
bool addAutomaticKeepAlives = true,
bool addRepaintBoundaries = true,
bool addSemanticIndexes = true,
}) : super(
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
final int itemIndex = index ~/ 2;
final Widget? widget;
if (index.isEven) {
widget = itemBuilder(context, itemIndex);
} else {
widget = separatorBuilder(context, itemIndex);
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: (Widget _, int index) {
return index.isEven ? index ~/ 2 : null;
},
),
);

/// Creates a SuperSliverList from list of child widgets.
///
/// See [SliverList.list] for details.
SuperSliverList.list({
super.key,
required List<Widget> children,
this.extentPrecalculationPolicy,
this.listController,
this.extentEstimation,
this.delayPopulatingCacheArea = true,
this.layoutKeptAliveChildren = false,
bool addAutomaticKeepAlives = true,
bool addRepaintBoundaries = true,
bool addSemanticIndexes = true,
}) : super(
delegate: SliverChildListDelegate(
children,
addAutomaticKeepAlives: addAutomaticKeepAlives,
addRepaintBoundaries: addRepaintBoundaries,
addSemanticIndexes: addSemanticIndexes,
),
);

/// When set provides access to extents of individual children.
/// [ListController] can also be used to jump to a specific item in the list.
final ListController? listController;
Expand Down

0 comments on commit 97c723e

Please sign in to comment.