describeSemanticsConfiguration method

  1. @override
void describeSemanticsConfiguration(
  1. SemanticsConfiguration config
)
override

Report the semantics of this node, for example for accessibility purposes.

This method should be overridden by subclasses that have interesting semantic information.

The given SemanticsConfiguration object is mutable and should be annotated in a manner that describes the current state. No reference should be kept to that object; mutating it outside of the context of the describeSemanticsConfiguration call (for example as a result of asynchronous computation) will at best have no useful effect and at worse will cause crashes as the data will be in an inconsistent state.

{@tool snippet}

The following snippet will describe the node as a button that responds to tap actions.

abstract class SemanticButtonRenderObject extends RenderObject {
  @override
  void describeSemanticsConfiguration(SemanticsConfiguration config) {
    super.describeSemanticsConfiguration(config);
    config
      ..onTap = _handleTap
      ..label = 'I am a button'
      ..isButton = true;
  }

  void _handleTap() {
    // Do something.
  }
}

{@end-tool}

Implementation

@override
void describeSemanticsConfiguration(SemanticsConfiguration config) {
  super.describeSemanticsConfiguration(config);
  _semanticsInfo = text.getSemanticsInformation();

  if (_semanticsInfo!.any(
      (InlineSpanSemanticsInformation info) => info.recognizer != null)) {
    config.explicitChildNodes = true;
    config.isSemanticBoundary = true;
  } else {
    final StringBuffer buffer = StringBuffer();
    for (final InlineSpanSemanticsInformation info in _semanticsInfo!) {
      buffer.write(info.semanticsLabel ?? info.text);
    }
    config.label = buffer.toString();
    config.textDirection = textDirection;
  }
}