From 13f106b026355281888666026f43bcba58ebf214 Mon Sep 17 00:00:00 2001 From: Kate Lovett Date: Wed, 2 Feb 2022 15:32:05 -0600 Subject: [PATCH] Allow Clip.none as a valid clipBehavior (#95593) --- .../flutter/lib/src/rendering/proxy_box.dart | 112 +++++++++++------- packages/flutter/lib/src/widgets/basic.dart | 50 ++++---- packages/flutter/test/widgets/clip_test.dart | 16 +++ 3 files changed, 112 insertions(+), 66 deletions(-) diff --git a/packages/flutter/lib/src/rendering/proxy_box.dart b/packages/flutter/lib/src/rendering/proxy_box.dart index 9e1caa3df8..f3b3571f12 100644 --- a/packages/flutter/lib/src/rendering/proxy_box.dart +++ b/packages/flutter/lib/src/rendering/proxy_box.dart @@ -1429,13 +1429,13 @@ class RenderClipRect extends _RenderCustomClip { /// If [clipper] is null, the clip will match the layout size and position of /// the child. /// - /// The [clipBehavior] must not be null or [Clip.none]. + /// The [clipBehavior] must not be null. If [clipBehavior] is + /// [Clip.none], no clipping will be applied. RenderClipRect({ RenderBox? child, CustomClipper? clipper, Clip clipBehavior = Clip.antiAlias, }) : assert(clipBehavior != null), - assert(clipBehavior != Clip.none), super(child: child, clipper: clipper, clipBehavior: clipBehavior); @override @@ -1455,15 +1455,20 @@ class RenderClipRect extends _RenderCustomClip { @override void paint(PaintingContext context, Offset offset) { if (child != null) { - _updateClip(); - layer = context.pushClipRect( - needsCompositing, - offset, - _clip!, - super.paint, - clipBehavior: clipBehavior, - oldLayer: layer as ClipRectLayer?, - ); + if (clipBehavior != Clip.none) { + _updateClip(); + layer = context.pushClipRect( + needsCompositing, + offset, + _clip!, + super.paint, + clipBehavior: clipBehavior, + oldLayer: layer as ClipRectLayer?, + ); + } else { + context.paintChild(child!, offset); + layer = null; + } } else { layer = null; } @@ -1495,14 +1500,14 @@ class RenderClipRRect extends _RenderCustomClip { /// /// If [clipper] is non-null, then [borderRadius] is ignored. /// - /// The [clipBehavior] argument must not be null or [Clip.none]. + /// The [clipBehavior] argument must not be null. If [clipBehavior] is + /// [Clip.none], no clipping will be applied. RenderClipRRect({ RenderBox? child, BorderRadius borderRadius = BorderRadius.zero, CustomClipper? clipper, Clip clipBehavior = Clip.antiAlias, }) : assert(clipBehavior != null), - assert(clipBehavior != Clip.none), _borderRadius = borderRadius, super(child: child, clipper: clipper, clipBehavior: clipBehavior) { assert(_borderRadius != null || clipper != null); @@ -1541,14 +1546,21 @@ class RenderClipRRect extends _RenderCustomClip { @override void paint(PaintingContext context, Offset offset) { if (child != null) { - _updateClip(); - layer = context.pushClipRRect( - needsCompositing, - offset, - _clip!.outerRect, - _clip!, - super.paint, clipBehavior: clipBehavior, oldLayer: layer as ClipRRectLayer?, - ); + if (clipBehavior != Clip.none) { + _updateClip(); + layer = context.pushClipRRect( + needsCompositing, + offset, + _clip!.outerRect, + _clip!, + super.paint, + clipBehavior: clipBehavior, + oldLayer: layer as ClipRRectLayer?, + ); + } else { + context.paintChild(child!, offset); + layer = null; + } } else { layer = null; } @@ -1578,13 +1590,13 @@ class RenderClipOval extends _RenderCustomClip { /// If [clipper] is null, the oval will be inscribed into the layout size and /// position of the child. /// - /// The [clipBehavior] argument must not be null or [Clip.none]. + /// The [clipBehavior] argument must not be null. If [clipBehavior] is + /// [Clip.none], no clipping will be applied. RenderClipOval({ RenderBox? child, CustomClipper? clipper, Clip clipBehavior = Clip.antiAlias, }) : assert(clipBehavior != null), - assert(clipBehavior != Clip.none), super(child: child, clipper: clipper, clipBehavior: clipBehavior); Rect? _cachedRect; @@ -1620,16 +1632,21 @@ class RenderClipOval extends _RenderCustomClip { @override void paint(PaintingContext context, Offset offset) { if (child != null) { - _updateClip(); - layer = context.pushClipPath( - needsCompositing, - offset, - _clip!, - _getClipPath(_clip!), - super.paint, - clipBehavior: clipBehavior, - oldLayer: layer as ClipPathLayer?, - ); + if (clipBehavior != Clip.none) { + _updateClip(); + layer = context.pushClipPath( + needsCompositing, + offset, + _clip!, + _getClipPath(_clip!), + super.paint, + clipBehavior: clipBehavior, + oldLayer: layer as ClipPathLayer?, + ); + } else { + context.paintChild(child!, offset); + layer = null; + } } else { layer = null; } @@ -1667,13 +1684,13 @@ class RenderClipPath extends _RenderCustomClip { /// consider using a [RenderClipRect], which can achieve the same effect more /// efficiently. /// - /// The [clipBehavior] argument must not be null or [Clip.none]. + /// The [clipBehavior] argument must not be null. If [clipBehavior] is + /// [Clip.none], no clipping will be applied. RenderClipPath({ RenderBox? child, CustomClipper? clipper, Clip clipBehavior = Clip.antiAlias, }) : assert(clipBehavior != null), - assert(clipBehavior != Clip.none), super(child: child, clipper: clipper, clipBehavior: clipBehavior); @override @@ -1693,16 +1710,21 @@ class RenderClipPath extends _RenderCustomClip { @override void paint(PaintingContext context, Offset offset) { if (child != null) { - _updateClip(); - layer = context.pushClipPath( - needsCompositing, - offset, - Offset.zero & size, - _clip!, - super.paint, - clipBehavior: clipBehavior, - oldLayer: layer as ClipPathLayer?, - ); + if (clipBehavior != Clip.none) { + _updateClip(); + layer = context.pushClipPath( + needsCompositing, + offset, + Offset.zero & size, + _clip!, + super.paint, + clipBehavior: clipBehavior, + oldLayer: layer as ClipPathLayer?, + ); + } else { + context.paintChild(child!, offset); + layer = null; + } } else { layer = null; } diff --git a/packages/flutter/lib/src/widgets/basic.dart b/packages/flutter/lib/src/widgets/basic.dart index 3b98c7bcf7..d0befb1458 100644 --- a/packages/flutter/lib/src/widgets/basic.dart +++ b/packages/flutter/lib/src/widgets/basic.dart @@ -662,10 +662,15 @@ class ClipRect extends SingleChildRenderObjectWidget { /// If [clipper] is null, the clip will match the layout size and position of /// the child. /// - /// The [clipBehavior] argument must not be null or [Clip.none]. - const ClipRect({ Key? key, this.clipper, this.clipBehavior = Clip.hardEdge, Widget? child }) - : assert(clipBehavior != null), - super(key: key, child: child); + /// The [clipBehavior] argument must not be null. If [clipBehavior] is + /// [Clip.none], no clipping will be applied. + const ClipRect({ + Key? key, + this.clipper, + this.clipBehavior = Clip.hardEdge, + Widget? child, + }) : assert(clipBehavior != null), + super(key: key, child: child); /// If non-null, determines which clip to use. final CustomClipper? clipper; @@ -677,13 +682,11 @@ class ClipRect extends SingleChildRenderObjectWidget { @override RenderClipRect createRenderObject(BuildContext context) { - assert(clipBehavior != Clip.none); return RenderClipRect(clipper: clipper, clipBehavior: clipBehavior); } @override void updateRenderObject(BuildContext context, RenderClipRect renderObject) { - assert(clipBehavior != Clip.none); renderObject ..clipper = clipper ..clipBehavior = clipBehavior; @@ -723,7 +726,8 @@ class ClipRRect extends SingleChildRenderObjectWidget { /// /// If [clipper] is non-null, then [borderRadius] is ignored. /// - /// The [clipBehavior] argument must not be null or [Clip.none]. + /// The [clipBehavior] argument must not be null. If [clipBehavior] is + /// [Clip.none], no clipping will be applied. const ClipRRect({ Key? key, this.borderRadius = BorderRadius.zero, @@ -752,13 +756,15 @@ class ClipRRect extends SingleChildRenderObjectWidget { @override RenderClipRRect createRenderObject(BuildContext context) { - assert(clipBehavior != Clip.none); - return RenderClipRRect(borderRadius: borderRadius!, clipper: clipper, clipBehavior: clipBehavior); + return RenderClipRRect( + borderRadius: borderRadius!, + clipper: clipper, + clipBehavior: clipBehavior, + ); } @override void updateRenderObject(BuildContext context, RenderClipRRect renderObject) { - assert(clipBehavior != Clip.none); renderObject ..borderRadius = borderRadius! ..clipBehavior = clipBehavior @@ -793,10 +799,15 @@ class ClipOval extends SingleChildRenderObjectWidget { /// If [clipper] is null, the oval will be inscribed into the layout size and /// position of the child. /// - /// The [clipBehavior] argument must not be null or [Clip.none]. - const ClipOval({Key? key, this.clipper, this.clipBehavior = Clip.antiAlias, Widget? child}) - : assert(clipBehavior != null), - super(key: key, child: child); + /// The [clipBehavior] argument must not be null. If [clipBehavior] is + /// [Clip.none], no clipping will be applied. + const ClipOval({ + Key? key, + this.clipper, + this.clipBehavior = Clip.antiAlias, + Widget? child, + }) : assert(clipBehavior != null), + super(key: key, child: child); /// If non-null, determines which clip to use. /// @@ -816,13 +827,11 @@ class ClipOval extends SingleChildRenderObjectWidget { @override RenderClipOval createRenderObject(BuildContext context) { - assert(clipBehavior != Clip.none); return RenderClipOval(clipper: clipper, clipBehavior: clipBehavior); } @override void updateRenderObject(BuildContext context, RenderClipOval renderObject) { - assert(clipBehavior != Clip.none); renderObject ..clipper = clipper ..clipBehavior = clipBehavior; @@ -866,7 +875,8 @@ class ClipPath extends SingleChildRenderObjectWidget { /// consider using a [ClipRect], which can achieve the same effect more /// efficiently. /// - /// The [clipBehavior] argument must not be null or [Clip.none]. + /// The [clipBehavior] argument must not be null. If [clipBehavior] is + /// [Clip.none], no clipping will be applied. const ClipPath({ Key? key, this.clipper, @@ -886,7 +896,6 @@ class ClipPath extends SingleChildRenderObjectWidget { Widget? child, }) { assert(clipBehavior != null); - assert(clipBehavior != Clip.none); assert(shape != null); return Builder( key: key, @@ -917,13 +926,11 @@ class ClipPath extends SingleChildRenderObjectWidget { @override RenderClipPath createRenderObject(BuildContext context) { - assert(clipBehavior != Clip.none); return RenderClipPath(clipper: clipper, clipBehavior: clipBehavior); } @override void updateRenderObject(BuildContext context, RenderClipPath renderObject) { - assert(clipBehavior != Clip.none); renderObject ..clipper = clipper ..clipBehavior = clipBehavior; @@ -1014,7 +1021,8 @@ class PhysicalModel extends SingleChildRenderObjectWidget { shape: shape, clipBehavior: clipBehavior, borderRadius: borderRadius, - elevation: elevation, color: color, + elevation: elevation, + color: color, shadowColor: shadowColor, ); } diff --git a/packages/flutter/test/widgets/clip_test.dart b/packages/flutter/test/widgets/clip_test.dart index 0f4861068b..49b7b51ba5 100644 --- a/packages/flutter/test/widgets/clip_test.dart +++ b/packages/flutter/test/widgets/clip_test.dart @@ -87,6 +87,10 @@ void main() { await tester.pumpWidget(const ClipRect(clipBehavior: Clip.antiAlias)); expect(renderClip.clipBehavior, equals(Clip.antiAlias)); + + await tester.pumpWidget(const ClipRect(clipBehavior: Clip.none)); + + expect(renderClip.clipBehavior, equals(Clip.none)); }); test('ClipRRect constructs with the right default values', () { @@ -105,6 +109,10 @@ void main() { await tester.pumpWidget(const ClipRRect(clipBehavior: Clip.hardEdge)); expect(renderClip.clipBehavior, equals(Clip.hardEdge)); + + await tester.pumpWidget(const ClipRRect(clipBehavior: Clip.none)); + + expect(renderClip.clipBehavior, equals(Clip.none)); }); testWidgets('ClipOval updates clipBehavior in updateRenderObject', (WidgetTester tester) async { @@ -117,6 +125,10 @@ void main() { await tester.pumpWidget(const ClipOval(clipBehavior: Clip.hardEdge)); expect(renderClip.clipBehavior, equals(Clip.hardEdge)); + + await tester.pumpWidget(const ClipOval(clipBehavior: Clip.none)); + + expect(renderClip.clipBehavior, equals(Clip.none)); }); testWidgets('ClipPath updates clipBehavior in updateRenderObject', (WidgetTester tester) async { @@ -129,6 +141,10 @@ void main() { await tester.pumpWidget(const ClipPath(clipBehavior: Clip.hardEdge)); expect(renderClip.clipBehavior, equals(Clip.hardEdge)); + + await tester.pumpWidget(const ClipPath(clipBehavior: Clip.none)); + + expect(renderClip.clipBehavior, equals(Clip.none)); }); testWidgets('ClipPath', (WidgetTester tester) async {