feat(app): a pasted or picked image shows a thumbnail in the chat composer

This commit is contained in:
2026-10-23 19:00:39 +01:00
parent 6265b5d1cc
commit 24e465e521
2 changed files with 106 additions and 1 deletions
@@ -616,7 +616,9 @@ class _ChatComposerBarState extends State<ChatComposerBar> {
runSpacing: 4,
children: [
for (var index = 0; index < attachments.length; index += 1)
if (cupertino)
if (_imageBytes(index) case final bytes?)
_imageThumbnail(context, attachments[index], bytes, index)
else if (cupertino)
_cupertinoChip(context, attachments[index], index)
else
_materialChip(attachments[index], index),
@@ -625,6 +627,104 @@ class _ChatComposerBarState extends State<ChatComposerBar> {
);
}
/// The pasted or picked bytes of an image attachment, or null for any other
/// file.
Uint8List? _imageBytes(int index) {
final attachment = _attachments.attachments[index];
if (!attachment.mime.toLowerCase().startsWith('image/')) return null;
if (index >= _bytes.length || _bytes[index].isEmpty) return null;
return _bytes[index];
}
/// An image attachment as a small thumbnail with its remove control in the
/// corner; its name is the accessible label.
Widget _imageThumbnail(
BuildContext context,
ChatAttachment attachment,
Uint8List bytes,
int index,
) {
final cupertino =
chatControlsStyleOf(context) == ChatControlsStyle.cupertino;
return Semantics(
key: ValueKey<String>('chat-composer-attachment-$index'),
container: true,
explicitChildNodes: true,
label: attachment.name,
image: true,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
SizedBox.square(
dimension: 72,
child: Stack(
clipBehavior: Clip.none,
children: [
Positioned.fill(
child: ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Image.memory(
bytes,
key: ValueKey<String>('chat-composer-thumbnail-$index'),
fit: BoxFit.cover,
errorBuilder: (_, _, _) => Center(
child: Icon(
cupertino
? CupertinoIcons.photo
: Icons.image_outlined,
),
),
),
),
),
Positioned(
top: -6,
right: -6,
child: Semantics(
button: true,
label: 'Remove ${attachment.name}',
onTap: () => _removeAttachmentAt(index),
child: ExcludeSemantics(
child: GestureDetector(
key: ValueKey<String>(
'chat-composer-attachment-remove-$index',
),
onTap: () => _removeAttachmentAt(index),
child: Container(
decoration: const BoxDecoration(
color: Color(0xCC000000),
shape: BoxShape.circle,
),
padding: const EdgeInsets.all(2),
child: Icon(
cupertino ? CupertinoIcons.xmark : Icons.close,
size: 14,
color: const Color(0xFFFFFFFF),
),
),
),
),
),
),
],
),
),
// The name under the picture, one line.
SizedBox(
width: 72,
child: Text(
attachment.name,
maxLines: 1,
overflow: TextOverflow.ellipsis,
textAlign: TextAlign.center,
style: const TextStyle(fontSize: 11),
),
),
],
),
);
}
Widget _materialChip(ChatAttachment attachment, int index) => Chip(
key: ValueKey<String>('chat-composer-attachment-$index'),
label: Text(attachment.name),
@@ -121,6 +121,11 @@ void main() {
expect(_chips, findsOneWidget);
expect(find.text('shot.png'), findsOneWidget);
// A pasted image shows its picture, not only its name.
expect(
find.byKey(const ValueKey<String>('chat-composer-thumbnail-0')),
findsOneWidget,
);
expect(fake.listens, 1);
expect(fake.stops, 0);
});