diff --git a/packages/flutter/lib/src/rendering/editable.dart b/packages/flutter/lib/src/rendering/editable.dart index 5d0121e3f37..a42c758b2fb 100644 --- a/packages/flutter/lib/src/rendering/editable.dart +++ b/packages/flutter/lib/src/rendering/editable.dart @@ -321,7 +321,7 @@ class RenderEditable extends RenderBox Offset cursorOffset = Offset.zero, double devicePixelRatio = 1.0, ui.BoxHeightStyle selectionHeightStyle = ui.BoxHeightStyle.max, - ui.BoxWidthStyle selectionWidthStyle = ui.BoxWidthStyle.max, + ui.BoxWidthStyle selectionWidthStyle = ui.BoxWidthStyle.tight, bool? enableInteractiveSelection, this.floatingCursorAddedMargin = const EdgeInsets.fromLTRB(4, 4, 4, 5), TextRange? promptRectRange, @@ -2912,17 +2912,31 @@ class _TextHighlightPainter extends RenderEditablePainter { highlightPaint.color = color; final TextPainter textPainter = renderEditable._textPainter; - final Set boxes = textPainter - .getBoxesForSelection( - TextSelection(baseOffset: range.start, extentOffset: range.end), - boxHeightStyle: selectionHeightStyle, - boxWidthStyle: selectionWidthStyle, - ) - .toSet(); - for (final box in boxes) { + final List boxes = textPainter.getBoxesForSelection( + TextSelection(baseOffset: range.start, extentOffset: range.end), + boxHeightStyle: ui.BoxHeightStyle.max, + boxWidthStyle: ui.BoxWidthStyle.tight, + ); + ui.TextBox? mergedBox; + for (final textBox in boxes) { + if (mergedBox == null) { + mergedBox = textBox; + } else if (mergedBox.top != textBox.top) { + canvas.drawRect( + Rect.fromLTRB(mergedBox.left, mergedBox.top, size.width, mergedBox.bottom) + .shift(renderEditable._paintOffset) + .intersect(Rect.fromLTRB(0, 0, textPainter.width, textPainter.height)), + highlightPaint, + ); + mergedBox = textBox; + } else { + mergedBox = TextBox.fromLTRBD(mergedBox.left, mergedBox.top, textBox.right, mergedBox.bottom, mergedBox.direction); + } + } + if (mergedBox != null) { canvas.drawRect( - box + mergedBox .toRect() .shift(renderEditable._paintOffset) .intersect(Rect.fromLTWH(0, 0, textPainter.width, textPainter.height)), diff --git a/packages/flutter/lib/src/widgets/editable_text.dart b/packages/flutter/lib/src/widgets/editable_text.dart index b1ae878f2cc..7b179c7e3b3 100644 --- a/packages/flutter/lib/src/widgets/editable_text.dart +++ b/packages/flutter/lib/src/widgets/editable_text.dart @@ -2099,7 +2099,7 @@ class EditableText extends StatefulWidget { } return ui.BoxWidthStyle.tight; } - return ui.BoxWidthStyle.max; + return ui.BoxWidthStyle.tight; } /// The default value for [stylusHandwritingEnabled]. @@ -5046,7 +5046,9 @@ class EditableTextState extends State void userUpdateTextEditingValue(TextEditingValue value, SelectionChangedCause? cause) { // Compare the current TextEditingValue with the pre-format new // TextEditingValue value, in case the formatter would reject the change. - final shouldShowCaret = widget.readOnly ? _value.selection != value.selection : _value != value; + final shouldShowCaret = + cause != SelectionChangedCause.drag && + (widget.readOnly ? _value.selection != value.selection : _value != value); if (shouldShowCaret) { _scheduleShowCaretOnScreen(withAnimation: true); }