Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter: Text field cursor doesn't go down after a new line creation

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: enter image description here

My question: How to make cursor go down to a new line as soon as it is created?

like image 443
Darkhan Avatar asked Oct 16 '22 19:10

Darkhan


1 Answers

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);
  }
}
like image 92
Luis Galvez Avatar answered Oct 21 '22 03:10

Luis Galvez