Flutter Text field cursor doesn’t go down after a new line creation. The _paintCaret method in /lib/src/rendering/editable.dart has to be modified and the maxLines parameter in the textField has to be null. It worked for me, hoping to be a temp workaround.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | /// MODIFIED: void _paintCaret(Canvas canvas, Offset effectiveOffset) { assert(_textLayoutLastWidth == constraints.maxWidth); final Offset caretOffset = _textPainter.getOffsetForCaret(_selection.extent, _caretPrototype); final Paint paint = new Paint()..color = _cursorColor; //final Rect caretRect = _caretPrototype.shift(caretOffset + effectiveOffset); var textLength = 0; var inputString = ''; if (text.children != null) { for (var ts in text.children) { textLength += ts.text.length; inputString += ts.text; } } else if (text.text != null) { textLength += text.text.length; inputString += text.text; } final Rect tmpRect = _caretPrototype.shift(caretOffset + effectiveOffset); Rect caretRect = new Rect.fromLTRB(tmpRect.left, _viewportExtent - 2.0 - tmpRect.height, tmpRect.right, _viewportExtent - 2.0); if ((tmpRect.top.abs() - caretRect.top.abs()).abs() > 10) { caretRect = new Rect.fromLTWH(0.0, caretRect.top, caretRect.width, caretRect.height); } if (_selection.extentOffset != textLength){ caretRect = tmpRect; } if (caretRect.left != 0 && inputString[_selection.extentOffset - 1] == '\n') { _selection = new TextSelection.fromPosition(new TextPosition(offset: _selection.extentOffset)); caretRect = _caretPrototype.shift(caretOffset + effectiveOffset); } canvas.drawRect(caretRect, paint); if (caretRect != _lastCaretRect) { _lastCaretRect = caretRect; if (onCaretChanged != null) onCaretChanged(caretRect); } } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.