getCaretOffset method

Offset getCaretOffset(
  1. TextPosition textPosition, {
  2. ValueChanged<double>? caretHeightCallBack,
  3. Offset? effectiveOffset,
  4. Rect caretPrototype = Rect.zero,
})

Implementation

Offset getCaretOffset(
  TextPosition textPosition, {
  ValueChanged<double>? caretHeightCallBack,
  Offset? effectiveOffset,
  Rect caretPrototype = Rect.zero,
}) {
  effectiveOffset ??= Offset.zero;

  ///zmt
  /// fix widget span
  if (hasPlaceholderSpan) {
    ///if first index, check by first span
    int offset = textPosition.offset;
    List<TextBox> boxs = textPainter.getBoxesForSelection(
      TextSelection(
          baseOffset: offset,
          extentOffset: offset + 1,
          affinity: textPosition.affinity),
      boxWidthStyle: selectionWidthStyle,
      boxHeightStyle: selectionHeightStyle,
    );
    if (boxs.isNotEmpty) {
      final Rect rect = boxs.toList().last.toRect();
      caretHeightCallBack?.call(rect.height);
      return rect.topLeft + effectiveOffset;
    } else {
      if (offset <= 0) {
        offset = 1;
      }
      boxs = textPainter.getBoxesForSelection(TextSelection(
          baseOffset: offset - 1,
          extentOffset: offset,
          affinity: textPosition.affinity));
      if (boxs.isNotEmpty) {
        final Rect rect = boxs.toList().last.toRect();
        caretHeightCallBack?.call(rect.height);
        if (textPosition.offset <= 0) {
          return rect.topLeft + effectiveOffset;
        } else {
          return rect.topRight + effectiveOffset;
        }
      }
    }
  }

  final Offset caretOffset =
      textPainter.getOffsetForCaret(textPosition, caretPrototype) +
          effectiveOffset;
  return caretOffset;
}