9551eceaed
Remove obsolete control-family flags and icon metadata across all callers, dead alternate themes and host helpers, obsolete test setup, and the unused icon-font dependency. Preserve native Material Back transitions. Make navigation fixtures independent of update plugins and answer auxiliary shell snapshots so the complete application suite can run deterministically.
176 lines
5.3 KiB
Dart
176 lines
5.3 KiB
Dart
import 'dart:typed_data';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import '../../data/chat_attachment_images.dart';
|
|
import 'chat_attachment_frame.dart';
|
|
import 'chat_image_viewer.dart';
|
|
|
|
/// The largest edge a sent image's thumbnail takes in the transcript.
|
|
const double chatThumbnailMaxWidth = 200;
|
|
const double chatThumbnailMaxHeight = 240;
|
|
const BorderRadius _radius = BorderRadius.all(Radius.circular(12));
|
|
|
|
/// One image the operator attached, drawn as a rounded thumbnail that opens
|
|
/// [ChatImageViewer]. Bytes load lazily through [images] and stay cached there;
|
|
/// a file the server no longer has becomes a small labelled placeholder.
|
|
class ChatAttachmentThumbnail extends StatefulWidget {
|
|
const ChatAttachmentThumbnail({
|
|
super.key,
|
|
required this.name,
|
|
required this.uri,
|
|
required this.images,
|
|
});
|
|
|
|
final String name;
|
|
final String uri;
|
|
final ChatAttachmentImages images;
|
|
|
|
@override
|
|
State<ChatAttachmentThumbnail> createState() =>
|
|
_ChatAttachmentThumbnailState();
|
|
}
|
|
|
|
class _ChatAttachmentThumbnailState extends State<ChatAttachmentThumbnail> {
|
|
Uint8List? _bytes;
|
|
String? _failure;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_resolve();
|
|
}
|
|
|
|
@override
|
|
void didUpdateWidget(covariant ChatAttachmentThumbnail oldWidget) {
|
|
super.didUpdateWidget(oldWidget);
|
|
if (oldWidget.uri != widget.uri || oldWidget.images != widget.images) {
|
|
_resolve();
|
|
}
|
|
}
|
|
|
|
void _resolve() {
|
|
final uri = widget.uri;
|
|
_bytes = widget.images.ready(uri);
|
|
_failure = widget.images.failure(uri);
|
|
if (_bytes != null || _failure != null) return;
|
|
widget.images
|
|
.load(uri)
|
|
.then(
|
|
(bytes) {
|
|
if (mounted && widget.uri == uri) setState(() => _bytes = bytes);
|
|
},
|
|
onError: (Object error) {
|
|
if (mounted && widget.uri == uri) {
|
|
setState(() => _failure = '$error');
|
|
}
|
|
},
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final bytes = _bytes;
|
|
if (bytes != null) return _thumbnail(context, bytes);
|
|
final failure = _failure;
|
|
if (failure != null) return _placeholder(context, failure);
|
|
return Semantics(
|
|
label: '${widget.name}, loading',
|
|
child: _pending(context),
|
|
);
|
|
}
|
|
|
|
/// A quiet surface square while loaded bytes decode, which is brief.
|
|
Widget _decoding(BuildContext context) => SizedBox.square(
|
|
dimension: 96,
|
|
child: ColoredBox(
|
|
color: Theme.of(context).colorScheme.surfaceContainerHighest,
|
|
),
|
|
);
|
|
|
|
/// A square the thumbnail occupies while its bytes load from the server.
|
|
Widget _pending(BuildContext context) => SizedBox.square(
|
|
key: const Key('chat-attachment-thumbnail-loading'),
|
|
dimension: 96,
|
|
child: const Center(child: CircularProgressIndicator()),
|
|
);
|
|
|
|
Widget _thumbnail(BuildContext context, Uint8List bytes) {
|
|
final ratio = MediaQuery.devicePixelRatioOf(context);
|
|
final image = ChatAttachmentFrame(
|
|
radius: _radius,
|
|
child: Image(
|
|
key: const Key('chat-attachment-thumbnail-image'),
|
|
// Decode at the thumbnail's size, not the photo's, while scrolling.
|
|
image: ResizeImage(
|
|
MemoryImage(bytes),
|
|
width: (chatThumbnailMaxWidth * ratio).round(),
|
|
height: (chatThumbnailMaxHeight * ratio).round(),
|
|
policy: ResizeImagePolicy.fit,
|
|
allowUpscaling: false,
|
|
),
|
|
fit: BoxFit.contain,
|
|
gaplessPlayback: true,
|
|
// Until the first frame decodes, hold the loading square's place so
|
|
// the row does not collapse and the control can still be tapped.
|
|
frameBuilder: (context, child, frame, synchronous) =>
|
|
frame == null && !synchronous ? _decoding(context) : child,
|
|
errorBuilder: (context, _, _) =>
|
|
_placeholder(context, 'Image could not be read'),
|
|
),
|
|
);
|
|
void open() =>
|
|
openChatImageViewer(context, name: widget.name, bytes: bytes);
|
|
final constrained = ConstrainedBox(
|
|
constraints: const BoxConstraints(
|
|
maxWidth: chatThumbnailMaxWidth,
|
|
maxHeight: chatThumbnailMaxHeight,
|
|
),
|
|
child: image,
|
|
);
|
|
return Semantics(
|
|
button: true,
|
|
image: true,
|
|
label: widget.name,
|
|
excludeSemantics: true,
|
|
onTap: open,
|
|
child: Material(
|
|
type: MaterialType.transparency,
|
|
child: InkWell(
|
|
key: const Key('chat-attachment-thumbnail'),
|
|
borderRadius: _radius,
|
|
onTap: open,
|
|
child: constrained,
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _placeholder(BuildContext context, String reason) {
|
|
final secondary = Theme.of(context).colorScheme.onSurfaceVariant;
|
|
return Semantics(
|
|
key: const Key('chat-attachment-thumbnail-unavailable'),
|
|
label: '${widget.name}, $reason',
|
|
excludeSemantics: true,
|
|
child: ConstrainedBox(
|
|
constraints: const BoxConstraints(maxWidth: chatThumbnailMaxWidth),
|
|
child: Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Icon(Icons.broken_image_outlined, color: secondary),
|
|
const SizedBox(width: 8),
|
|
Flexible(
|
|
child: Text(
|
|
reason,
|
|
style: DefaultTextStyle.of(
|
|
context,
|
|
).style.copyWith(color: secondary),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|