@@ -383,6 +383,7 @@ class RenderEditable extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
383383 TextSelectionDelegate textSelectionDelegate;
384384
385385 Rect ? _lastCaretRect;
386+ late Rect _currentCaretRect;
386387
387388 /// Track whether position of the start of the selected text is within the viewport.
388389 ///
@@ -2124,7 +2125,7 @@ class RenderEditable extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
21242125 return Offset (pixelPerfectOffsetX, pixelPerfectOffsetY);
21252126 }
21262127
2127- void _paintCaret (Canvas canvas, Offset effectiveOffset, TextPosition textPosition) {
2128+ void _paintCaretIfNeeded (Canvas canvas, Offset effectiveOffset, TextPosition textPosition) {
21282129 assert (_textLayoutLastMaxWidth == constraints.maxWidth &&
21292130 _textLayoutLastMinWidth == constraints.minWidth,
21302131 'Last width ($_textLayoutLastMinWidth , $_textLayoutLastMaxWidth ) not the same as max width constraint (${constraints .minWidth }, ${constraints .maxWidth }).' );
@@ -2171,18 +2172,24 @@ class RenderEditable extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
21712172 }
21722173
21732174 caretRect = caretRect.shift (_getPixelPerfectCursorOffset (caretRect));
2175+ _currentCaretRect = caretRect;
2176+
2177+ if (! _showCursor.value)
2178+ return ;
21742179
21752180 if (cursorRadius == null ) {
21762181 canvas.drawRect (caretRect, paint);
21772182 } else {
21782183 final RRect caretRRect = RRect .fromRectAndRadius (caretRect, cursorRadius! );
21792184 canvas.drawRRect (caretRRect, paint);
21802185 }
2186+ }
21812187
2182- if (caretRect != _lastCaretRect) {
2183- _lastCaretRect = caretRect;
2188+ void _updateCaretRect () {
2189+ if (_currentCaretRect != _lastCaretRect) {
2190+ _lastCaretRect = _currentCaretRect;
21842191 if (onCaretChanged != null )
2185- onCaretChanged !(caretRect );
2192+ onCaretChanged !(_currentCaretRect );
21862193 }
21872194 }
21882195
@@ -2333,11 +2340,11 @@ class RenderEditable extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
23332340 final Offset effectiveOffset = offset + _paintOffset;
23342341
23352342 bool showSelection = false ;
2336- bool showCaret = false ;
2343+ bool canShowCaret = false ;
23372344
23382345 if (selection != null && ! _floatingCursorOn) {
2339- if (selection! .isCollapsed && _showCursor.value && cursorColor != null )
2340- showCaret = true ;
2346+ if (selection! .isCollapsed && cursorColor != null )
2347+ canShowCaret = true ;
23412348 else if (! selection! .isCollapsed && _selectionColor != null )
23422349 showSelection = true ;
23432350 _updateSelectionExtentsVisibility (effectiveOffset);
@@ -2356,17 +2363,20 @@ class RenderEditable extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
23562363 if (paintCursorAboveText)
23572364 _textPainter.paint (context.canvas, effectiveOffset);
23582365
2359- if (showCaret ) {
2366+ if (canShowCaret ) {
23602367 assert (selection != null );
2361- _paintCaret (context.canvas, effectiveOffset, selection! .extent);
2368+ _paintCaretIfNeeded (context.canvas, effectiveOffset, selection! .extent);
2369+ _updateCaretRect ();
23622370 }
23632371
23642372 if (! paintCursorAboveText)
23652373 _textPainter.paint (context.canvas, effectiveOffset);
23662374
23672375 if (_floatingCursorOn) {
2368- if (_resetFloatingCursorAnimationValue == null )
2369- _paintCaret (context.canvas, effectiveOffset, _floatingCursorTextPosition);
2376+ if (_resetFloatingCursorAnimationValue == null ) {
2377+ _paintCaretIfNeeded (context.canvas, effectiveOffset, _floatingCursorTextPosition);
2378+ _updateCaretRect ();
2379+ }
23702380 _paintFloatingCaret (context.canvas, _floatingCursorOffset);
23712381 }
23722382 }
0 commit comments