From 24e465e521fcc2048ce4ae167486fcc054db5e25 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Fri, 23 Oct 2026 19:00:39 +0100 Subject: [PATCH] feat(app): a pasted or picked image shows a thumbnail in the chat composer --- .../chat/conversation/chat_composer_bar.dart | 102 +++++++++++++++++- .../chat_composer_paste_test.dart | 5 + 2 files changed, 106 insertions(+), 1 deletion(-) diff --git a/app/lib/features/chat/conversation/chat_composer_bar.dart b/app/lib/features/chat/conversation/chat_composer_bar.dart index 7e119805..bf8bd5cc 100644 --- a/app/lib/features/chat/conversation/chat_composer_bar.dart +++ b/app/lib/features/chat/conversation/chat_composer_bar.dart @@ -616,7 +616,9 @@ class _ChatComposerBarState extends State { 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 { ); } + /// 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('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('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( + '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('chat-composer-attachment-$index'), label: Text(attachment.name), diff --git a/app/test/features/chat/conversation/chat_composer_paste_test.dart b/app/test/features/chat/conversation/chat_composer_paste_test.dart index e100c40e..0a6bcb01 100644 --- a/app/test/features/chat/conversation/chat_composer_paste_test.dart +++ b/app/test/features/chat/conversation/chat_composer_paste_test.dart @@ -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('chat-composer-thumbnail-0')), + findsOneWidget, + ); expect(fake.listens, 1); expect(fake.stops, 0); });