convertTextPainterSelectionToTextInputSelection function Null safety

TextSelection convertTextPainterSelectionToTextInputSelection(
  1. InlineSpan text,
  2. TextSelection selection,
  3. {bool selectWord = false}
)

Implementation

TextSelection convertTextPainterSelectionToTextInputSelection(
    InlineSpan text, TextSelection selection,
    {bool selectWord = false}) {
  if (selection.isValid) {
    if (selection.isCollapsed) {
      final TextPosition? extent =
          convertTextPainterPostionToTextInputPostion(text, selection.extent);
      if (selection.extent != extent) {
        selection = selection.copyWith(
            baseOffset: extent!.offset,
            extentOffset: extent.offset,
            affinity: selection.affinity,
            isDirectional: selection.isDirectional);
        return selection;
      }
    } else {
      final TextPosition? extent = convertTextPainterPostionToTextInputPostion(
          text, selection.extent,
          end: selectWord ? true : null);

      final TextPosition? base = convertTextPainterPostionToTextInputPostion(
          text, selection.base,
          end: selectWord ? false : null);

      if (selection.extent != extent || selection.base != base) {
        selection = selection.copyWith(
            baseOffset: base!.offset,
            extentOffset: extent!.offset,
            affinity: selection.affinity,
            isDirectional: selection.isDirectional);
        return selection;
      }
    }
  }

  return selection;
}