buildButtonItems static method

List<ContextMenuButtonItem>? buildButtonItems(
  1. EditableTextState editableTextState
)

Builds the button items for the toolbar based on the available spell check suggestions.

Implementation

static List<ContextMenuButtonItem>? buildButtonItems(
  EditableTextState editableTextState,
) {
  // Determine if composing region is misspelled.
  final SuggestionSpan? spanAtCursorIndex =
    editableTextState.findSuggestionSpanAtCursorIndex(
      editableTextState.currentTextEditingValue.selection.baseOffset,
  );

  if (spanAtCursorIndex == null) {
    return null;
  }

  final List<ContextMenuButtonItem> buttonItems = <ContextMenuButtonItem>[];

  // Build suggestion buttons.
  for (final String suggestion in spanAtCursorIndex.suggestions.take(_kMaxSuggestions)) {
    buttonItems.add(ContextMenuButtonItem(
      onPressed: () {
        if (!editableTextState.mounted) {
          return;
        }
        _replaceText(
          editableTextState,
          suggestion,
          spanAtCursorIndex.range,
        );
      },
      label: suggestion,
    ));
  }

  // Build delete button.
  final ContextMenuButtonItem deleteButton =
    ContextMenuButtonItem(
      onPressed: () {
        if (!editableTextState.mounted) {
          return;
        }
        _replaceText(
          editableTextState,
          '',
          editableTextState.currentTextEditingValue.composing,
        );
      },
      type: ContextMenuButtonType.delete,
  );
  buttonItems.add(deleteButton);

  return buttonItems;
}