Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit 58007fc

Browse files
authored
Fix debugPaintSize throws 'Null Check error' (#106108)
1 parent d08a1b0 commit 58007fc

File tree

3 files changed

+93
-8
lines changed

3 files changed

+93
-8
lines changed

packages/flutter/lib/src/rendering/box.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2738,6 +2738,7 @@ abstract class RenderBox extends RenderObject {
27382738
///
27392739
/// Called for every [RenderBox] when [debugPaintSizeEnabled] is true.
27402740
@protected
2741+
@visibleForTesting
27412742
void debugPaintSize(PaintingContext context, Offset offset) {
27422743
assert(() {
27432744
final Paint paint = Paint()

packages/flutter/lib/src/rendering/proxy_box.dart

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1580,8 +1580,10 @@ class RenderClipRect extends _RenderCustomClip<Rect> {
15801580
assert(() {
15811581
if (child != null) {
15821582
super.debugPaintSize(context, offset);
1583-
context.canvas.drawRect(_clip!.shift(offset), _debugPaint!);
1584-
_debugText!.paint(context.canvas, offset + Offset(_clip!.width / 8.0, -_debugText!.text!.style!.fontSize! * 1.1));
1583+
if (clipBehavior != Clip.none) {
1584+
context.canvas.drawRect(_clip!.shift(offset), _debugPaint!);
1585+
_debugText!.paint(context.canvas, offset + Offset(_clip!.width / 8.0, -_debugText!.text!.style!.fontSize! * 1.1));
1586+
}
15851587
}
15861588
return true;
15871589
}());
@@ -1687,8 +1689,10 @@ class RenderClipRRect extends _RenderCustomClip<RRect> {
16871689
assert(() {
16881690
if (child != null) {
16891691
super.debugPaintSize(context, offset);
1690-
context.canvas.drawRRect(_clip!.shift(offset), _debugPaint!);
1691-
_debugText!.paint(context.canvas, offset + Offset(_clip!.tlRadiusX, -_debugText!.text!.style!.fontSize! * 1.1));
1692+
if (clipBehavior != Clip.none) {
1693+
context.canvas.drawRRect(_clip!.shift(offset), _debugPaint!);
1694+
_debugText!.paint(context.canvas, offset + Offset(_clip!.tlRadiusX, -_debugText!.text!.style!.fontSize! * 1.1));
1695+
}
16921696
}
16931697
return true;
16941698
}());
@@ -1773,8 +1777,10 @@ class RenderClipOval extends _RenderCustomClip<Rect> {
17731777
assert(() {
17741778
if (child != null) {
17751779
super.debugPaintSize(context, offset);
1776-
context.canvas.drawPath(_getClipPath(_clip!).shift(offset), _debugPaint!);
1777-
_debugText!.paint(context.canvas, offset + Offset((_clip!.width - _debugText!.width) / 2.0, -_debugText!.text!.style!.fontSize! * 1.1));
1780+
if (clipBehavior != Clip.none) {
1781+
context.canvas.drawPath(_getClipPath(_clip!).shift(offset), _debugPaint!);
1782+
_debugText!.paint(context.canvas, offset + Offset((_clip!.width - _debugText!.width) / 2.0, -_debugText!.text!.style!.fontSize! * 1.1));
1783+
}
17781784
}
17791785
return true;
17801786
}());
@@ -1851,8 +1857,10 @@ class RenderClipPath extends _RenderCustomClip<Path> {
18511857
assert(() {
18521858
if (child != null) {
18531859
super.debugPaintSize(context, offset);
1854-
context.canvas.drawPath(_clip!.shift(offset), _debugPaint!);
1855-
_debugText!.paint(context.canvas, offset);
1860+
if (clipBehavior != Clip.none) {
1861+
context.canvas.drawPath(_clip!.shift(offset), _debugPaint!);
1862+
_debugText!.paint(context.canvas, offset);
1863+
}
18561864
}
18571865
return true;
18581866
}());

packages/flutter/test/rendering/proxy_box_test.dart

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import 'package:flutter/material.dart';
99
import 'package:flutter/rendering.dart';
1010
import 'package:flutter_test/flutter_test.dart';
1111

12+
import 'mock_canvas.dart';
1213
import 'rendering_tester.dart';
1314

1415
void main() {
@@ -770,6 +771,81 @@ void main() {
770771
Offset.zero & renderClipRect.size,
771772
);
772773
});
774+
775+
// Simulate painting a RenderBox as if 'debugPaintSizeEnabled == true'
776+
Function(PaintingContext, Offset) debugPaint(RenderBox renderBox) {
777+
layout(renderBox);
778+
pumpFrame(phase: EnginePhase.compositingBits);
779+
return (PaintingContext context, Offset offset) {
780+
renderBox.paint(context, offset);
781+
renderBox.debugPaintSize(context, offset);
782+
};
783+
}
784+
785+
test('RenderClipPath.debugPaintSize draws a path and a debug text when clipBehavior is not Clip.none', () {
786+
Function(PaintingContext, Offset) debugPaintClipRect(Clip clip) {
787+
final RenderBox child = RenderConstrainedBox(additionalConstraints: const BoxConstraints.tightFor(width: 200, height: 200));
788+
final RenderClipPath renderClipPath = RenderClipPath(clipBehavior: clip, child: child);
789+
return debugPaint(renderClipPath);
790+
}
791+
792+
// RenderClipPath.debugPaintSize draws when clipBehavior is not Clip.none
793+
expect(debugPaintClipRect(Clip.hardEdge), paintsExactlyCountTimes(#drawPath, 1));
794+
expect(debugPaintClipRect(Clip.hardEdge), paintsExactlyCountTimes(#drawParagraph, 1));
795+
796+
// RenderClipPath.debugPaintSize does not draw when clipBehavior is Clip.none
797+
// Regression test for https://github.com/flutter/flutter/issues/105969
798+
expect(debugPaintClipRect(Clip.none), paintsExactlyCountTimes(#drawPath, 0));
799+
expect(debugPaintClipRect(Clip.none), paintsExactlyCountTimes(#drawParagraph, 0));
800+
});
801+
802+
test('RenderClipRect.debugPaintSize draws a rect and a debug text when clipBehavior is not Clip.none', () {
803+
Function(PaintingContext, Offset) debugPaintClipRect(Clip clip) {
804+
final RenderBox child = RenderConstrainedBox(additionalConstraints: const BoxConstraints.tightFor(width: 200, height: 200));
805+
final RenderClipRect renderClipRect = RenderClipRect(clipBehavior: clip, child: child);
806+
return debugPaint(renderClipRect);
807+
}
808+
809+
// RenderClipRect.debugPaintSize draws when clipBehavior is not Clip.none
810+
expect(debugPaintClipRect(Clip.hardEdge), paintsExactlyCountTimes(#drawRect, 1));
811+
expect(debugPaintClipRect(Clip.hardEdge), paintsExactlyCountTimes(#drawParagraph, 1));
812+
813+
// RenderClipRect.debugPaintSize does not draw when clipBehavior is Clip.none
814+
expect(debugPaintClipRect(Clip.none), paintsExactlyCountTimes(#drawRect, 0));
815+
expect(debugPaintClipRect(Clip.none), paintsExactlyCountTimes(#drawParagraph, 0));
816+
});
817+
818+
test('RenderClipRRect.debugPaintSize draws a rounded rect and a debug text when clipBehavior is not Clip.none', () {
819+
Function(PaintingContext, Offset) debugPaintClipRRect(Clip clip) {
820+
final RenderBox child = RenderConstrainedBox(additionalConstraints: const BoxConstraints.tightFor(width: 200, height: 200));
821+
final RenderClipRRect renderClipRRect = RenderClipRRect(clipBehavior: clip, child: child);
822+
return debugPaint(renderClipRRect);
823+
}
824+
825+
// RenderClipRRect.debugPaintSize draws when clipBehavior is not Clip.none
826+
expect(debugPaintClipRRect(Clip.hardEdge), paintsExactlyCountTimes(#drawRRect, 1));
827+
expect(debugPaintClipRRect(Clip.hardEdge), paintsExactlyCountTimes(#drawParagraph, 1));
828+
829+
// RenderClipRRect.debugPaintSize does not draw when clipBehavior is Clip.none
830+
expect(debugPaintClipRRect(Clip.none), paintsExactlyCountTimes(#drawRRect, 0));
831+
expect(debugPaintClipRRect(Clip.none), paintsExactlyCountTimes(#drawParagraph, 0));
832+
});
833+
834+
test('RenderClipOval.debugPaintSize draws a path and a debug text when clipBehavior is not Clip.none', () {
835+
Function(PaintingContext, Offset) debugPaintClipOval(Clip clip) {
836+
final RenderBox child = RenderConstrainedBox(additionalConstraints: const BoxConstraints.tightFor(width: 200, height: 200));
837+
final RenderClipOval renderClipOval = RenderClipOval(clipBehavior: clip, child: child);
838+
return debugPaint(renderClipOval);
839+
}
840+
841+
// RenderClipOval.debugPaintSize draws when clipBehavior is not Clip.none
842+
expect(debugPaintClipOval(Clip.hardEdge), paintsExactlyCountTimes(#drawPath, 1));
843+
expect(debugPaintClipOval(Clip.hardEdge), paintsExactlyCountTimes(#drawParagraph, 1));
844+
845+
// RenderClipOval.debugPaintSize does not draw when clipBehavior is Clip.none
846+
expect(debugPaintClipOval(Clip.none), paintsExactlyCountTimes(#drawPath, 0));
847+
expect(debugPaintClipOval(Clip.none), paintsExactlyCountTimes(#drawParagraph, 0));
848+
});
773849
}
774850

775851
class _TestRectClipper extends CustomClipper<Rect> {

0 commit comments

Comments
 (0)