我正在使用Text Field小部件进行文本编辑:
new TextField(
controller: _controller,
maxLines: null,
),
问题:当我开始一个新行时,光标不会下降,直到我开始输入文本.
我的问题:如何在创建光标后立即将光标移动到新行?
最佳答案 根据:
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);
}
}