--- name: flutter-accessibility description: Use when implementing or auditing accessibility on a Flutter screen — the Semantics widget, screen-reader labels (TalkBack/VoiceOver), colour contrast, minimum tap-target size, system font scaling, or semantics in a web build. Also triggers on "make this accessible", "a11y", or a request for an accessibility review. --- You are implementing or auditing accessibility (a11y) for a Flutter app. ## Context Accessibility pressure differs by surface, and it changes what you prioritise: - **Customer-facing web** — the highest bar. Screen-reader semantics are off by default on Flutter web (see section 5), the audience is unknown, and in many jurisdictions this is a legal requirement, not a nicety. - **Internal/admin web** — keyboard navigation and contrast matter most; staff use these all day. - **Mobile** — platform screen readers (TalkBack/VoiceOver) mostly work if you give widgets labels and keep tap targets large enough. Choose based on the skill argument, if one was given, or on the screen/task at hand: - `audit` — Review existing code for a11y violations and produce a report with fixes - `implement` — Add accessibility annotations and fixes to existing widgets - `web-setup` — Enable Flutter web semantics and configure for screen readers - If no mode specified, default to `implement` --- ## 1. Semantic Annotations Wrap custom widgets with `Semantics` to expose meaning to assistive technology: ```dart // Label interactive elements Semantics( label: 'Adicionar ao pedido', button: true, child: CustomAddButton(...), ) // Group related elements into a single announcement MergeSemantics( child: Row( children: [ Icon(Icons.star), Text('4.5'), ], ), ) // Hide decorative elements from screen readers ExcludeSemantics( child: DecorativeBackground(), ) ``` ### When to use each | Widget | Use case | |---|---| | `Semantics(label:)` | Custom widgets that lack built-in semantics (raw GestureDetector, Container with onTap) | | `Semantics(button: true)` | Custom buttons not using Material `ElevatedButton`/`TextButton` | | `Semantics(header: true)` | Section headers (category names in a catalog) | | `Semantics(image: true, label:)` | Product/content images | | `MergeSemantics` | Icon + text pairs, price + currency pairs | | `ExcludeSemantics` | Purely decorative images, background gradients, dividers | Material widgets (`ElevatedButton`, `TextField`, `Checkbox`) already have built-in semantics. Do NOT double-wrap them unless overriding the default label. --- ## 2. Color Contrast Follow WCAG 2.1 minimum contrast ratios: | Element | Minimum ratio | |---|---| | Normal text (< 18sp) | **4.5:1** | | Large text (>= 18sp or >= 14sp bold) | **3.0:1** | | UI components & graphical objects | **3.0:1** | ### Verification - Use the **Accessibility Scanner** on Android (Settings > Accessibility) - Run **Lighthouse** audit in Chrome DevTools for the web build - Use online contrast checkers (WebAIM Contrast Checker) with hex values from `ds_colors.dart` ### Violations that show up again and again - Light gray text on white backgrounds for secondary info (descriptions, prices in light theme) - Disabled button text that falls below 3.0:1 - Placeholder text in search fields When fixing, update colors in `design_system/` tokens so the fix propagates everywhere. --- ## 3. Tap Targets All interactive elements must be at least **48x48 logical pixels**: ```dart // BAD - icon button too small GestureDetector( onTap: onAdd, child: Icon(Icons.add, size: 24), ) // GOOD - ensure minimum hit area IconButton( onPressed: onAdd, icon: Icon(Icons.add, size: 24), // IconButton already enforces 48x48 by default ) // GOOD - for custom widgets, use constraints GestureDetector( onTap: onAdd, child: ConstrainedBox( constraints: const BoxConstraints(minWidth: 48, minHeight: 48), child: Center(child: Icon(Icons.add, size: 24)), ), ) ``` ### Spacing between targets Adjacent tap targets must have enough spacing to avoid accidental taps. Minimum **8px** gap between interactive elements. --- ## 4. Screen Reader Support ### Labels for images (product photos, avatars, thumbnails) ```dart // Product image in a catalog Semantics( image: true, label: 'Foto do produto: ${product.name}', child: CachedNetworkImage( imageUrl: product.imageUrl, // ... ), ) // Decorative images (backgrounds, patterns) ExcludeSemantics( child: Image.asset('assets/background_pattern.png'), ) ``` ### Labels for icon buttons ```dart IconButton( icon: Icon(Icons.shopping_cart), tooltip: 'Ver carrinho', // tooltip doubles as semantic label onPressed: () => openCart(), ) ``` ### Price announcements ```dart // Announce price clearly MergeSemantics( child: Row( children: [ Text('R\$'), Text('29,90'), ], ), ) // Screen reader announces: "R$ 29,90" ``` --- ## 5. Web Accessibility (any web build) Flutter web **disables semantics by default** for performance. For customer-facing apps this is unacceptable. ### Enable semantics in web builds In the app's `web/index.html`, add before the Flutter loader script: ```html ``` In the app's `main.dart`: ```dart import 'package:flutter/semantics.dart'; void main() { // Force enable semantics for web accessibility WidgetsFlutterBinding.ensureInitialized(); SemanticsBinding.instance.ensureSemantics(); runApp(const MyApp()); } ``` ### Alternative: enable via URL parameter For testing, append `?semantics=true` to the web URL to activate the semantics tree without code changes. ### Web-specific considerations - Ensure the HTML `