findOffsetOfLineBreak static method

int findOffsetOfLineBreak(
  1. String nodeId, [
  2. Finder? finder
])

Locates the first line break in a text node, or throws an exception if it cannot find one.

Implementation

static int findOffsetOfLineBreak(String nodeId, [Finder? finder]) {
  late final Finder layoutFinder;
  if (finder != null) {
    layoutFinder = find.descendant(of: finder, matching: find.byType(SingleColumnDocumentLayout));
  } else {
    layoutFinder = find.byType(SingleColumnDocumentLayout);
  }
  final documentLayoutElement = layoutFinder.evaluate().single as StatefulElement;
  final documentLayout = documentLayoutElement.state as DocumentLayout;

  final componentState = documentLayout.getComponentByNodeId(nodeId) as State;
  late final GlobalKey textComponentKey;
  if (componentState is ProxyDocumentComponent) {
    // ignore: invalid_use_of_protected_member
    textComponentKey = componentState.childDocumentComponentKey;
  } else {
    textComponentKey = componentState.widget.key as GlobalKey;
  }

  final textLayout = (textComponentKey.currentState as TextComponentState).textLayout;
  if (textLayout.getLineCount() < 2) {
    throw Exception('Specified node does not contain a line break');
  }
  return textLayout.getPositionAtEndOfLine(const TextPosition(offset: 0)).offset;
}