From 4f6e9afa313f9de331e8a2da9b7e2b913c716013 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Alejandro=20L=C3=B3pez=20Camacho?= Date: Fri, 25 Sep 2026 16:12:56 -0500 Subject: [PATCH 1/2] fix(genui): let the catalog's description name an image The basic catalog declares `description` on `Image` as the accessibility text a screen reader announces, and web_core, React, Angular and Swift all apply it. genui's schema declared only url, fit and variant, and the widget built Image.network / Image.asset with no semanticLabel, so a description sent against the published catalog was validated, accepted and then dropped. An image is the one component whose whole accessible name has to come from the agent, which made it the worst one to drop it on. It is a DynamicString in the spec, so it resolves through BoundString and a {"path": ...} binding works the same as a literal. Absent, nothing changes: semanticLabel stays null rather than becoming an empty string, so an image with no description is left exactly as it was. Fixes a2ui-project/a2ui#2740. --- packages/genui/CHANGELOG.md | 10 + .../catalog/basic_catalog_widgets/image.dart | 236 ++++++++++-------- .../test/catalog/core_widgets/image_test.dart | 114 +++++++++ 3 files changed, 259 insertions(+), 101 deletions(-) diff --git a/packages/genui/CHANGELOG.md b/packages/genui/CHANGELOG.md index c10b17b25..983c9270a 100644 --- a/packages/genui/CHANGELOG.md +++ b/packages/genui/CHANGELOG.md @@ -1,5 +1,15 @@ # [genui](https://pub.dev/packages/genui) Changelog +## (WIP) + +- **Fix**: `Image` accepts the catalog's `description` and passes it to Flutter + as `semanticLabel`. The basic catalog declares it as the accessibility text + for an image, and every other first-party renderer applies it, but genui's + schema left it out and built `Image.network` / `Image.asset` without a label, + so an image was the one component whose whole accessible name comes from the + agent and the one component that dropped it. It is a `DynamicString`, so a + data binding works the same as a literal. + ## 0.10.3 - Updated `basicCatalogId` to point to the canonical URL: `https://a2ui.org/specification/v0_9/catalogs/basic/catalog.json`. diff --git a/packages/genui/lib/src/catalog/basic_catalog_widgets/image.dart b/packages/genui/lib/src/catalog/basic_catalog_widgets/image.dart index da9829cdd..fb9cef84b 100644 --- a/packages/genui/lib/src/catalog/basic_catalog_widgets/image.dart +++ b/packages/genui/lib/src/catalog/basic_catalog_widgets/image.dart @@ -17,6 +17,9 @@ Schema _schema() { description: 'Asset path (e.g. assets/...) or network URL (e.g. https://...)', ), + 'description': A2uiSchemas.stringReference( + description: 'Accessibility text for the image.', + ), 'fit': S.string( description: 'How the image should be inscribed into the box.', enumValues: BoxFit.values.map((e) => e.name).toList(), @@ -47,10 +50,20 @@ Schema _schema() { } extension type _ImageData.fromMap(JsonMap _json) { - factory _ImageData({required JsonMap url, String? fit, String? variant}) => - _ImageData.fromMap({'url': url, 'fit': fit, 'variant': variant}); + factory _ImageData({ + required JsonMap url, + Object? description, + String? fit, + String? variant, + }) => _ImageData.fromMap({ + 'url': url, + 'description': description, + 'fit': fit, + 'variant': variant, + }); Object get url => _json['url'] as Object; + Object? get description => _json['description']; BoxFit? get fit => _json['fit'] != null ? BoxFit.values.firstWhere((e) => e.name == _json['fit'] as String) : null; @@ -65,6 +78,8 @@ extension type _ImageData.fromMap(JsonMap _json) { /// /// - `url`: The URL of the image to display. Can be a network URL or a local /// asset path. +/// - `description`: Accessibility text for the image, announced by a screen +/// reader in place of the image itself. /// - `fit`: How the image should be inscribed into the box. See [BoxFit] for /// possible values. /// - `variant`: A usage hint for the image size and style. One of 'icon', @@ -91,107 +106,126 @@ final CatalogItem image = CatalogItem( return BoundString( dataContext: itemContext.dataContext, - value: imageData.url, - builder: (context, value) { - if (value == null || value.isEmpty) { - genUiLogger.warning( - 'Image widget created with no URL at path: ' - '${itemContext.dataContext.path}', - ); - return const SizedBox.shrink(); - } + value: imageData.description ?? '', + builder: (context, description) { + final String? semanticLabel = description == null || description.isEmpty + ? null + : description; + return _buildImage(itemContext, imageData, semanticLabel); + }, + ); + }, +); + +Widget _buildImage( + CatalogItemContext itemContext, + _ImageData imageData, + String? semanticLabel, +) { + return BoundString( + dataContext: itemContext.dataContext, + value: imageData.url, + builder: (context, value) { + if (value == null || value.isEmpty) { + genUiLogger.warning( + 'Image widget created with no URL at path: ' + '${itemContext.dataContext.path}', + ); + return const SizedBox.shrink(); + } - Widget child; - if (value.startsWith('http')) { - child = Image.network( - value, - fit: imageData.fit, - frameBuilder: - ( - BuildContext context, - Widget child, - int? frame, - bool wasSynchronouslyLoaded, - ) { - if (wasSynchronouslyLoaded) { - return child; - } - return AnimatedOpacity( - opacity: frame == null ? 0 : 1, - duration: const Duration(seconds: 1), - curve: Curves.easeOut, - child: child, - ); - }, - loadingBuilder: - ( - BuildContext context, - Widget child, - ImageChunkEvent? loadingProgress, - ) { - if (loadingProgress == null) { - return child; - } - return Center( - child: CircularProgressIndicator( - value: loadingProgress.expectedTotalBytes != null - ? loadingProgress.cumulativeBytesLoaded / - loadingProgress.expectedTotalBytes! - : null, - ), - ); - }, - errorBuilder: - (BuildContext context, Object error, StackTrace? stackTrace) { - return const Icon(Icons.broken_image); - }, - ); - } else { - child = Image.asset( - value, - fit: imageData.fit, - frameBuilder: - ( - BuildContext context, - Widget child, - int? frame, - bool wasSynchronouslyLoaded, - ) { - if (wasSynchronouslyLoaded) { - return child; - } - return AnimatedOpacity( - opacity: frame == null ? 0 : 1, - duration: const Duration(seconds: 1), - curve: Curves.easeOut, - child: child, - ); - }, - errorBuilder: - (BuildContext context, Object error, StackTrace? stackTrace) { - return const Icon(Icons.broken_image); - }, - ); - } + Widget child; + if (value.startsWith('http')) { + child = Image.network( + value, + fit: imageData.fit, + semanticLabel: semanticLabel, + frameBuilder: + ( + BuildContext context, + Widget child, + int? frame, + bool wasSynchronouslyLoaded, + ) { + if (wasSynchronouslyLoaded) { + return child; + } + return AnimatedOpacity( + opacity: frame == null ? 0 : 1, + duration: const Duration(seconds: 1), + curve: Curves.easeOut, + child: child, + ); + }, + loadingBuilder: + ( + BuildContext context, + Widget child, + ImageChunkEvent? loadingProgress, + ) { + if (loadingProgress == null) { + return child; + } + return Center( + child: CircularProgressIndicator( + value: loadingProgress.expectedTotalBytes != null + ? loadingProgress.cumulativeBytesLoaded / + loadingProgress.expectedTotalBytes! + : null, + ), + ); + }, + errorBuilder: + (BuildContext context, Object error, StackTrace? stackTrace) { + return const Icon(Icons.broken_image); + }, + ); + } else { + child = Image.asset( + value, + fit: imageData.fit, + semanticLabel: semanticLabel, + frameBuilder: + ( + BuildContext context, + Widget child, + int? frame, + bool wasSynchronouslyLoaded, + ) { + if (wasSynchronouslyLoaded) { + return child; + } + return AnimatedOpacity( + opacity: frame == null ? 0 : 1, + duration: const Duration(seconds: 1), + curve: Curves.easeOut, + child: child, + ); + }, + errorBuilder: + (BuildContext context, Object error, StackTrace? stackTrace) { + return const Icon(Icons.broken_image); + }, + ); + } - if (imageData.variant == 'avatar') { - child = CircleAvatar(child: child); - } + if (imageData.variant == 'avatar') { + child = CircleAvatar(child: child); + } - if (imageData.variant == 'header') { - return SizedBox(width: double.infinity, child: child); - } + if (imageData.variant == 'header') { + return SizedBox(width: double.infinity, child: child); + } - final double size = switch (imageData.variant) { - 'icon' || 'avatar' => 32.0, - 'smallFeature' => 50.0, - 'mediumFeature' => 150.0, - 'largeFeature' => 400.0, - _ => 150.0, - }; + final double size = switch (imageData.variant) { + 'icon' || 'avatar' => 32.0, + 'smallFeature' => 50.0, + 'mediumFeature' => 150.0, + 'largeFeature' => 400.0, + _ => 150.0, + }; - return SizedBox(width: size, height: size, child: child); - }, - ); - }, -); + return SizedBox(width: size, height: size, child: child); + }, + ); +} diff --git a/packages/genui/test/catalog/core_widgets/image_test.dart b/packages/genui/test/catalog/core_widgets/image_test.dart index f82e2bd4e..0a181e2a6 100644 --- a/packages/genui/test/catalog/core_widgets/image_test.dart +++ b/packages/genui/test/catalog/core_widgets/image_test.dart @@ -118,6 +118,120 @@ void main() { debugNetworkImageHttpClientProvider = null; } }); + + testWidgets('the catalog description names the image', ( + WidgetTester tester, + ) async { + debugNetworkImageHttpClientProvider = _FakeSuccessHttpClient.new; + try { + await HttpOverrides.runZoned(() async { + await tester.pumpWidget( + _surface([ + component( + id: 'root', + type: 'Image', + properties: { + 'url': 'https://example.com/image.png', + 'description': 'A golden retriever puppy', + }, + ), + ]), + ); + await tester.pumpAndSettle(); + + expect( + tester.widget(find.byType(Image)).semanticLabel, + 'A golden retriever puppy', + ); + }); + } finally { + debugNetworkImageHttpClientProvider = null; + } + }); + + testWidgets('the description may come from the data model', ( + WidgetTester tester, + ) async { + debugNetworkImageHttpClientProvider = _FakeSuccessHttpClient.new; + try { + await HttpOverrides.runZoned(() async { + await tester.pumpWidget( + _surface( + [ + component( + id: 'root', + type: 'Image', + properties: { + 'url': 'https://example.com/image.png', + 'description': {'path': '/alt'}, + }, + ), + ], + data: {'alt': 'The chart for this quarter'}, + ), + ); + await tester.pumpAndSettle(); + + expect( + tester.widget(find.byType(Image)).semanticLabel, + 'The chart for this quarter', + ); + }); + } finally { + debugNetworkImageHttpClientProvider = null; + } + }); + + testWidgets('an image with no description is left as it was', ( + WidgetTester tester, + ) async { + debugNetworkImageHttpClientProvider = _FakeSuccessHttpClient.new; + try { + await HttpOverrides.runZoned(() async { + await tester.pumpWidget( + _surface([ + component( + id: 'root', + type: 'Image', + properties: {'url': 'https://example.com/image.png'}, + ), + ]), + ); + await tester.pumpAndSettle(); + + expect(tester.widget(find.byType(Image)).semanticLabel, isNull); + }); + } finally { + debugNetworkImageHttpClientProvider = null; + } + }); +} + +/// Renders [components] on one surface built from the image catalog item. +Widget _surface(List components, {JsonMap? data}) { + final surfaceController = SurfaceController( + catalogs: [ + Catalog([BasicCatalogItems.image], catalogId: 'test_catalog'), + ], + ); + addTearDown(surfaceController.dispose); + const surfaceId = 'testSurface'; + surfaceController.handleMessage( + updateComponents(surfaceId: surfaceId, components: components), + ); + surfaceController.handleMessage( + createSurface(surfaceId: surfaceId, catalogId: 'test_catalog'), + ); + if (data != null) { + surfaceController.handleMessage( + updateDataModel(surfaceId: surfaceId, value: data), + ); + } + return MaterialApp( + home: Scaffold( + body: Surface(surfaceContext: surfaceController.contextFor(surfaceId)), + ), + ); } class _FakeSuccessHttpClient extends Fake implements HttpClient { From 76f5f1743b5cf7be5492b79b2ed1b9e35790ce5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Alejandro=20L=C3=B3pez=20Camacho?= Date: Fri, 25 Sep 2026 22:35:05 -0500 Subject: [PATCH 2/2] fix(genui): skip the binding when an image has no description Most images carry no description, and that path was still allocating a BoundString and its state for a value that could never resolve to anything. --- .../lib/src/catalog/basic_catalog_widgets/image.dart | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/genui/lib/src/catalog/basic_catalog_widgets/image.dart b/packages/genui/lib/src/catalog/basic_catalog_widgets/image.dart index fb9cef84b..a96813d5e 100644 --- a/packages/genui/lib/src/catalog/basic_catalog_widgets/image.dart +++ b/packages/genui/lib/src/catalog/basic_catalog_widgets/image.dart @@ -104,13 +104,17 @@ final CatalogItem image = CatalogItem( widgetBuilder: (itemContext) { final imageData = _ImageData.fromMap(itemContext.data as JsonMap); + // Most images carry no description, and that path needs no binding. + final Object? description = imageData.description; + if (description == null) return _buildImage(itemContext, imageData, null); + return BoundString( dataContext: itemContext.dataContext, - value: imageData.description ?? '', - builder: (context, description) { - final String? semanticLabel = description == null || description.isEmpty + value: description, + builder: (context, resolved) { + final String? semanticLabel = resolved == null || resolved.isEmpty ? null - : description; + : resolved; return _buildImage(itemContext, imageData, semanticLabel); }, );