79338971

Date: 2025-01-08 11:37:13
Score: 1.5
Natty:
Report link

Ok after talking to @pskink comment I reached the solution

void initState() {
    super.initState();

  // other code

  SchedulerBinding.instance.addPostFrameCallback((timeStamp) {
    context.findRenderObject()?.visitChildren(_visitor);
  });
}

void _visitor(RenderObject child) {
    if (child is RenderEditable) {
      setState(() {
        // assign RenderEditable node to widget state
        // make sure you get the correct child, for me there is only one textfield for testing
        reEdt = child;
      });
      return;
    }
    child.visitChildren(_visitor);
  }


// call when inserting text and want to scroll to cursor
void scrollToSelection(TextSelection selection) {
    // find local rect of cursor or starting selection in case of selecting text 
    final localRect = reEdt?.getLocalRectForCaret(TextPosition(offset: selection.baseOffset));
    if (localRect == null) return;
    scrollController.jumpTo(localRect.top);
  }

and don't forget to assign scrollController to TextField

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @pskinkI
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: fanggiz