Skip to content

Commit 25a0cc7

Browse files
authored
Light sliver clean up before SliverTree (#146696)
This cleans up a few sliver classes, like moving RenderSliverVariedExtentList to the rendering layer (it was in the widgets layer), and moving SliverVariedExtentList to live with its sibling subclasses, SliverFixedExtentList, SliverList, and so on. I moved these while working on SliverTree, so figure I should break out into a separate change. SliverTree and SliverCarousel (both inbound in separate changes) will also be subclasses of RenderSliverFixedExtentBoxAdaptor, organizing them together felt easier to work with. Related to flutter/flutter#114299 and flutter/flutter#125980
1 parent 58ac0dc commit 25a0cc7

File tree

6 files changed

+132
-144
lines changed

6 files changed

+132
-144
lines changed

packages/flutter/lib/src/rendering/sliver_fixed_extent_list.dart

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,3 +466,28 @@ class RenderSliverFixedExtentList extends RenderSliverFixedExtentBoxAdaptor {
466466
markNeedsLayout();
467467
}
468468
}
469+
470+
/// A sliver that places multiple box children with the corresponding main axis extent in
471+
/// a linear array.
472+
class RenderSliverVariedExtentList extends RenderSliverFixedExtentBoxAdaptor {
473+
/// Creates a sliver that contains multiple box children that have a explicit
474+
/// extent in the main axis.
475+
RenderSliverVariedExtentList({
476+
required super.childManager,
477+
required ItemExtentBuilder itemExtentBuilder,
478+
}) : _itemExtentBuilder = itemExtentBuilder;
479+
480+
@override
481+
ItemExtentBuilder get itemExtentBuilder => _itemExtentBuilder;
482+
ItemExtentBuilder _itemExtentBuilder;
483+
set itemExtentBuilder(ItemExtentBuilder value) {
484+
if (_itemExtentBuilder == value) {
485+
return;
486+
}
487+
_itemExtentBuilder = value;
488+
markNeedsLayout();
489+
}
490+
491+
@override
492+
double? get itemExtent => null;
493+
}

packages/flutter/lib/src/widgets/reorderable_list.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import 'scrollable.dart';
2222
import 'scrollable_helpers.dart';
2323
import 'sliver.dart';
2424
import 'sliver_prototype_extent_list.dart';
25-
import 'sliver_varied_extent_list.dart';
2625
import 'ticker_provider.dart';
2726
import 'transitions.dart';
2827

packages/flutter/lib/src/widgets/scroll_view.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import 'scrollable.dart';
2424
import 'scrollable_helpers.dart';
2525
import 'sliver.dart';
2626
import 'sliver_prototype_extent_list.dart';
27-
import 'sliver_varied_extent_list.dart';
2827
import 'viewport.dart';
2928

3029
// Examples can assume:

packages/flutter/lib/src/widgets/sliver.dart

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,113 @@ class SliverFixedExtentList extends SliverMultiBoxAdaptorWidget {
521521
}
522522
}
523523

524+
/// A sliver that places its box children in a linear array and constrains them
525+
/// to have the corresponding extent returned by [itemExtentBuilder].
526+
///
527+
/// _To learn more about slivers, see [CustomScrollView.slivers]._
528+
///
529+
/// [SliverVariedExtentList] arranges its children in a line along
530+
/// the main axis starting at offset zero and without gaps. Each child is
531+
/// constrained to the corresponding extent along the main axis
532+
/// and the [SliverConstraints.crossAxisExtent] along the cross axis.
533+
///
534+
/// [SliverVariedExtentList] is more efficient than [SliverList] because
535+
/// [SliverVariedExtentList] does not need to lay out its children to obtain
536+
/// their extent along the main axis. It's a little more flexible than
537+
/// [SliverFixedExtentList] because this allow the children to have different extents.
538+
///
539+
/// See also:
540+
///
541+
/// * [SliverFixedExtentList], whose children are forced to a given pixel
542+
/// extent.
543+
/// * [SliverPrototypeExtentList], which is similar to [SliverFixedExtentList]
544+
/// except that it uses a prototype list item instead of a pixel value to define
545+
/// the main axis extent of each item.
546+
/// * [SliverList], which does not require its children to have the same
547+
/// extent in the main axis.
548+
/// * [SliverFillViewport], which sizes its children based on the
549+
/// size of the viewport, regardless of what else is in the scroll view.
550+
class SliverVariedExtentList extends SliverMultiBoxAdaptorWidget {
551+
/// Creates a sliver that places box children with the same main axis extent
552+
/// in a linear array.
553+
const SliverVariedExtentList({
554+
super.key,
555+
required super.delegate,
556+
required this.itemExtentBuilder,
557+
});
558+
559+
/// A sliver that places multiple box children in a linear array along the main
560+
/// axis.
561+
///
562+
/// [SliverVariedExtentList] places its children in a linear array along the main
563+
/// axis starting at offset zero and without gaps. Each child is forced to have
564+
/// the returned extent of [itemExtentBuilder] in the main axis and the
565+
/// [SliverConstraints.crossAxisExtent] in the cross axis.
566+
///
567+
/// This constructor is appropriate for sliver lists with a large (or
568+
/// infinite) number of children whose extent is already determined.
569+
///
570+
/// Providing a non-null `itemCount` improves the ability of the [SliverGrid]
571+
/// to estimate the maximum scroll extent.
572+
SliverVariedExtentList.builder({
573+
super.key,
574+
required NullableIndexedWidgetBuilder itemBuilder,
575+
required this.itemExtentBuilder,
576+
ChildIndexGetter? findChildIndexCallback,
577+
int? itemCount,
578+
bool addAutomaticKeepAlives = true,
579+
bool addRepaintBoundaries = true,
580+
bool addSemanticIndexes = true,
581+
}) : super(delegate: SliverChildBuilderDelegate(
582+
itemBuilder,
583+
findChildIndexCallback: findChildIndexCallback,
584+
childCount: itemCount,
585+
addAutomaticKeepAlives: addAutomaticKeepAlives,
586+
addRepaintBoundaries: addRepaintBoundaries,
587+
addSemanticIndexes: addSemanticIndexes,
588+
));
589+
590+
/// A sliver that places multiple box children in a linear array along the main
591+
/// axis.
592+
///
593+
/// [SliverVariedExtentList] places its children in a linear array along the main
594+
/// axis starting at offset zero and without gaps. Each child is forced to have
595+
/// the returned extent of [itemExtentBuilder] in the main axis and the
596+
/// [SliverConstraints.crossAxisExtent] in the cross axis.
597+
///
598+
/// This constructor uses a list of [Widget]s to build the sliver.
599+
SliverVariedExtentList.list({
600+
super.key,
601+
required List<Widget> children,
602+
required this.itemExtentBuilder,
603+
bool addAutomaticKeepAlives = true,
604+
bool addRepaintBoundaries = true,
605+
bool addSemanticIndexes = true,
606+
}) : super(delegate: SliverChildListDelegate(
607+
children,
608+
addAutomaticKeepAlives: addAutomaticKeepAlives,
609+
addRepaintBoundaries: addRepaintBoundaries,
610+
addSemanticIndexes: addSemanticIndexes,
611+
));
612+
613+
/// The children extent builder.
614+
///
615+
/// Should return null if asked to build an item extent with a greater index than
616+
/// exists.
617+
final ItemExtentBuilder itemExtentBuilder;
618+
619+
@override
620+
RenderSliverVariedExtentList createRenderObject(BuildContext context) {
621+
final SliverMultiBoxAdaptorElement element = context as SliverMultiBoxAdaptorElement;
622+
return RenderSliverVariedExtentList(childManager: element, itemExtentBuilder: itemExtentBuilder);
623+
}
624+
625+
@override
626+
void updateRenderObject(BuildContext context, RenderSliverVariedExtentList renderObject) {
627+
renderObject.itemExtentBuilder = itemExtentBuilder;
628+
}
629+
}
630+
524631
/// A sliver that places multiple box children in a two dimensional arrangement.
525632
///
526633
/// _To learn more about slivers, see [CustomScrollView.slivers]._

packages/flutter/lib/src/widgets/sliver_varied_extent_list.dart

Lines changed: 0 additions & 141 deletions
This file was deleted.

packages/flutter/lib/widgets.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,6 @@ export 'src/widgets/sliver_fill.dart';
136136
export 'src/widgets/sliver_layout_builder.dart';
137137
export 'src/widgets/sliver_persistent_header.dart';
138138
export 'src/widgets/sliver_prototype_extent_list.dart';
139-
export 'src/widgets/sliver_varied_extent_list.dart';
140139
export 'src/widgets/slotted_render_object_widget.dart';
141140
export 'src/widgets/snapshot_widget.dart';
142141
export 'src/widgets/spacer.dart';

0 commit comments

Comments
 (0)