I'm using Text Field widget for text editing:
new TextField(
controller: _controller,
maxLines: null,
),
The problem: when I start a new line, the cursor doesn't go down until I start typing a text.
Here is the screenshot:
My question: How to make cursor go down to a new line as soon as it is created?
According to: Github
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.
/// 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 love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With