semanticsBuilder property

  1. @override
SemanticsBuilderCallback get semanticsBuilder
override

Returns a function that builds semantic information for the picture drawn by this painter.

If the returned function is null, this painter will not contribute new SemanticsNodes to the semantics tree and the CustomPaint corresponding to this painter will not create a semantics boundary. However, if the child of a CustomPaint is not null, the child may contribute SemanticsNodes to the tree.

See also:

Implementation

@override
SemanticsBuilderCallback get semanticsBuilder {
  return (size) {
    final amounts = _amountsPerDay(numDays);

    // We divide the graph and the amounts into [numGroups] groups, with
    // [numItemsPerGroup] amounts per group.
    const numGroups = 10;
    final numItemsPerGroup = amounts.length ~/ numGroups;

    // For each group we calculate the median value.
    final medians = List.generate(
      numGroups,
      (i) {
        final middleIndex = i * numItemsPerGroup + numItemsPerGroup ~/ 2;
        if (numItemsPerGroup.isEven) {
          return (amounts[middleIndex] + amounts[middleIndex + 1]) / 2;
        } else {
          return amounts[middleIndex];
        }
      },
    );

    // Return a list of [CustomPainterSemantics] with the length of
    // [numGroups], all have the same width with the median amount as label.
    return List.generate(numGroups, (i) {
      return CustomPainterSemantics(
        rect: Offset((i / numGroups) * size.width, 0) &
            Size(size.width / numGroups, size.height),
        properties: SemanticsProperties(
          label: numberFormat.format(medians[i]),
          textDirection: textDirection,
        ),
      );
    });
  };
}