Skip to content

Detect text contrast violations when the semantics label differs from the rendered text#188024

Open
manan-tech wants to merge 2 commits into
flutter:masterfrom
manan-tech:text-contrast-semantics-label-180081
Open

Detect text contrast violations when the semantics label differs from the rendered text#188024
manan-tech wants to merge 2 commits into
flutter:masterfrom
manan-tech:text-contrast-semantics-label-180081

Conversation

@manan-tech

Copy link
Copy Markdown

Description

MinimumTextContrastGuideline (the textContrastGuideline used by meetsGuideline) located the Text/EditableText widget to measure by string-matching the semantics node's label against the rendered text (find.text(data.label)).

When a Semantics widget contributes a label that merges with a descendant Text (e.g. Semantics(label: 'A', child: Text('B')) produces the merged label "A\nB"), or when Text.semanticsLabel is set, no Text widget has that exact string. find.text therefore returned nothing and the contrast check was silently skipped, so genuine violations such as white text on a white background went undetected.

This locates the text-bearing widgets by inspecting the widget tree and matching render geometry to each semantics node, instead of matching label strings. Results are de-duplicated per view so each widget is evaluated once, against the deepest semantics node it belongs to.

Related Issues

Fixes #180081

Tests

Adds 3 regression tests to packages/flutter_test/test/accessibility_test.dart:

  • a Semantics label that merges with a child Text (white-on-white is now detected),
  • Text.semanticsLabel differing from the visible text (now detected),
  • a guard confirming sufficient-contrast text whose label differs still passes.

Note for reviewers

Because the guideline now checks text it previously skipped, it surfaces some pre-existing contrast problems in the dev sample apps (dev/a11y_assessments, dev/integration_tests/flutter_gallery) — for example a Badge with white text on Colors.green, and tab labels drawn in the same color as a primary-colored app bar. These are genuine accessibility issues in those sample apps rather than regressions introduced here, and are best fixed separately.

… the rendered text

MinimumTextContrastGuideline located the Text/EditableText widget to measure by string-matching the semantics node's label against the rendered text (find.text(label)). When a Semantics widget contributes a label that merges with a descendant Text, or when Text.semanticsLabel is set, no Text widget has that exact string, so the widget was never found and the contrast check was silently skipped -- for example white text on a white background went undetected.

Locate the text-bearing widgets by inspecting the widget tree and matching render geometry to each semantics node instead, deduplicating per view so each widget is checked once.

Fixes flutter#180081
@google-cla

google-cla Bot commented Jun 15, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@github-actions github-actions Bot added a: tests "flutter test", flutter_test, or one of our tests framework flutter/packages/flutter repository. See also f: labels. a: accessibility Accessibility, e.g. VoiceOver or TalkBack. (aka a11y) labels Jun 15, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request modifies MinimumTextContrastGuideline to locate text-bearing widgets by directly querying the widget tree for Text and EditableText elements, rather than matching semantics labels or values. This change ensures contrast checks are performed even when semantics labels differ from the visible text. Additionally, it tracks evaluated elements to avoid duplicate checks and introduces corresponding regression tests. There are no review comments, and I have no feedback to provide.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

a: accessibility Accessibility, e.g. VoiceOver or TalkBack. (aka a11y) a: tests "flutter test", flutter_test, or one of our tests framework flutter/packages/flutter repository. See also f: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

textContrastGuideline does not detect violations (white text on white background) in common use cases

1 participant