import 'dart:math' as math; import 'dart:ui' show lerpDouble; import 'package:flutter/rendering.dart'; import 'package:flutter/widgets.dart'; /// The sliver counterpart of a clipped, top-aligned `AnimatedSize`: when /// [changeToken] changes, the sliver's extent moves from what it showed to /// its child's new extent over [duration]. The child is laid out (and drawn) /// at its new extent at once and clipped to the moving one, so a closing /// section (an empty child) leaves blank space that shrinks away. The child /// stays lazy: only its visible part is built. /// /// The token, rather than every change of the child's extent, starts the /// animation: a lazy list's extent is an estimate that moves while it /// scrolls, and that must not animate. class SliverAnimatedExtent extends StatefulWidget { const SliverAnimatedExtent({ super.key, required this.duration, required this.curve, required this.changeToken, required this.sliver, }); final Duration duration; final Curve curve; final Object? changeToken; final Widget sliver; @override State createState() => _SliverAnimatedExtentState(); } class _SliverAnimatedExtentState extends State with SingleTickerProviderStateMixin { late final AnimationController _controller = AnimationController( vsync: this, duration: widget.duration, value: 1, ); int _generation = 0; @override void didUpdateWidget(SliverAnimatedExtent oldWidget) { super.didUpdateWidget(oldWidget); _controller.duration = widget.duration; if (widget.changeToken == oldWidget.changeToken) return; if (widget.duration == Duration.zero) { _controller.value = 1; return; } _generation++; _controller.forward(from: 0); } @override void dispose() { _controller.dispose(); super.dispose(); } @override Widget build(BuildContext context) => AnimatedBuilder( animation: _controller, builder: (context, sliver) => _SliverExtentLerp( generation: _generation, progress: _controller.isCompleted ? 1 : widget.curve.transform(_controller.value), sliver: sliver, ), child: widget.sliver, ); } class _SliverExtentLerp extends SingleChildRenderObjectWidget { const _SliverExtentLerp({ required this.generation, required this.progress, Widget? sliver, }) : super(child: sliver); final int generation; final double progress; @override RenderObject createRenderObject(BuildContext context) => _RenderSliverExtentLerp(generation, progress); @override void updateRenderObject( BuildContext context, _RenderSliverExtentLerp renderObject, ) => renderObject ..generation = generation ..progress = progress; } /// Reports an extent between the one it last reported when [generation] /// changed and its child's extent, by [progress], clipping the child to it. class _RenderSliverExtentLerp extends RenderProxySliver { _RenderSliverExtentLerp(this._generation, this._progress); int _generation; set generation(int value) { if (value == _generation) return; _generation = value; _from = _shown; markNeedsLayout(); } double _progress; set progress(double value) { if (value == _progress) return; _progress = value; markNeedsLayout(); } /// The extent reported by the last layout, and where the current /// animation started. double _shown = 0; double _from = 0; bool _clipped = false; @override void performLayout() { final child = this.child!; child.layout(constraints, parentUsesSize: true); final full = child.geometry!; _clipped = false; if (_progress >= 1 || full.scrollOffsetCorrection != null) { geometry = full; _shown = full.scrollExtent; return; } final extent = math.max( 0.0, lerpDouble(_from, full.scrollExtent, _progress)!, ); _shown = extent; _clipped = extent < full.scrollExtent; final paintExtent = calculatePaintOffset(constraints, from: 0, to: extent); geometry = SliverGeometry( scrollExtent: extent, paintExtent: paintExtent, layoutExtent: paintExtent, maxPaintExtent: extent, cacheExtent: calculateCacheOffset(constraints, from: 0, to: extent), hasVisualOverflow: _clipped || full.hasVisualOverflow, ); } @override bool hitTestChildren( SliverHitTestResult result, { required double mainAxisPosition, required double crossAxisPosition, }) { if (mainAxisPosition >= geometry!.paintExtent) return false; return super.hitTestChildren( result, mainAxisPosition: mainAxisPosition, crossAxisPosition: crossAxisPosition, ); } @override void paint(PaintingContext context, Offset offset) { if (child == null || geometry!.paintExtent <= 0) return; if (!_clipped) { _clip.layer = null; return super.paint(context, offset); } final extent = geometry!.paintExtent; final cross = constraints.crossAxisExtent; final clip = switch (constraints.axis) { Axis.vertical => Rect.fromLTWH(0, 0, cross, extent), Axis.horizontal => Rect.fromLTWH(0, 0, extent, cross), }; _clip.layer = context.pushClipRect( needsCompositing, offset, clip, super.paint, oldLayer: _clip.layer, ); } final _clip = LayerHandle(); @override void dispose() { _clip.layer = null; super.dispose(); } }