Merge main
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
import 'dart:math' as math;
|
||||
import 'dart:ui' as ui;
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/painting.dart';
|
||||
|
||||
/// An image's bytes decoded no larger than needed to cover a box of
|
||||
/// [width] x [height] device pixels (as `BoxFit.cover` shows it), keeping the
|
||||
/// image's aspect ratio and never upscaling. A gallery of full-size pictures
|
||||
/// then holds thumbnails, not originals, in memory.
|
||||
@immutable
|
||||
class CoverSizedMemoryImage extends ImageProvider<CoverSizedMemoryImage> {
|
||||
const CoverSizedMemoryImage(
|
||||
this.bytes, {
|
||||
required this.width,
|
||||
required this.height,
|
||||
});
|
||||
|
||||
final Uint8List bytes;
|
||||
final int width;
|
||||
final int height;
|
||||
|
||||
@override
|
||||
Future<CoverSizedMemoryImage> obtainKey(ImageConfiguration configuration) =>
|
||||
SynchronousFuture<CoverSizedMemoryImage>(this);
|
||||
|
||||
@override
|
||||
ImageStreamCompleter loadImage(
|
||||
CoverSizedMemoryImage key,
|
||||
ImageDecoderCallback decode,
|
||||
) => MultiFrameImageStreamCompleter(
|
||||
codec: _decode(key, decode),
|
||||
scale: 1,
|
||||
debugLabel: 'CoverSizedMemoryImage(${describeIdentity(key.bytes)})',
|
||||
);
|
||||
|
||||
static Future<ui.Codec> _decode(
|
||||
CoverSizedMemoryImage key,
|
||||
ImageDecoderCallback decode,
|
||||
) async {
|
||||
final buffer = await ui.ImmutableBuffer.fromUint8List(key.bytes);
|
||||
return decode(
|
||||
buffer,
|
||||
getTargetSize: (intrinsicWidth, intrinsicHeight) => coverTargetSize(
|
||||
intrinsicWidth,
|
||||
intrinsicHeight,
|
||||
width: key.width,
|
||||
height: key.height,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) =>
|
||||
other is CoverSizedMemoryImage &&
|
||||
identical(other.bytes, bytes) &&
|
||||
other.width == width &&
|
||||
other.height == height;
|
||||
|
||||
@override
|
||||
int get hashCode => Object.hash(identityHashCode(bytes), width, height);
|
||||
}
|
||||
|
||||
/// The decode size that still covers a [width] x [height] box: the picture's
|
||||
/// aspect ratio scaled down until its shorter side fits, never scaled up.
|
||||
ui.TargetImageSize coverTargetSize(
|
||||
int intrinsicWidth,
|
||||
int intrinsicHeight, {
|
||||
required int width,
|
||||
required int height,
|
||||
}) {
|
||||
final scale = math.max(width / intrinsicWidth, height / intrinsicHeight);
|
||||
if (scale >= 1) return const ui.TargetImageSize();
|
||||
return ui.TargetImageSize(
|
||||
width: math.max(1, (intrinsicWidth * scale).ceil()),
|
||||
height: math.max(1, (intrinsicHeight * scale).ceil()),
|
||||
);
|
||||
}
|
||||
@@ -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),
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import 'dart:math' as math;
|
||||
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
@@ -143,19 +145,25 @@ class _ArtifactVisitListState extends State<ArtifactVisitList> {
|
||||
key: key,
|
||||
title: artifactGroupLabel(group),
|
||||
count: group.artifacts.length,
|
||||
body: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
// Lazy: a visit with hundreds of artifacts builds and lays out
|
||||
// only the rows on screen.
|
||||
body: SliverMainAxisGroup(
|
||||
slivers: [
|
||||
if (group.artifacts.isNotEmpty)
|
||||
_mode == ArtifactGalleryMode.grid
|
||||
? _grid(group)
|
||||
: _list(group),
|
||||
if (widget.showTurnDocuments && group.documents.isNotEmpty)
|
||||
ArtifactDocumentSection(
|
||||
group: group,
|
||||
selectedIdentity: widget.selectedIdentity,
|
||||
onSelected: widget.onSelected,
|
||||
showHeader: false,
|
||||
SliverToBoxAdapter(
|
||||
child: _inkSurface(
|
||||
context,
|
||||
ArtifactDocumentSection(
|
||||
group: group,
|
||||
selectedIdentity: widget.selectedIdentity,
|
||||
onSelected: widget.onSelected,
|
||||
showHeader: false,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
@@ -194,7 +202,7 @@ class _ArtifactVisitListState extends State<ArtifactVisitList> {
|
||||
child: Text('$count'),
|
||||
),
|
||||
],
|
||||
body: body,
|
||||
sliverBody: body,
|
||||
);
|
||||
|
||||
Widget _modeSwitch(BuildContext context) {
|
||||
@@ -241,70 +249,89 @@ class _ArtifactVisitListState extends State<ArtifactVisitList> {
|
||||
);
|
||||
}
|
||||
|
||||
/// Rows keep a Material surface for their ink, as the section body's own
|
||||
/// surface was when it was one box around every row.
|
||||
Widget _inkSurface(BuildContext context, Widget child) =>
|
||||
usesCupertinoHost(context)
|
||||
? child
|
||||
: Material(type: MaterialType.transparency, child: child);
|
||||
|
||||
/// The list view: one row per artifact with a small thumbnail beside it.
|
||||
Widget _list(ArtifactGroup group) => Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
for (var index = 0; index < group.artifacts.length; index += 1)
|
||||
Widget _list(ArtifactGroup group) => SliverList.builder(
|
||||
itemCount: group.artifacts.length,
|
||||
itemBuilder: (context, index) {
|
||||
final artifact = group.artifacts[index];
|
||||
final identity = artifactVisitIdentity(group, artifact);
|
||||
return _inkSurface(
|
||||
context,
|
||||
ArtifactRecordRow(
|
||||
key: ValueKey<String>(
|
||||
'${artifactVisitIdentity(group, group.artifacts[index])}'
|
||||
'\u0000$index',
|
||||
),
|
||||
key: ValueKey<String>('$identity\u0000$index'),
|
||||
group: group,
|
||||
artifact: group.artifacts[index],
|
||||
selected:
|
||||
widget.selectedIdentity ==
|
||||
artifactVisitIdentity(group, group.artifacts[index]),
|
||||
artifact: artifact,
|
||||
selected: widget.selectedIdentity == identity,
|
||||
onSelected: widget.onSelected,
|
||||
leading: ClipRRect(
|
||||
borderRadius: BorderRadius.circular(6),
|
||||
child: SizedBox.square(
|
||||
dimension: 44,
|
||||
child: ArtifactThumbnail(
|
||||
artifact: group.artifacts[index],
|
||||
artifact: artifact,
|
||||
configuredEndpoint: widget.configuredEndpoint,
|
||||
fetch: widget.fetch,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
/// A group's artifacts as a grid: two tiles across on a phone, more as the
|
||||
/// pane widens.
|
||||
Widget _grid(ArtifactGroup group) => LayoutBuilder(
|
||||
/// pane widens. Each line of tiles is one lazy item, top-aligned like the
|
||||
/// runs of a wrap.
|
||||
Widget _grid(ArtifactGroup group) => SliverLayoutBuilder(
|
||||
builder: (context, constraints) {
|
||||
const gap = 12.0;
|
||||
final width = constraints.maxWidth - 32;
|
||||
final width = constraints.crossAxisExtent - 32;
|
||||
final columns = (width / 170).floor().clamp(2, 6);
|
||||
final tile = (width - gap * (columns - 1)) / columns;
|
||||
return Padding(
|
||||
final artifacts = group.artifacts;
|
||||
return SliverPadding(
|
||||
padding: const EdgeInsets.fromLTRB(16, 4, 16, 8),
|
||||
child: Wrap(
|
||||
spacing: gap,
|
||||
runSpacing: gap,
|
||||
children: [
|
||||
for (var index = 0; index < group.artifacts.length; index += 1)
|
||||
SizedBox(
|
||||
width: tile,
|
||||
child: ArtifactTile(
|
||||
key: ValueKey<String>(
|
||||
'${artifactVisitIdentity(group, group.artifacts[index])}'
|
||||
'\u0000$index',
|
||||
),
|
||||
group: group,
|
||||
artifact: group.artifacts[index],
|
||||
selected:
|
||||
widget.selectedIdentity ==
|
||||
artifactVisitIdentity(group, group.artifacts[index]),
|
||||
configuredEndpoint: widget.configuredEndpoint,
|
||||
fetch: widget.fetch,
|
||||
onSelected: widget.onSelected,
|
||||
),
|
||||
sliver: SliverList.builder(
|
||||
itemCount: (artifacts.length + columns - 1) ~/ columns,
|
||||
itemBuilder: (context, line) {
|
||||
final first = line * columns;
|
||||
final end = math.min(first + columns, artifacts.length);
|
||||
return Padding(
|
||||
padding: EdgeInsets.only(top: line == 0 ? 0 : gap),
|
||||
child: Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
for (var index = first; index < end; index += 1) ...[
|
||||
if (index > first) const SizedBox(width: gap),
|
||||
SizedBox(
|
||||
width: tile,
|
||||
child: ArtifactTile(
|
||||
key: ValueKey<String>(
|
||||
'${artifactVisitIdentity(group, artifacts[index])}'
|
||||
'\u0000$index',
|
||||
),
|
||||
group: group,
|
||||
artifact: artifacts[index],
|
||||
selected:
|
||||
widget.selectedIdentity ==
|
||||
artifactVisitIdentity(group, artifacts[index]),
|
||||
configuredEndpoint: widget.configuredEndpoint,
|
||||
fetch: widget.fetch,
|
||||
onSelected: widget.onSelected,
|
||||
),
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user