flutter-accessibility
Original:🇺🇸 English
Translated
Configure your Flutter app to support assistive technologies like Screen Readers
91installs
Sourceflutter/skills
Added on
NPX Install
npx skill4agent add flutter/skills flutter-accessibilityTags
Translated version includes tags in frontmatterSKILL.md Content
View Translation Comparison →flutter-accessibility-and-adaptive-design
Goal
Implements, audits, and enforces accessibility (a11y) and adaptive design standards in Flutter applications. Ensures compliance with WCAG 2 and EN 301 549 by applying proper semantic roles, contrast ratios, tap target sizes, and assistive technology integrations. Constructs adaptive layouts that respond to available screen space and input modalities (touch, mouse, keyboard) without relying on hardware-specific checks or locked orientations.
Decision Logic
When implementing UI components, follow this decision tree to determine the required accessibility and adaptive design implementations:
- Is the app targeting Flutter Web?
- Yes: Ensure is called at startup. Explicitly map custom widgets to
SemanticsBinding.instance.ensureSemantics();to generate correct ARIA tags.SemanticsRole - No: Proceed to standard mobile/desktop semantics.
- Yes: Ensure
- Is the widget interactive?
- Yes: Wrap in with
Semanticsor appropriate role. Ensure tap target is $\ge$ 48x48 logical pixels.button: true - No: Ensure text contrast meets WCAG standards (4.5:1 for small text, 3.0:1 for large text).
- Yes: Wrap in
- Does the layout need to change based on screen size?
- Yes: Use or
LayoutBuilder. Do NOT useMediaQuery.sizeOf(context)or hardware type checks (e.g.,MediaQuery.orientation).isTablet - No: Use standard flexible widgets (,
Expanded) to fill available space.Flexible
- Yes: Use
- Does the app support desktop/web input?
- Yes: Implement ,
FocusableActionDetector, andShortcutsfor hover states and keyboard traversal.MouseRegion - No: Focus primarily on touch targets and screen reader traversal order.
- Yes: Implement
Instructions
-
Initialize Web Accessibility (If Applicable) For web targets, accessibility is disabled by default for performance. Force enable it at the entry point.dart
import 'package:flutter/foundation.dart'; import 'package:flutter/semantics.dart'; void main() { runApp(const MyApp()); if (kIsWeb) { SemanticsBinding.instance.ensureSemantics(); } } -
Apply Semantic Annotations Use,
Semantics, andMergeSemanticsto build a clean accessibility tree. For custom web components, explicitly define theExcludeSemantics.SemanticsRoledartSemantics( role: SemanticsRole.button, label: 'Submit Form', hint: 'Press to send your application', button: true, child: GestureDetector( onTap: _submit, child: const CustomButtonUI(), ), ) -
Enforce Tap Target and Contrast Standards Ensure all interactive elements meet the 48x48 dp minimum (Android) or 44x44 pt minimum (iOS/Web).dart
// Example of enforcing minimum tap target size ConstrainedBox( constraints: const BoxConstraints( minWidth: 48.0, minHeight: 48.0, ), child: IconButton( icon: const Icon(Icons.info), onPressed: () {}, tooltip: 'Information', // Tooltip.message follows Tooltip.child in semantics tree ), ) -
Implement Adaptive Layouts Useto respond to available space rather than device type.
LayoutBuilderdartLayoutBuilder( builder: (context, constraints) { if (constraints.maxWidth > 600) { return const WideDesktopLayout(); } else { return const NarrowMobileLayout(); } }, ) -
Implement Keyboard and Mouse Support Usefor custom controls to handle focus, hover, and keyboard shortcuts simultaneously.
FocusableActionDetectordartFocusableActionDetector( onFocusChange: (hasFocus) => setState(() => _hasFocus = hasFocus), onShowHoverHighlight: (hasHover) => setState(() => _hasHover = hasHover), actions: <Type, Action<Intent>>{ ActivateIntent: CallbackAction<Intent>( onInvoke: (intent) { _performAction(); return null; }, ), }, child: MouseRegion( cursor: SystemMouseCursors.click, child: CustomWidget(isHovered: _hasHover, isFocused: _hasFocus), ), ) -
Manage Focus Traversal Group related widgets usingto ensure logical tab order for keyboard users.
FocusTraversalGroupdartFocusTraversalGroup( policy: OrderedTraversalPolicy(), child: Column( children: [ FocusTraversalOrder( order: const NumericFocusOrder(1.0), child: TextField(), ), FocusTraversalOrder( order: const NumericFocusOrder(2.0), child: ElevatedButton(onPressed: () {}, child: Text('Submit')), ), ], ), ) -
Validate Accessibility via Automated Tests STOP AND ASK THE USER: "Would you like me to generate widget tests to validate accessibility guidelines (contrast, tap targets) for your UI?" If yes, implement theAPI in the test suite:
AccessibilityGuidelinedartimport 'package:flutter_test/flutter_test.dart'; void main() { testWidgets('Validates a11y guidelines', (WidgetTester tester) async { final SemanticsHandle handle = tester.ensureSemantics(); await tester.pumpWidget(const MyApp()); await expectLater(tester, meetsGuideline(androidTapTargetGuideline)); await expectLater(tester, meetsGuideline(iOSTapTargetGuideline)); await expectLater(tester, meetsGuideline(labeledTapTargetGuideline)); await expectLater(tester, meetsGuideline(textContrastGuideline)); handle.dispose(); }); }
Constraints
- Never lock device orientation. Apps must support both portrait and landscape modes to comply with accessibility standards.
- Never use hardware type checks (e.g., checking if the device is a phone or tablet) for layout decisions. Always use or
MediaQuery.sizeOf.LayoutBuilder - Never use near the top of the widget tree to switch layouts. Rely on available width/height breakpoints.
MediaQuery.orientation - Always provide labels for custom interactive widgets or images that convey meaning.
Semantics - Always use on scrollable lists that do not change layout during orientation shifts to preserve scroll state.
PageStorageKey - Do not consume infinite horizontal space for text fields or lists on large screens; constrain maximum widths for readability.
- Account for Tooltip semantics order: is visited immediately after
Tooltip.messagein the semantics tree. Update tests accordingly.Tooltip.child