Skip to content

Commit

Permalink
RenderEditable WidgetSpan intrinsics (#136979)
Browse files Browse the repository at this point in the history
Update the `RenderEditable` implementation to match `RenderParagraph`. Fixes flutter/flutter#136596
  • Loading branch information
LongCatIsLooong authored Oct 20, 2023
1 parent 78b754e commit 5f47764
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
24 changes: 21 additions & 3 deletions packages/flutter/lib/src/rendering/editable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1817,12 +1817,28 @@ class RenderEditable extends RenderBox with RelayoutWhenSystemFontsChangeMixin,

@override
double computeMinIntrinsicWidth(double height) {
if (!_canComputeIntrinsics) {
return 0.0;
}
_textPainter.setPlaceholderDimensions(layoutInlineChildren(
double.infinity,
(RenderBox child, BoxConstraints constraints) => Size(child.getMinIntrinsicWidth(double.infinity), 0.0),
));
_layoutText();
return _textPainter.minIntrinsicWidth;
}

@override
double computeMaxIntrinsicWidth(double height) {
if (!_canComputeIntrinsics) {
return 0.0;
}
_textPainter.setPlaceholderDimensions(layoutInlineChildren(
double.infinity,
// Height and baseline is irrelevant as all text will be laid
// out in a single line. Therefore, using 0.0 as a dummy for the height.
(RenderBox child, BoxConstraints constraints) => Size(child.getMaxIntrinsicWidth(double.infinity), 0.0),
));
_layoutText();
return _textPainter.maxIntrinsicWidth + _caretMargin;
}
Expand Down Expand Up @@ -1890,12 +1906,14 @@ class RenderEditable extends RenderBox with RelayoutWhenSystemFontsChangeMixin,
}

@override
double computeMinIntrinsicHeight(double width) {
return _preferredHeight(width);
}
double computeMinIntrinsicHeight(double width) => computeMaxIntrinsicHeight(width);

@override
double computeMaxIntrinsicHeight(double width) {
if (!_canComputeIntrinsics) {
return 0.0;
}
_textPainter.setPlaceholderDimensions(layoutInlineChildren(width, ChildLayoutHelper.dryLayoutChild));
return _preferredHeight(width);
}

Expand Down
14 changes: 14 additions & 0 deletions packages/flutter/test/rendering/editable_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:math' as math;
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
Expand Down Expand Up @@ -1632,7 +1633,20 @@ void main() {
children: renderBoxes,
);
_applyParentData(renderBoxes, editable.text!);
// Intrinsics can be computed without doing layout.
expect(editable.computeMaxIntrinsicWidth(fixedHeight),
2.0 * 10.0 * 4 + 14.0 * 7 + 1.0,
reason: "intrinsic width = scale factor * width of 'test' + width of 'one two' + _caretMargin",
);
expect(editable.computeMinIntrinsicWidth(fixedHeight),
math.max(math.max(2.0 * 10.0 * 4, 14.0 * 3), 14.0 * 3),
reason: "intrinsic width = max(scale factor * width of 'test', width of 'one', width of 'two')",
);
expect(editable.computeMaxIntrinsicHeight(fixedHeight), 40.0);
expect(editable.computeMinIntrinsicHeight(fixedHeight), 40.0);

layout(editable, constraints: const BoxConstraints(maxWidth: screenWidth));
// Intrinsics can be computed after layout.
expect(editable.computeMaxIntrinsicWidth(fixedHeight),
2.0 * 10.0 * 4 + 14.0 * 7 + 1.0,
reason: "intrinsic width = scale factor * width of 'test' + width of 'one two' + _caretMargin",
Expand Down

0 comments on commit 5f47764

Please sign in to comment.