Fix leading and body

pull/13/head
singerdmx 4 years ago
parent e997ee9298
commit 68f4634b42
  1. 104
      lib/widgets/text_line.dart

@ -201,8 +201,8 @@ class EditableTextLine extends RenderObjectWidget {
enum TextLineSlot { LEADING, BODY } enum TextLineSlot { LEADING, BODY }
class RenderEditableTextLine extends RenderEditableBox { class RenderEditableTextLine extends RenderEditableBox {
RenderBox leading; RenderBox _leading;
RenderContentProxyBox body; RenderContentProxyBox _body;
Line line; Line line;
TextDirection textDirection; TextDirection textDirection;
TextSelection textSelection; TextSelection textSelection;
@ -235,11 +235,11 @@ class RenderEditableTextLine extends RenderEditableBox {
assert(cursorCont != null); assert(cursorCont != null);
Iterable<RenderBox> get _children sync* { Iterable<RenderBox> get _children sync* {
if (leading != null) { if (_leading != null) {
yield leading; yield _leading;
} }
if (body != null) { if (_body != null) {
yield body; yield _body;
} }
} }
@ -335,11 +335,11 @@ class RenderEditableTextLine extends RenderEditableBox {
} }
setLeading(RenderBox l) { setLeading(RenderBox l) {
leading = _updateChild(leading, l, TextLineSlot.LEADING); _leading = _updateChild(_leading, l, TextLineSlot.LEADING);
} }
setBody(RenderContentProxyBox b) { setBody(RenderContentProxyBox b) {
body = _updateChild(body, b, TextLineSlot.BODY); _body = _updateChild(_body, b, TextLineSlot.BODY);
} }
bool containsTextSelection() { bool containsTextSelection() {
@ -365,8 +365,8 @@ class RenderEditableTextLine extends RenderEditableBox {
} }
List<TextBox> _getBoxes(TextSelection textSelection) { List<TextBox> _getBoxes(TextSelection textSelection) {
BoxParentData parentData = body.parentData as BoxParentData; BoxParentData parentData = _body.parentData as BoxParentData;
return body.getBoxesForSelection(textSelection).map((box) { return _body.getBoxesForSelection(textSelection).map((box) {
return TextBox.fromLTRBD( return TextBox.fromLTRBD(
box.left + parentData.offset.dx, box.left + parentData.offset.dx,
box.top + parentData.offset.dy, box.top + parentData.offset.dy,
@ -428,8 +428,8 @@ class RenderEditableTextLine extends RenderEditableBox {
@override @override
Offset getOffsetForCaret(TextPosition position) { Offset getOffsetForCaret(TextPosition position) {
return body.getOffsetForCaret(position, _caretPrototype) + return _body.getOffsetForCaret(position, _caretPrototype) +
(body.parentData as BoxParentData).offset; (_body.parentData as BoxParentData).offset;
} }
@override @override
@ -446,8 +446,8 @@ class RenderEditableTextLine extends RenderEditableBox {
assert(textPosition.offset < line.length); assert(textPosition.offset < line.length);
Offset offset = getOffsetForCaret(textPosition) Offset offset = getOffsetForCaret(textPosition)
.translate(0, dyScale * preferredLineHeight(textPosition)); .translate(0, dyScale * preferredLineHeight(textPosition));
if (body.size if (_body.size
.contains(offset - (body.parentData as BoxParentData).offset)) { .contains(offset - (_body.parentData as BoxParentData).offset)) {
return getPositionForOffset(offset); return getPositionForOffset(offset);
} }
return null; return null;
@ -455,18 +455,18 @@ class RenderEditableTextLine extends RenderEditableBox {
@override @override
TextPosition getPositionForOffset(Offset offset) { TextPosition getPositionForOffset(Offset offset) {
return body.getPositionForOffset( return _body.getPositionForOffset(
offset - (body.parentData as BoxParentData).offset); offset - (_body.parentData as BoxParentData).offset);
} }
@override @override
TextRange getWordBoundary(TextPosition position) { TextRange getWordBoundary(TextPosition position) {
return body.getWordBoundary(position); return _body.getWordBoundary(position);
} }
@override @override
double preferredLineHeight(TextPosition position) { double preferredLineHeight(TextPosition position) {
return body.getPreferredLineHeight(); return _body.getPreferredLineHeight();
} }
@override @override
@ -542,8 +542,8 @@ class RenderEditableTextLine extends RenderEditableBox {
} }
} }
add(leading, 'leading'); add(_leading, 'leading');
add(body, 'body'); add(_body, 'body');
return value; return value;
} }
@ -555,12 +555,12 @@ class RenderEditableTextLine extends RenderEditableBox {
_resolvePadding(); _resolvePadding();
double horizontalPadding = _resolvedPadding.left + _resolvedPadding.right; double horizontalPadding = _resolvedPadding.left + _resolvedPadding.right;
double verticalPadding = _resolvedPadding.top + _resolvedPadding.bottom; double verticalPadding = _resolvedPadding.top + _resolvedPadding.bottom;
int leadingWidth = leading == null int leadingWidth = _leading == null
? 0 ? 0
: leading.getMinIntrinsicWidth(height - verticalPadding); : _leading.getMinIntrinsicWidth(height - verticalPadding);
int bodyWidth = body == null int bodyWidth = _body == null
? 0 ? 0
: body.getMinIntrinsicWidth(math.max(0.0, height - verticalPadding)); : _body.getMinIntrinsicWidth(math.max(0.0, height - verticalPadding));
return horizontalPadding + leadingWidth + bodyWidth; return horizontalPadding + leadingWidth + bodyWidth;
} }
@ -569,12 +569,12 @@ class RenderEditableTextLine extends RenderEditableBox {
_resolvePadding(); _resolvePadding();
double horizontalPadding = _resolvedPadding.left + _resolvedPadding.right; double horizontalPadding = _resolvedPadding.left + _resolvedPadding.right;
double verticalPadding = _resolvedPadding.top + _resolvedPadding.bottom; double verticalPadding = _resolvedPadding.top + _resolvedPadding.bottom;
int leadingWidth = leading == null int leadingWidth = _leading == null
? 0 ? 0
: leading.getMaxIntrinsicWidth(height - verticalPadding); : _leading.getMaxIntrinsicWidth(height - verticalPadding);
int bodyWidth = body == null int bodyWidth = _body == null
? 0 ? 0
: body.getMaxIntrinsicWidth(math.max(0.0, height - verticalPadding)); : _body.getMaxIntrinsicWidth(math.max(0.0, height - verticalPadding));
return horizontalPadding + leadingWidth + bodyWidth; return horizontalPadding + leadingWidth + bodyWidth;
} }
@ -583,8 +583,8 @@ class RenderEditableTextLine extends RenderEditableBox {
_resolvePadding(); _resolvePadding();
double horizontalPadding = _resolvedPadding.left + _resolvedPadding.right; double horizontalPadding = _resolvedPadding.left + _resolvedPadding.right;
double verticalPadding = _resolvedPadding.top + _resolvedPadding.bottom; double verticalPadding = _resolvedPadding.top + _resolvedPadding.bottom;
if (body != null) { if (_body != null) {
return body return _body
.getMinIntrinsicHeight(math.max(0.0, width - horizontalPadding)) + .getMinIntrinsicHeight(math.max(0.0, width - horizontalPadding)) +
verticalPadding; verticalPadding;
} }
@ -596,8 +596,8 @@ class RenderEditableTextLine extends RenderEditableBox {
_resolvePadding(); _resolvePadding();
double horizontalPadding = _resolvedPadding.left + _resolvedPadding.right; double horizontalPadding = _resolvedPadding.left + _resolvedPadding.right;
double verticalPadding = _resolvedPadding.top + _resolvedPadding.bottom; double verticalPadding = _resolvedPadding.top + _resolvedPadding.bottom;
if (body != null) { if (_body != null) {
return body return _body
.getMaxIntrinsicHeight(math.max(0.0, width - horizontalPadding)) + .getMaxIntrinsicHeight(math.max(0.0, width - horizontalPadding)) +
verticalPadding; verticalPadding;
} }
@ -607,7 +607,7 @@ class RenderEditableTextLine extends RenderEditableBox {
@override @override
double computeDistanceToActualBaseline(TextBaseline baseline) { double computeDistanceToActualBaseline(TextBaseline baseline) {
_resolvePadding(); _resolvePadding();
return body.getDistanceToActualBaseline(baseline) + _resolvedPadding.top; return _body.getDistanceToActualBaseline(baseline) + _resolvedPadding.top;
} }
@override @override
@ -618,7 +618,7 @@ class RenderEditableTextLine extends RenderEditableBox {
_resolvePadding(); _resolvePadding();
assert(_resolvedPadding != null); assert(_resolvedPadding != null);
if (body == null && leading == null) { if (_body == null && _leading == null) {
size = constraints.constrain(Size( size = constraints.constrain(Size(
_resolvedPadding.left + _resolvedPadding.right, _resolvedPadding.left + _resolvedPadding.right,
_resolvedPadding.top + _resolvedPadding.bottom, _resolvedPadding.top + _resolvedPadding.bottom,
@ -631,30 +631,30 @@ class RenderEditableTextLine extends RenderEditableBox {
? _resolvedPadding.left ? _resolvedPadding.left
: _resolvedPadding.right; : _resolvedPadding.right;
body.layout(innerConstraints, parentUsesSize: true); _body.layout(innerConstraints, parentUsesSize: true);
final bodyParentData = body.parentData as BoxParentData; final bodyParentData = _body.parentData as BoxParentData;
bodyParentData.offset = Offset(_resolvedPadding.left, _resolvedPadding.top); bodyParentData.offset = Offset(_resolvedPadding.left, _resolvedPadding.top);
if (leading != null) { if (_leading != null) {
final leadingConstraints = innerConstraints.copyWith( final leadingConstraints = innerConstraints.copyWith(
minWidth: indentWidth, minWidth: indentWidth,
maxWidth: indentWidth, maxWidth: indentWidth,
maxHeight: body.size.height); maxHeight: _body.size.height);
leading.layout(leadingConstraints, parentUsesSize: true); _leading.layout(leadingConstraints, parentUsesSize: true);
final parentData = leading.parentData as BoxParentData; final parentData = _leading.parentData as BoxParentData;
parentData.offset = Offset(0.0, _resolvedPadding.top); parentData.offset = Offset(0.0, _resolvedPadding.top);
} }
size = constraints.constrain(Size( size = constraints.constrain(Size(
_resolvedPadding.left + body.size.width + _resolvedPadding.right, _resolvedPadding.left + _body.size.width + _resolvedPadding.right,
_resolvedPadding.top + body.size.height + _resolvedPadding.bottom, _resolvedPadding.top + _body.size.height + _resolvedPadding.bottom,
)); ));
_computeCaretPrototype(); _computeCaretPrototype();
} }
CursorPainter get _cursorPainter => CursorPainter( CursorPainter get _cursorPainter => CursorPainter(
body, _body,
cursorCont.style, cursorCont.style,
_caretPrototype, _caretPrototype,
cursorCont.cursorColor.value, cursorCont.cursorColor.value,
@ -663,20 +663,20 @@ class RenderEditableTextLine extends RenderEditableBox {
@override @override
paint(PaintingContext context, Offset offset) { paint(PaintingContext context, Offset offset) {
if (leading != null) { if (_leading != null) {
final parentData = leading.parentData as BoxParentData; final parentData = _leading.parentData as BoxParentData;
final effectiveOffset = offset + parentData.offset; final effectiveOffset = offset + parentData.offset;
context.paintChild(leading, effectiveOffset); context.paintChild(_leading, effectiveOffset);
} }
if (body != null) { if (_body != null) {
final parentData = body.parentData as BoxParentData; final parentData = _body.parentData as BoxParentData;
final effectiveOffset = offset + parentData.offset; final effectiveOffset = offset + parentData.offset;
if ((enableInteractiveSelection ?? true) && if ((enableInteractiveSelection ?? true) &&
line.getDocumentOffset() <= textSelection.end && line.getDocumentOffset() <= textSelection.end &&
textSelection.start <= line.getDocumentOffset() + line.length - 1) { textSelection.start <= line.getDocumentOffset() + line.length - 1) {
final local = localSelection(line, textSelection, false); final local = localSelection(line, textSelection, false);
_selectedRects ??= body.getBoxesForSelection( _selectedRects ??= _body.getBoxesForSelection(
local, local,
); );
_paintSelection(context, effectiveOffset); _paintSelection(context, effectiveOffset);
@ -689,7 +689,7 @@ class RenderEditableTextLine extends RenderEditableBox {
_paintCursor(context, effectiveOffset); _paintCursor(context, effectiveOffset);
} }
context.paintChild(body, effectiveOffset); context.paintChild(_body, effectiveOffset);
if (hasFocus && if (hasFocus &&
cursorCont.show.value && cursorCont.show.value &&
@ -792,10 +792,10 @@ class _TextLineElement extends RenderObjectElement {
_updateRenderObject(RenderObject child, TextLineSlot slot) { _updateRenderObject(RenderObject child, TextLineSlot slot) {
switch (slot) { switch (slot) {
case TextLineSlot.LEADING: case TextLineSlot.LEADING:
renderObject.leading = child as RenderBox; renderObject.setLeading(child as RenderBox);
break; break;
case TextLineSlot.BODY: case TextLineSlot.BODY:
renderObject.body = child as RenderBox; renderObject.setBody(child as RenderBox);
break; break;
default: default:
throw UnimplementedError(); throw UnimplementedError();

Loading…
Cancel
Save