generate static method

A11yReport generate(
  1. BuildContext context, {
  2. List<A11yRule> rules = const [ContrastRule(), TapTargetSizeRule()],
  3. List<A11ySemanticsRule> semanticsRules = const [SemanticLabelRule(), FocusOrderRule()],
})

Walks context with ElementTreeWalker and, if semanticsRules is non-empty, SemanticsTreeWalker too, runs every rule, and returns the combined result.

SemanticsTreeWalker needs an active SemanticsHandle to return anything — under flutter test, testWidgets provides one by default, so this "just works" there; outside of tests, hold one yourself (e.g. via SemanticsBinding.instance.ensureSemantics()) for the duration of the call.

Implementation

static A11yReport generate(
  BuildContext context, {
  List<A11yRule> rules = const [ContrastRule(), TapTargetSizeRule()],
  List<A11ySemanticsRule> semanticsRules = const [
    SemanticLabelRule(),
    FocusOrderRule(),
  ],
}) {
  const walker = ElementTreeWalker();
  final elements = walker.walk(context);
  final violations = <A11yViolation>[
    for (final rule in rules) ...rule.check(elements),
  ];

  if (semanticsRules.isNotEmpty) {
    const semanticsWalker = SemanticsTreeWalker();
    final nodes = semanticsWalker.walk(context);
    for (final rule in semanticsRules) {
      violations.addAll(rule.check(nodes));
    }
  }

  return A11yReport(violations: violations, generatedAt: DateTime.now());
}