微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

widget – Flutter:创建新行后文本字段光标不会关闭

我正在使用Text Field小部件进行文本编辑:

new TextField(
            controller: _controller,maxLines: null,),

问题:当我开始一个新行时,光标不会下降,直到我开始输入文本.

这是截图:

enter image description here

我的问题:如何在创建光标后立即将光标移动到新行?

解决方法

根据: Github

必须修改/lib/src/rendering/editable.dart中的_paintCaret方法,并且textField中的maxLines参数必须为null.它对我有用,希望能成为一个临时解决方案.

/// 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);
  }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐