From 5aa6b7f90aab07dfcdfdd16954b7dcd7da8fc5ae Mon Sep 17 00:00:00 2001 From: Karol Kulesza Date: Tue, 3 Nov 2026 21:58:57 +0100 Subject: [PATCH 1/3] feat(a2ui_flutter): render data: image URLs in Image web_core accepts data: media URLs since #3108, but the Flutter Image showed the broken-image icon for anything other than http(s). Decode a data: URL of an image/* type into a MemoryImage that compares by URL, so a rebuild reuses the cached decode. Other types and undecodable content keep the broken-image icon. --- dart/a2ui_flutter/CHANGELOG.md | 4 + .../lib/src/catalog/basic/image.dart | 56 +++++++-- .../test/catalog/basic/image_test.dart | 117 ++++++++++++++++++ 3 files changed, 169 insertions(+), 8 deletions(-) diff --git a/dart/a2ui_flutter/CHANGELOG.md b/dart/a2ui_flutter/CHANGELOG.md index 4405700853..096c2f78e7 100644 --- a/dart/a2ui_flutter/CHANGELOG.md +++ b/dart/a2ui_flutter/CHANGELOG.md @@ -37,6 +37,10 @@ `UNALLOWED_CHILD`. The basic catalog components keep the constraints of their catalog definition ([#3533](https://github.com/a2ui-project/a2ui/issues/3533)). +- `Image` renders a `data:` URL of an `image/*` type, such as an inline + base64 PNG, as the web renderers do since + [#3108](https://github.com/a2ui-project/a2ui/pull/3108). A `data:` URL of + another type, or one that does not decode, shows the broken-image icon. - `Image` keeps its `description` when it does not load: the broken-image icon is labelled with it, as an `alt` text is on the web ([#3412](https://github.com/a2ui-project/a2ui/pull/3412)). diff --git a/dart/a2ui_flutter/lib/src/catalog/basic/image.dart b/dart/a2ui_flutter/lib/src/catalog/basic/image.dart index e2bf4f4fb9..7e65d489a1 100644 --- a/dart/a2ui_flutter/lib/src/catalog/basic/image.dart +++ b/dart/a2ui_flutter/lib/src/catalog/basic/image.dart @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +import 'dart:typed_data'; + import 'package:a2ui_core/a2ui_core.dart'; // ignore: deprecated_member_use, see https://github.com/a2ui-project/a2ui/issues/3235 import 'package:flutter/material.dart'; @@ -20,9 +22,10 @@ import '../../component_implementation.dart'; import '../../component_props.dart'; /// Builds the basic `Image` component: the image at an `http` or `https` -/// `url`, sized by `variant`, resized by `fit` (`cover` for a header without -/// one) and labelled for accessibility by `description`. Any other URL, and -/// an image that fails to load, shows a broken-image icon labelled by the +/// `url`, or inline in a `data:` URL of an `image/*` type, sized by +/// `variant`, resized by `fit` (`cover` for a header without one) and +/// labelled for accessibility by `description`. Any other URL, and an image +/// that fails to load or decode, shows a broken-image icon labelled by the /// same `description`. Widget buildImage( BuildContext context, @@ -43,17 +46,21 @@ Widget buildImage( label: description, child: const Icon(Icons.broken_image), ); - final Widget image = url.startsWith('http://') || url.startsWith('https://') - ? Image.network( - url, + final ImageProvider? provider = + url.startsWith('http://') || url.startsWith('https://') + ? NetworkImage(url) + : _DataUrlImage.tryParse(url); + final Widget image = provider == null + ? broken + : Image( + image: provider, fit: BoxFit.values.asNameMap()[props.string('fit')] ?? (variant == 'header' ? BoxFit.cover : BoxFit.fill), semanticLabel: description, excludeFromSemantics: description.isEmpty, errorBuilder: (context, error, stackTrace) => broken, - ) - : broken; + ); return switch (variant) { 'icon' => _atStart(SizedBox.square(dimension: 24, child: image)), 'avatar' => _atStart( @@ -81,3 +88,36 @@ Widget _atStart(Widget child) => Align( heightFactor: 1, child: child, ); + +/// The image inline in a `data:` URL. +/// +/// Two are equal when their URLs are, unlike [MemoryImage], so a rebuild with +/// the same URL finds the decoded image in the image cache. +class _DataUrlImage extends MemoryImage { + _DataUrlImage._(this._url, super.bytes); + + /// The image in [url], or null when [url] is not a valid `data:` URL of an + /// `image/*` type. + static _DataUrlImage? tryParse(String url) { + final UriData? data = Uri.tryParse(url)?.data; + if (data == null || !data.mimeType.toLowerCase().startsWith('image/')) { + return null; + } + final Uint8List bytes; + try { + bytes = data.contentAsBytes(); + } on FormatException { + return null; + } + return _DataUrlImage._(url, bytes); + } + + final String _url; + + @override + bool operator ==(Object other) => + other is _DataUrlImage && other._url == _url && other.scale == scale; + + @override + int get hashCode => Object.hash(_url, scale); +} diff --git a/dart/a2ui_flutter/test/catalog/basic/image_test.dart b/dart/a2ui_flutter/test/catalog/basic/image_test.dart index e7285bdd43..edd8e92fe9 100644 --- a/dart/a2ui_flutter/test/catalog/basic/image_test.dart +++ b/dart/a2ui_flutter/test/catalog/basic/image_test.dart @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +import 'dart:convert'; + import 'package:a2ui_flutter/a2ui_flutter.dart'; import 'package:a2ui_flutter/v0_9/basic_catalog.dart'; // ignore: deprecated_member_use, see https://github.com/a2ui-project/a2ui/issues/3235 @@ -33,6 +35,12 @@ const String _missing = 'https://images.test/missing.png'; final Map> _images = {_wide: widePng, _tall: tallPng}; +/// A 1x1 PNG. +const String _pixelPng = + 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB' + '0C8AAAAASUVORK5CYII='; +const String _pixelDataUrl = 'data:image/png;base64,$_pixelPng'; + /// Runs [body] with network images served from [_images] and an empty image /// cache. void _testImages( @@ -192,6 +200,115 @@ void main() { expect(harness.errors, isEmpty); }); + group('data URL', () { + _testImages('renders an image/* data URL from its bytes', ( + tester, + server, + ) async { + final SurfaceHarness harness = await _pumpImages(tester, [ + _image({'url': _pixelDataUrl, 'fit': 'contain'}), + ]); + + final Image image = _imageWidget(tester); + expect(image.image, isA()); + expect((image.image as MemoryImage).bytes, base64Decode(_pixelPng)); + expect(image.fit, BoxFit.contain); + expect(server.requested, isEmpty); + expect(find.byIcon(Icons.broken_image), findsNothing); + expect(tester.takeException(), isNull); + expect(harness.errors, isEmpty); + }); + + _testImages('keeps the natural size', (tester, _) async { + expect(await _sizeOf(tester, null, url: _pixelDataUrl), const Size(1, 1)); + }); + + _testImages('accepts an upper-case scheme and MIME type', ( + tester, + _, + ) async { + await _pumpImages(tester, [ + _image({'url': 'DATA:IMAGE/PNG;base64,$_pixelPng'}), + ]); + + expect(_imageWidget(tester).image, isA()); + expect(find.byIcon(Icons.broken_image), findsNothing); + }); + + _testImages('labels a data URL image for accessibility', (tester, _) async { + final SemanticsHandle semantics = tester.ensureSemantics(); + await _pumpImages(tester, [ + _image({'url': _pixelDataUrl, 'description': 'A pixel'}), + ]); + + expect(_imageWidget(tester).semanticLabel, 'A pixel'); + expect(_imageWidget(tester).excludeFromSemantics, isFalse); + expect(find.bySemanticsLabel('A pixel'), findsOneWidget); + semantics.dispose(); + }); + + _testImages('keeps the decoded image when the component rebuilds', ( + tester, + _, + ) async { + final SurfaceHarness harness = await _pumpImages( + tester, + [ + _image({ + 'url': _pixelDataUrl, + 'description': {'path': '/description'}, + }), + ], + data: {'description': 'Before'}, + ); + final ImageProvider before = _imageWidget(tester).image; + + await harness.send([updateDataModel('After', path: '/description')]); + + expect(_imageWidget(tester).semanticLabel, 'After'); + expect(_imageWidget(tester).image, before); + }); + + for (final (String description, String url) in [ + ('a non-image MIME type', 'data:text/plain;base64,aGVsbG8='), + ('no MIME type', 'data:;base64,$_pixelPng'), + ('malformed base64', 'data:image/png;base64,%%%'), + ]) { + _testImages('shows a placeholder for $description', ( + tester, + server, + ) async { + final SemanticsHandle semantics = tester.ensureSemantics(); + await _pumpImages(tester, [ + _image({'url': url, 'description': 'Not an image'}), + ]); + + expect(find.byType(Image), findsNothing); + expect(server.requested, isEmpty); + expect(find.byIcon(Icons.broken_image), findsOneWidget); + expect( + tester.getSemantics(find.bySemanticsLabel('Not an image')), + matchesSemantics(label: 'Not an image', isImage: true), + ); + expect(tester.takeException(), isNull); + semantics.dispose(); + }); + } + + _testImages('shows a placeholder for image bytes that do not decode', ( + tester, + _, + ) async { + final SurfaceHarness harness = await _pumpImages(tester, [ + _image({'url': 'data:image/png;base64,aGVsbG8='}), + ]); + + expect(find.byIcon(Icons.broken_image), findsOneWidget); + expect(tester.takeException(), isNull); + expect(harness.errors, isEmpty); + }); + }); + for (final url in ['assets/photo.png', 'file:///tmp/photo.png']) { _testImages('shows a placeholder for $url', (tester, server) async { await _pumpImages(tester, [ From 43463a4820077bc06a2b898ae1bac5f169d5d638 Mon Sep 17 00:00:00 2001 From: Karol Kulesza Date: Thu, 12 Nov 2026 18:26:43 +0100 Subject: [PATCH 2/3] docs(changelog): move the entry under Unreleased after the release --- dart/a2ui_flutter/CHANGELOG.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/dart/a2ui_flutter/CHANGELOG.md b/dart/a2ui_flutter/CHANGELOG.md index 928baa2272..85da13e89f 100644 --- a/dart/a2ui_flutter/CHANGELOG.md +++ b/dart/a2ui_flutter/CHANGELOG.md @@ -18,6 +18,10 @@ ## Unreleased +- `Image` renders a `data:` URL of an `image/*` type, such as an inline + base64 PNG, as the web renderers do since + [#3108](https://github.com/a2ui-project/a2ui/pull/3108). A `data:` URL of + another type, or one that does not decode, shows the broken-image icon. - `NodeView` reads `@accessibility` with a fallback to `accessibility` so v1.0 surfaces resolve `@`-prefixed accessibility attributes alongside custom `accessibility` component properties ([#1376](https://github.com/a2ui-project/a2ui/issues/1376), [#4037](https://github.com/a2ui-project/a2ui/pull/4037)). ## 0.3.0 @@ -58,10 +62,6 @@ `UNALLOWED_CHILD`. The basic catalog components keep the constraints of their catalog definition ([#3533](https://github.com/a2ui-project/a2ui/issues/3533)). -- `Image` renders a `data:` URL of an `image/*` type, such as an inline - base64 PNG, as the web renderers do since - [#3108](https://github.com/a2ui-project/a2ui/pull/3108). A `data:` URL of - another type, or one that does not decode, shows the broken-image icon. - `Image` keeps its `description` when it does not load: the broken-image icon is labelled with it, as an `alt` text is on the web ([#3412](https://github.com/a2ui-project/a2ui/pull/3412)). From 4ba2f8d22a4a7ac068ca7ee75e3e7af4745b3353 Mon Sep 17 00:00:00 2001 From: Karol Kulesza Date: Tue, 1 Dec 2026 21:16:15 +0100 Subject: [PATCH 3/3] docs(a2ui_flutter): move the data: URL Image entry to Unreleased --- dart/a2ui_flutter/CHANGELOG.md | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/dart/a2ui_flutter/CHANGELOG.md b/dart/a2ui_flutter/CHANGELOG.md index 1bb60e550f..c5554fde64 100644 --- a/dart/a2ui_flutter/CHANGELOG.md +++ b/dart/a2ui_flutter/CHANGELOG.md @@ -16,6 +16,13 @@ # [a2ui_flutter](https://pub.dev/packages/a2ui_flutter) Changelog +## Unreleased + +- `Image` renders a `data:` URL of an `image/*` type, such as an inline + base64 PNG, as the web renderers do since + [#3108](https://github.com/a2ui-project/a2ui/pull/3108). A `data:` URL of + another type, or one that does not decode, shows the broken-image icon. + ## 0.6.0 - **Breaking:** Requires `a2ui_core` `^0.10.0`, whose types this library @@ -37,10 +44,6 @@ - **Breaking:** Requires `a2ui_core` `^0.8.0`, whose types this library re-exports. -- `Image` renders a `data:` URL of an `image/*` type, such as an inline - base64 PNG, as the web renderers do since - [#3108](https://github.com/a2ui-project/a2ui/pull/3108). A `data:` URL of - another type, or one that does not decode, shows the broken-image icon. - `NodeView` reads `@accessibility` with a fallback to `accessibility` so v1.0 surfaces resolve `@`-prefixed accessibility attributes alongside custom `accessibility` component properties ([#1376](https://github.com/a2ui-project/a2ui/issues/1376), [#4037](https://github.com/a2ui-project/a2ui/pull/4037)). - Add explicit type annotation in `example/lib/chat_session.dart` to satisfy the `specify_nonobvious_local_variable_types` analyzer lint on Dart 3.15+ ([#4233](https://github.com/a2ui-project/a2ui/pull/4233)).