defaultLabelBuilder static method

Widget defaultLabelBuilder(
  1. BuildContext context,
  2. String label,
  3. int index
)

Serves as the default value for builder, rendering the label as a RichText widget with appropriate TextSpans for rendering the label with an underscore under the selected accelerator for the label when the index is non-negative, and a Text widget when the index is negative.

The arguments to the function are as follows:

  • The context supplies the BuildContext to use.
  • The label is the MenuAcceleratorLabel.label attribute for the relevant MenuAcceleratorLabel with the accelerator markers stripped out of it.
  • The index is the index of the accelerator character within the label.characters that applies to this accelerator. If it is -1, then the accelerator should not be highlighted. Otherwise, the given character should be highlighted somehow in the rendered label (typically with an underscore). Importantly, index is not an index into the String label, it is an index into the Characters iterable returned by label.characters, so that it is in terms of user-visible characters (a.k.a. grapheme clusters), not Unicode code points.

Implementation

static Widget defaultLabelBuilder(
  BuildContext context,
  String label,
  int index,
) {
  if (index < 0) {
    return Text(label);
  }
  final TextStyle defaultStyle = DefaultTextStyle.of(context).style;
  final Characters characters = label.characters;
  return RichText(
    text: TextSpan(
      children: <TextSpan>[
        if (index > 0)
          TextSpan(text: characters.getRange(0, index).toString(), style: defaultStyle),
        TextSpan(
          text: characters.getRange(index, index + 1).toString(),
          style: defaultStyle.copyWith(decoration: TextDecoration.underline),
        ),
        if (index < characters.length - 1)
          TextSpan(text: characters.getRange(index + 1).toString(), style: defaultStyle),
      ],
    ),
  );
}