b00dff864c
Sent images and composer thumbnails share ChatAttachmentFrame: rounded corners plus a faint outline in the host's outline role, so a dark screenshot no longer melts into a dark page. Adds a web probe that renders the streaming activity line at several text sizes.
204 lines
6.1 KiB
Dart
204 lines
6.1 KiB
Dart
import 'dart:typed_data';
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
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,
|
|
required this.cupertino,
|
|
});
|
|
|
|
final String name;
|
|
final String uri;
|
|
final ChatAttachmentImages images;
|
|
final bool cupertino;
|
|
|
|
@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: widget.cupertino
|
|
? CupertinoColors.systemGrey5.resolveFrom(context)
|
|
: 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: Center(
|
|
child: widget.cupertino
|
|
? const CupertinoActivityIndicator()
|
|
: const CircularProgressIndicator.adaptive(),
|
|
),
|
|
);
|
|
|
|
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,
|
|
cupertino: widget.cupertino,
|
|
);
|
|
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: widget.cupertino
|
|
? CupertinoButton(
|
|
key: const Key('chat-attachment-thumbnail'),
|
|
padding: EdgeInsets.zero,
|
|
minimumSize: Size.zero,
|
|
onPressed: open,
|
|
child: constrained,
|
|
)
|
|
: 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 = widget.cupertino
|
|
? CupertinoColors.secondaryLabel.resolveFrom(context)
|
|
: 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(
|
|
widget.cupertino
|
|
? CupertinoIcons.photo
|
|
: Icons.broken_image_outlined,
|
|
color: secondary,
|
|
),
|
|
const SizedBox(width: 8),
|
|
Flexible(
|
|
child: Text(
|
|
reason,
|
|
style: DefaultTextStyle.of(
|
|
context,
|
|
).style.copyWith(color: secondary),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|