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:
@@ -7,6 +7,7 @@ import '../domain/artifact_kind.dart';
|
||||
import '../domain/artifact_record.dart';
|
||||
import '../domain/artifact_url.dart';
|
||||
import 'artifact_row_labels.dart';
|
||||
import 'artifact_thumbnail_image.dart';
|
||||
import 'artifact_viewer_fetch.dart';
|
||||
|
||||
/// One artifact in the gallery grid: a thumbnail for an image, otherwise an
|
||||
@@ -139,9 +140,15 @@ class ArtifactThumbnail extends StatefulWidget {
|
||||
State<ArtifactThumbnail> createState() => _ArtifactThumbnailState();
|
||||
}
|
||||
|
||||
class _ArtifactThumbnailState extends State<ArtifactThumbnail> {
|
||||
class _ArtifactThumbnailState extends State<ArtifactThumbnail>
|
||||
with AutomaticKeepAliveClientMixin {
|
||||
Future<ArtifactFetchResult>? _image;
|
||||
|
||||
/// A loaded picture stays alive when a lazy gallery scrolls it away, so
|
||||
/// scrolling back does not fetch it again.
|
||||
@override
|
||||
bool get wantKeepAlive => _image != null;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
@@ -160,6 +167,7 @@ class _ArtifactThumbnailState extends State<ArtifactThumbnail> {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
super.build(context);
|
||||
final cupertino = usesCupertinoHost(context);
|
||||
final icon = Icon(
|
||||
artifactKindIcon(widget.artifact, cupertino: cupertino),
|
||||
@@ -176,11 +184,25 @@ class _ArtifactThumbnailState extends State<ArtifactThumbnail> {
|
||||
if (result == null || !result.ok || result.bytes.isEmpty) {
|
||||
return Center(child: icon);
|
||||
}
|
||||
return Image.memory(
|
||||
result.bytes,
|
||||
fit: BoxFit.cover,
|
||||
gaplessPlayback: true,
|
||||
errorBuilder: (_, _, _) => Center(child: icon),
|
||||
return LayoutBuilder(
|
||||
builder: (context, constraints) {
|
||||
final ratio = MediaQuery.devicePixelRatioOf(context);
|
||||
// Decoded at the size shown, not the picture's full size.
|
||||
final sized =
|
||||
constraints.hasBoundedWidth && constraints.hasBoundedHeight
|
||||
? CoverSizedMemoryImage(
|
||||
result.bytes,
|
||||
width: (constraints.maxWidth * ratio).ceil(),
|
||||
height: (constraints.maxHeight * ratio).ceil(),
|
||||
)
|
||||
: null;
|
||||
return Image(
|
||||
image: sized ?? MemoryImage(result.bytes),
|
||||
fit: BoxFit.cover,
|
||||
gaplessPlayback: true,
|
||||
errorBuilder: (_, _, _) => Center(child: icon),
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user