check method

  1. @override
List<A11yViolation> check(
  1. List<A11yElementInfo> elements
)
override

Inspects elements — a snapshot of the tree captured by the ElementTreeWalker — and returns any violations found.

Implementation

@override
List<A11yViolation> check(List<A11yElementInfo> elements) {
  final violations = <A11yViolation>[];

  for (final info in elements) {
    final widget = info.widget;
    if (widget is! Text) continue;
    if (!info.element.mounted) continue;

    final style = _resolveStyle(info.element, widget);
    final foreground = style.color;
    if (foreground == null || foreground.a <= 0.0) continue;

    final background = _findAncestorBackground(info.element);
    if (background == null) continue;

    final resolvedForeground = ColorContrast.compositeOver(
      foreground,
      background,
    );
    final ratio = ColorContrast.contrastRatio(resolvedForeground, background);

    final isLargeText = _isLargeText(style);
    final threshold = minimumRatio ?? _thresholdFor(isLargeText);

    if (ratio >= threshold) continue;

    violations.add(
      A11yViolation(
        ruleId: id,
        severity: A11ySeverity.error,
        widgetType: info.widgetType.toString(),
        bounds: info.bounds,
        message:
            'Text contrast ratio ${ratio.toStringAsFixed(2)}:1 is below the '
            '${threshold.toStringAsFixed(1)}:1 minimum for '
            '${isLargeText ? 'large' : 'normal'} text.',
        suggestedFix:
            'Darken the text or lighten its background so the contrast '
            'ratio is at least ${threshold.toStringAsFixed(1)}:1.',
      ),
    );
  }

  return violations;
}