Search code examples
flutterdartcallbacktextfield

Attach callback function to RenderEditable’s onCaretChanged


After traversing the RenderObject tree of a TextField, I have a reference to the underlying RenderEditable. RenderEditable has a property called onCaretChanged that will fire the attached callback function every time the cursor/caret moves.

If I were constructing a RenderEditable from scratch, I would understand how to attach my callback function in the constructor, but I now only have a reference to it after it has been made in the Build function. How can I attach a callback function to this object after the fact?

Thanks!


Solution

  • Looks like after a recent Flutter update (v3.13.0) onCaretChanged has been removed. Instead, the way to go about this is to attach a void callback to the TextEditingController with addListener (which gives text and caret changes) and utilize the RenderEditable function getLocalRectForCaret.