Perf round 4: lazy chat sessions list
Chat sections are slivers: a lazy, keyed SliverList under the same heading, with SliverAnimatedExtent reproducing the clipped AnimatedSize for open, close and arriving rows. The first Chat tab visit no longer builds every row (150 chats: worst build frame 116 ms to 19 ms).
This commit is contained in:
@@ -4,14 +4,22 @@ import 'package:flutter/material.dart';
|
||||
import '../adaptive/host.dart';
|
||||
import '../motion/motion.dart';
|
||||
import 'section_heading.dart';
|
||||
import 'sliver_animated_extent.dart';
|
||||
|
||||
/// A titled group in a list: a [SectionHeading] strip (bold title, count,
|
||||
/// chevron) that reads as a divider, and opens or closes its rows on a tap.
|
||||
class CollapsibleListSection extends StatefulWidget {
|
||||
const CollapsibleListSection({
|
||||
/// A titled group in a [CustomScrollView]: a [SectionHeading] strip (bold
|
||||
/// title, count, chevron) that reads as a divider, and opens or closes its
|
||||
/// rows on a tap.
|
||||
///
|
||||
/// The rows are a lazy sliver list: only the visible ones are built, however
|
||||
/// many there are. The section grows and shrinks like a clipped, top-aligned
|
||||
/// `AnimatedSize` when it opens or closes and when rows arrive or leave.
|
||||
class SliverCollapsibleListSection extends StatefulWidget {
|
||||
const SliverCollapsibleListSection({
|
||||
super.key,
|
||||
required this.title,
|
||||
required this.children,
|
||||
required this.itemCount,
|
||||
required this.itemBuilder,
|
||||
this.itemKey,
|
||||
this.count,
|
||||
this.initiallyOpen = true,
|
||||
});
|
||||
@@ -19,22 +27,70 @@ class CollapsibleListSection extends StatefulWidget {
|
||||
final String title;
|
||||
final int? count;
|
||||
final bool initiallyOpen;
|
||||
final List<Widget> children;
|
||||
final int itemCount;
|
||||
final IndexedWidgetBuilder itemBuilder;
|
||||
|
||||
/// The key of the item at an index, so a row keeps its element when rows
|
||||
/// before it arrive or leave.
|
||||
final Key Function(int index)? itemKey;
|
||||
|
||||
@override
|
||||
State<CollapsibleListSection> createState() => _CollapsibleListSectionState();
|
||||
State<SliverCollapsibleListSection> createState() =>
|
||||
_SliverCollapsibleListSectionState();
|
||||
}
|
||||
|
||||
class _CollapsibleListSectionState extends State<CollapsibleListSection> {
|
||||
class _SliverCollapsibleListSectionState
|
||||
extends State<SliverCollapsibleListSection> {
|
||||
late bool _open = widget.initiallyOpen;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final duration = Motion.of(context, Motion.standard);
|
||||
return SliverMainAxisGroup(
|
||||
slivers: [
|
||||
SliverToBoxAdapter(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.only(top: 8),
|
||||
child: _heading(context),
|
||||
),
|
||||
),
|
||||
SliverAnimatedExtent(
|
||||
duration: duration,
|
||||
curve: Motion.curve,
|
||||
changeToken: (_open, widget.itemCount),
|
||||
sliver: _open ? _rows() : const SliverToBoxAdapter(),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _rows() {
|
||||
final itemKey = widget.itemKey;
|
||||
Map<Key, int>? indexOf;
|
||||
return SliverList(
|
||||
delegate: SliverChildBuilderDelegate(
|
||||
(context, index) => itemKey == null
|
||||
? widget.itemBuilder(context, index)
|
||||
: KeyedSubtree(
|
||||
key: itemKey(index),
|
||||
child: widget.itemBuilder(context, index),
|
||||
),
|
||||
childCount: widget.itemCount,
|
||||
findChildIndexCallback: itemKey == null
|
||||
? null
|
||||
: (key) => (indexOf ??= {
|
||||
for (var i = 0; i < widget.itemCount; i++) itemKey(i): i,
|
||||
})[key],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _heading(BuildContext context) {
|
||||
final cupertino = usesCupertinoHost(context);
|
||||
final muted = cupertino
|
||||
? CupertinoColors.secondaryLabel.resolveFrom(context)
|
||||
: Theme.of(context).colorScheme.onSurfaceVariant;
|
||||
final heading = Semantics(
|
||||
return Semantics(
|
||||
container: true,
|
||||
header: true,
|
||||
button: true,
|
||||
@@ -73,31 +129,6 @@ class _CollapsibleListSectionState extends State<CollapsibleListSection> {
|
||||
),
|
||||
),
|
||||
);
|
||||
final rows = _open
|
||||
? Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: widget.children,
|
||||
)
|
||||
: const SizedBox(width: double.infinity);
|
||||
final duration = Motion.of(context, Motion.standard);
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
Padding(padding: const EdgeInsets.only(top: 8), child: heading),
|
||||
ClipRect(
|
||||
// A zero-duration AnimatedSize can notify during its own layout when
|
||||
// rows arrive asynchronously. Reduced motion should skip it entirely.
|
||||
child: duration == Duration.zero
|
||||
? rows
|
||||
: AnimatedSize(
|
||||
duration: duration,
|
||||
curve: Motion.curve,
|
||||
alignment: Alignment.topCenter,
|
||||
child: rows,
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
void _toggle() => setState(() => _open = !_open);
|
||||
|
||||
@@ -0,0 +1,197 @@
|
||||
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<SliverAnimatedExtent> createState() => _SliverAnimatedExtentState();
|
||||
}
|
||||
|
||||
class _SliverAnimatedExtentState extends State<SliverAnimatedExtent>
|
||||
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<ClipRectLayer>();
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_clip.layer = null;
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user