Rebuild only changed list rows and lay out big artifact sections lazily

Runs and chat session rows are memoized on their own data (MemoSubtree), so
a run event or a chat's running change rebuilds one row instead of every
built row. The artifact gallery's open sections are lazy slivers revealed
by a sliver open/close animation, and thumbnails decode at display size.
Benches in test/perf measure both.
This commit is contained in:
2026-11-02 00:08:29 +00:00
parent b3b761c56d
commit 7b1455631b
16 changed files with 889 additions and 152 deletions
+65
View File
@@ -0,0 +1,65 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
/// Builds [builder] once and hands Flutter the same child until [memo]
/// changes, so a list that rebuilds on every notification skips the rows
/// whose own data did not change.
///
/// [memo] must capture everything the built subtree shows or calls: the
/// row's data and every callback or controller its closures use (compare
/// callbacks by `==`, so a tear-off of the same method still matches). The
/// subtree's own inherited dependencies (theme, host, media) still rebuild
/// it directly, as they mark its elements rather than this one.
class MemoSubtree extends StatefulWidget {
const MemoSubtree({super.key, required this.memo, required this.builder});
final Object? memo;
final WidgetBuilder builder;
@override
State<MemoSubtree> createState() => _MemoSubtreeState();
}
class _MemoSubtreeState extends State<MemoSubtree> {
Widget? _child;
@override
void didUpdateWidget(MemoSubtree oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.memo != widget.memo) _child = null;
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
// A builder that read an inherited widget through this context.
_child = null;
}
@override
void reassemble() {
super.reassemble();
_child = null;
}
@override
Widget build(BuildContext context) => _child ??= widget.builder(context);
}
/// A JSON row map as a [MemoSubtree] memo: equal when it is the same map or
/// has the same entries (nested values compare by `==`, so a rebuilt nested
/// map counts as a change).
@immutable
class MemoMap {
const MemoMap(this.map);
final Map<String, dynamic>? map;
@override
bool operator ==(Object other) =>
other is MemoMap &&
(identical(map, other.map) || mapEquals(map, other.map));
@override
int get hashCode => map?.length ?? 0;
}
+152
View File
@@ -0,0 +1,152 @@
import 'dart:math' as math;
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
/// Opens and closes a sliver by growing or shrinking it from its top, the
/// sliver counterpart of a clipped `AnimatedSize` with top alignment. The
/// sliver stays lazy: only its visible part is built and laid out, however
/// long it is. Closed, it takes no space and builds nothing.
class SliverReveal extends StatefulWidget {
const SliverReveal({
super.key,
required this.open,
required this.duration,
required this.curve,
required this.sliver,
});
final bool open;
final Duration duration;
final Curve curve;
final Widget sliver;
@override
State<SliverReveal> createState() => _SliverRevealState();
}
class _SliverRevealState extends State<SliverReveal>
with SingleTickerProviderStateMixin {
late final AnimationController _controller = AnimationController(
vsync: this,
duration: widget.duration,
value: widget.open ? 1 : 0,
);
@override
void didUpdateWidget(SliverReveal oldWidget) {
super.didUpdateWidget(oldWidget);
_controller.duration = widget.duration;
if (widget.open == oldWidget.open) return;
if (widget.duration == Duration.zero) {
_controller.value = widget.open ? 1 : 0;
} else if (widget.open) {
_controller.forward();
} else {
_controller.reverse();
}
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) => AnimatedBuilder(
animation: _controller,
builder: (context, sliver) {
if (_controller.isDismissed) return const SliverToBoxAdapter();
final value = _controller.value;
return _SliverExtentFactor(
factor: value >= 1 ? 1 : widget.curve.transform(value),
sliver: sliver,
);
},
child: widget.sliver,
);
}
class _SliverExtentFactor extends SingleChildRenderObjectWidget {
const _SliverExtentFactor({required this.factor, Widget? sliver})
: super(child: sliver);
final double factor;
@override
RenderObject createRenderObject(BuildContext context) =>
_RenderSliverExtentFactor(factor);
@override
void updateRenderObject(
BuildContext context,
_RenderSliverExtentFactor renderObject,
) => renderObject.factor = factor;
}
/// Lays its sliver out normally and reports [factor] of its extent, clipping
/// what lies beyond.
class _RenderSliverExtentFactor extends RenderProxySliver {
_RenderSliverExtentFactor(this._factor);
double _factor;
set factor(double value) {
if (value == _factor) return;
_factor = value;
markNeedsLayout();
}
bool get _clipped => _factor < 1;
@override
void performLayout() {
final child = this.child!;
child.layout(constraints, parentUsesSize: true);
final full = child.geometry!;
if (!_clipped || full.scrollOffsetCorrection != null) {
geometry = full;
return;
}
final extent = full.scrollExtent * _factor;
final paintExtent = math.min(
calculatePaintOffset(constraints, from: 0, to: extent),
full.paintExtent,
);
geometry = SliverGeometry(
scrollExtent: extent,
paintExtent: paintExtent,
layoutExtent: paintExtent,
maxPaintExtent: extent,
cacheExtent: calculateCacheOffset(constraints, from: 0, to: extent),
hasVisualOverflow: true,
);
}
@override
void paint(PaintingContext context, Offset offset) {
if (child == null || geometry!.paintExtent <= 0) return;
if (!_clipped) 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();
}
}
+34 -17
View File
@@ -7,6 +7,7 @@ import '../adaptive/host.dart';
import '../theme/app_theme.dart';
import '../motion/motion.dart';
import 'section_heading.dart';
import 'sliver_reveal.dart';
/// A collapsible section for a [CustomScrollView]: its title stays pinned at
/// the top while its content scrolls under it, so it can always be closed
@@ -17,17 +18,22 @@ class StickySection extends StatelessWidget {
required this.title,
required this.open,
required this.onToggle,
required this.body,
this.body,
this.sliverBody,
this.trailing = const [],
this.titleColor,
});
}) : assert((body == null) != (sliverBody == null));
final String title;
final bool open;
final VoidCallback onToggle;
/// The content shown while open (a box widget).
final Widget body;
final Widget? body;
/// The content shown while open as a sliver, in place of [body]: a long
/// body (hundreds of items) then builds and lays out only what is visible.
final Widget? sliverBody;
/// Small actions beside the title (a count, a copy button).
final List<Widget> trailing;
@@ -54,18 +60,30 @@ class StickySection extends StatelessWidget {
),
// The content grows and shrinks under the pinned title, on its own
// surface with the title's accent continuing down its edge.
SliverToBoxAdapter(
child: ClipRect(
child: AnimatedSize(
duration: Motion.of(context, Motion.standard),
curve: Motion.curve,
alignment: Alignment.topCenter,
child: open
? _SectionBody(child: body)
: const SizedBox(width: double.infinity),
if (sliverBody case final sliver?)
SliverReveal(
open: open,
duration: Motion.of(context, Motion.standard),
curve: Motion.curve,
// The body's padding; its rows bring their own ink surface.
sliver: SliverPadding(
padding: _SectionBody.padding,
sliver: sliver,
),
)
else
SliverToBoxAdapter(
child: ClipRect(
child: AnimatedSize(
duration: Motion.of(context, Motion.standard),
curve: Motion.curve,
alignment: Alignment.topCenter,
child: open
? _SectionBody(child: body!)
: const SizedBox(width: double.infinity),
),
),
),
),
],
);
}
@@ -254,12 +272,11 @@ class _SectionBody extends StatelessWidget {
final Widget child;
static const padding = EdgeInsets.only(top: 12, bottom: 4);
@override
Widget build(BuildContext context) {
final content = Padding(
padding: const EdgeInsets.only(top: 12, bottom: 4),
child: child,
);
final content = Padding(padding: padding, child: child);
if (usesCupertinoHost(context)) {
return content;
}