From 7a52116d05d77c75dd70fcd2aa74610bfc624508 Mon Sep 17 00:00:00 2001 From: Ian Hickson Date: Wed, 10 May 2017 16:27:02 -0700 Subject: [PATCH] Make PhysicalModel more convenient, and add debug info (#9975) --- .../flutter/lib/src/material/material.dart | 2 +- .../lib/src/material/mergeable_material.dart | 2 +- .../flutter/lib/src/painting/box_painter.dart | 40 ++++++++++++ .../flutter/lib/src/rendering/proxy_box.dart | 53 +++++++++++----- packages/flutter/lib/src/rendering/stack.dart | 3 +- packages/flutter/lib/src/widgets/banner.dart | 20 ++++++ packages/flutter/lib/src/widgets/basic.dart | 61 +++++++++++++++++-- .../lib/src/widgets/implicit_animations.dart | 2 +- .../lib/src/widgets/scroll_configuration.dart | 9 +++ 9 files changed, 169 insertions(+), 23 deletions(-) diff --git a/packages/flutter/lib/src/material/material.dart b/packages/flutter/lib/src/material/material.dart index a0f2a14fb5..92ea645b75 100644 --- a/packages/flutter/lib/src/material/material.dart +++ b/packages/flutter/lib/src/material/material.dart @@ -173,7 +173,7 @@ class Material extends StatefulWidget { void debugFillDescription(List description) { super.debugFillDescription(description); description.add('$type'); - description.add('elevation: $elevation'); + description.add('elevation: ${elevation.toStringAsFixed(1)}'); if (color != null) description.add('color: $color'); if (textStyle != null) { diff --git a/packages/flutter/lib/src/material/mergeable_material.dart b/packages/flutter/lib/src/material/mergeable_material.dart index 8ffb1a62dc..4c6093fb2b 100644 --- a/packages/flutter/lib/src/material/mergeable_material.dart +++ b/packages/flutter/lib/src/material/mergeable_material.dart @@ -120,7 +120,7 @@ class MergeableMaterial extends StatefulWidget { @override String toString() { return 'MergeableMaterial(' - 'key: $key, mainAxis: $mainAxis, elevation: $elevation' + 'key: $key, mainAxis: $mainAxis, elevation: ${elevation.toStringAsFixed(1)}' ')'; } diff --git a/packages/flutter/lib/src/painting/box_painter.dart b/packages/flutter/lib/src/painting/box_painter.dart index f0e4482bfd..4df30daf39 100644 --- a/packages/flutter/lib/src/painting/box_painter.dart +++ b/packages/flutter/lib/src/painting/box_painter.dart @@ -136,6 +136,46 @@ class BorderRadius { @override String toString() { + if (topLeft == topRight && + topRight == bottomRight && + bottomRight == bottomLeft) { + if (topLeft == Radius.zero) + return 'BorderRadius.zero'; + if (topLeft.x == topLeft.y) + return 'BorderRadius.circular(${topLeft.x.toStringAsFixed(1)})'; + return 'BorderRadius.all($topLeft)'; + } + if (topLeft == Radius.zero || + topRight == Radius.zero || + bottomLeft == Radius.zero || + bottomRight == Radius.zero) { + final StringBuffer result = new StringBuffer(); + result.write('BorderRadius.only('); + bool comma = false; + if (topLeft != Radius.zero) { + result.write('topLeft: $topLeft'); + comma = true; + } + if (topRight != Radius.zero) { + if (comma) + result.write(', '); + result.write('topRight: $topRight'); + comma = true; + } + if (bottomLeft != Radius.zero) { + if (comma) + result.write(', '); + result.write('bottomLeft: $bottomLeft'); + comma = true; + } + if (bottomRight != Radius.zero) { + if (comma) + result.write(', '); + result.write('bottomRight: $bottomRight'); + } + result.write(')'); + return result.toString(); + } return 'BorderRadius($topLeft, $topRight, $bottomRight, $bottomLeft)'; } } diff --git a/packages/flutter/lib/src/rendering/proxy_box.dart b/packages/flutter/lib/src/rendering/proxy_box.dart index 42184b710c..1b60e85f05 100644 --- a/packages/flutter/lib/src/rendering/proxy_box.dart +++ b/packages/flutter/lib/src/rendering/proxy_box.dart @@ -926,6 +926,9 @@ abstract class CustomClipper { /// [shouldReclip] returns false or if the [shouldReclip] method is never /// called at all (e.g. if the box changes size). bool shouldReclip(covariant CustomClipper oldClipper); + + @override + String toString() => '$runtimeType'; } abstract class _RenderCustomClip extends RenderProxyBox { @@ -1190,32 +1193,39 @@ class RenderClipPath extends _RenderCustomClip { /// Creates a physical model layer that clips its children to a rounded /// rectangle. +/// +/// A physical model layer casts a shadow based on its [elevation]. class RenderPhysicalModel extends _RenderCustomClip { /// Creates a rounded-rectangular clip. /// - /// The [borderRadius] defaults to [BorderRadius.zero], i.e. a rectangle with - /// right-angled corners. + /// The [color] is required. + /// + /// The [shape], [elevation], and [color] must not be null. RenderPhysicalModel({ RenderBox child, - BoxShape shape, - BorderRadius borderRadius: BorderRadius.zero, - double elevation, - Color color, + BoxShape shape: BoxShape.rectangle, + BorderRadius borderRadius, + double elevation: 0.0, + @required Color color, }) : _shape = shape, _borderRadius = borderRadius, _elevation = elevation, _color = color, super(child: child) { - if (shape == BoxShape.rectangle) - assert(_borderRadius != null); + assert(shape != null); + assert(elevation != null); + assert(color != null); } /// The shape of the layer. + /// + /// Defaults to [BoxShape.rectangle]. The [borderRadius] affects the corners + /// of the rectangle. BoxShape get shape => _shape; BoxShape _shape; set shape(BoxShape value) { assert(value != null); - if (_shape == value) + if (shape == value) return; _shape = value; _markNeedsClip(); @@ -1225,11 +1235,14 @@ class RenderPhysicalModel extends _RenderCustomClip { /// /// Values are clamped so that horizontal and vertical radii sums do not /// exceed width/height. + /// + /// This property is ignored if the [shape] is not [BoxShape.rectangle]. + /// + /// The value null is treated like [BorderRadius.zero]. BorderRadius get borderRadius => _borderRadius; BorderRadius _borderRadius; set borderRadius(BorderRadius value) { - assert(value != null); - if (_borderRadius == value) + if (borderRadius == value) return; _borderRadius = value; _markNeedsClip(); @@ -1240,7 +1253,7 @@ class RenderPhysicalModel extends _RenderCustomClip { double _elevation; set elevation(double value) { assert(value != null); - if (_elevation == value) + if (elevation == value) return; _elevation = value; markNeedsPaint(); @@ -1251,7 +1264,7 @@ class RenderPhysicalModel extends _RenderCustomClip { Color _color; set color(Color value) { assert(value != null); - if (_color == value) + if (color == value) return; _color = value; markNeedsPaint(); @@ -1259,8 +1272,9 @@ class RenderPhysicalModel extends _RenderCustomClip { @override RRect get _defaultClip { + assert(hasSize); if (_shape == BoxShape.rectangle) { - return _borderRadius.toRRect(Offset.zero & size); + return (borderRadius ?? BorderRadius.zero).toRRect(Offset.zero & size); } else { final Rect rect = Offset.zero & size; return new RRect.fromRectXY(rect, rect.width / 2, rect.height / 2); @@ -1282,9 +1296,18 @@ class RenderPhysicalModel extends _RenderCustomClip { void paint(PaintingContext context, Offset offset) { if (child != null) { _updateClip(); - context.pushPhysicalModel(needsCompositing, offset, _clip.outerRect, _clip, _elevation, _color, super.paint); + context.pushPhysicalModel(needsCompositing, offset, _clip.outerRect, _clip, elevation, color, super.paint); } } + + @override + void debugFillDescription(List description) { + super.debugFillDescription(description); + description.add('shape: $shape'); + description.add('borderRadius: $borderRadius'); + description.add('elevation: ${elevation.toStringAsFixed(1)}'); + description.add('color: $color'); + } } /// Where to paint a box decoration. diff --git a/packages/flutter/lib/src/rendering/stack.dart b/packages/flutter/lib/src/rendering/stack.dart index be3eeef0be..97f93ea15a 100644 --- a/packages/flutter/lib/src/rendering/stack.dart +++ b/packages/flutter/lib/src/rendering/stack.dart @@ -526,8 +526,9 @@ class RenderStack extends RenderBox @override void debugFillDescription(List description) { super.debugFillDescription(description); - description.add('overflow: $overflow'); description.add('alignment: $alignment'); + description.add('fit: $fit'); + description.add('overflow: $overflow'); } } diff --git a/packages/flutter/lib/src/widgets/banner.dart b/packages/flutter/lib/src/widgets/banner.dart index 3de839deb2..ccd08f601b 100644 --- a/packages/flutter/lib/src/widgets/banner.dart +++ b/packages/flutter/lib/src/widgets/banner.dart @@ -207,6 +207,15 @@ class Banner extends StatelessWidget { child: child, ); } + + @override + void debugFillDescription(List description) { + super.debugFillDescription(description); + description.add('"$message"'); + description.add('$location'); + description.add('$color'); + '$textStyle'.split('\n').map((String value) => 'text $value').forEach(description.add); + } } /// Displays a [Banner] saying "SLOW MODE" when running in checked mode. @@ -234,4 +243,15 @@ class CheckedModeBanner extends StatelessWidget { }); return result; } + + @override + void debugFillDescription(List description) { + super.debugFillDescription(description); + String message = 'disabled'; + assert(() { + message = '"SLOW MODE"'; + return true; + }); + description.add(message); + } } diff --git a/packages/flutter/lib/src/widgets/basic.dart b/packages/flutter/lib/src/widgets/basic.dart index 9e397ef719..10ab47c12b 100644 --- a/packages/flutter/lib/src/widgets/basic.dart +++ b/packages/flutter/lib/src/widgets/basic.dart @@ -328,6 +328,13 @@ class ClipRect extends SingleChildRenderObjectWidget { void didUnmountRenderObject(RenderClipRect renderObject) { renderObject.clipper = null; } + + @override + void debugFillDescription(List description) { + super.debugFillDescription(description); + if (clipper != null) + description.add('clipper: $clipper'); + } } /// A widget that clips its child using a rounded rectangle. @@ -377,6 +384,15 @@ class ClipRRect extends SingleChildRenderObjectWidget { ..borderRadius = borderRadius ..clipper = clipper; } + + @override + void debugFillDescription(List description) { + super.debugFillDescription(description); + if (borderRadius != null) + description.add('$borderRadius'); + if (clipper != null) + description.add('clipper: $clipper'); + } } /// A widget that clips its child using an oval. @@ -422,6 +438,13 @@ class ClipOval extends SingleChildRenderObjectWidget { void didUnmountRenderObject(RenderClipOval renderObject) { renderObject.clipper = null; } + + @override + void debugFillDescription(List description) { + super.debugFillDescription(description); + if (clipper != null) + description.add('clipper: $clipper'); + } } /// A widget that clips its child using a path. @@ -464,20 +487,33 @@ class ClipPath extends SingleChildRenderObjectWidget { void didUnmountRenderObject(RenderClipPath renderObject) { renderObject.clipper = null; } + + @override + void debugFillDescription(List description) { + super.debugFillDescription(description); + if (clipper != null) + description.add('clipper: $clipper'); + } } /// A widget representing a physical layer that clips its children to a shape. +/// +/// Physical layers cast shadows based on an [elevation] which is nominally in +/// logical pixels, coming vertically out of the rendering surface. class PhysicalModel extends SingleChildRenderObjectWidget { /// Creates a physical model with a rounded-rectangular clip. + /// + /// The [color] is required; physical things have a color. + /// + /// The [shape], [elevation], and [color] must not be null. const PhysicalModel({ Key key, - @required this.shape, - this.borderRadius: BorderRadius.zero, - @required this.elevation, + this.shape: BoxShape.rectangle, + this.borderRadius, + this.elevation: 0.0, @required this.color, Widget child, }) : assert(shape != null), - assert(borderRadius != null), assert(elevation != null), assert(color != null), super(key: key, child: child); @@ -510,6 +546,15 @@ class PhysicalModel extends SingleChildRenderObjectWidget { ..elevation = elevation ..color = color; } + + @override + void debugFillDescription(List description) { + super.debugFillDescription(description); + description.add('shape: $shape'); + description.add('borderRadius: $borderRadius'); + description.add('elevation: ${elevation.toStringAsFixed(1)}'); + description.add('color: $color'); + } } // POSITIONING AND SIZING NODES @@ -1712,6 +1757,14 @@ class Stack extends MultiChildRenderObjectWidget { ..fit = fit ..overflow = overflow; } + + @override + void debugFillDescription(List description) { + super.debugFillDescription(description); + description.add('alignment: $alignment'); + description.add('fit: $fit'); + description.add('overflow: $overflow'); + } } /// A [Stack] that shows a single child from a list of children. diff --git a/packages/flutter/lib/src/widgets/implicit_animations.dart b/packages/flutter/lib/src/widgets/implicit_animations.dart index e3d3747c4f..3753638b93 100644 --- a/packages/flutter/lib/src/widgets/implicit_animations.dart +++ b/packages/flutter/lib/src/widgets/implicit_animations.dart @@ -761,7 +761,7 @@ class AnimatedPhysicalModel extends ImplicitlyAnimatedWidget { super.debugFillDescription(description); description.add('shape: $shape'); description.add('borderRadius: $borderRadius'); - description.add('elevation: $elevation'); + description.add('elevation: ${elevation.toStringAsFixed(1)}'); description.add('color: $color'); description.add('animateColor: $animateColor'); } diff --git a/packages/flutter/lib/src/widgets/scroll_configuration.dart b/packages/flutter/lib/src/widgets/scroll_configuration.dart index f2e3ed3731..1438f3b6ce 100644 --- a/packages/flutter/lib/src/widgets/scroll_configuration.dart +++ b/packages/flutter/lib/src/widgets/scroll_configuration.dart @@ -73,6 +73,9 @@ class ScrollBehavior { /// [ScrollConfiguration] will rebuild using the new [ScrollBehavior]. If this /// method returns false, the rebuilds might be optimized away. bool shouldNotify(covariant ScrollBehavior oldDelegate) => false; + + @override + String toString() => '$runtimeType'; } /// Controls how [Scrollable] widgets behave in a subtree. @@ -107,4 +110,10 @@ class ScrollConfiguration extends InheritedWidget { return behavior.runtimeType != oldWidget.behavior.runtimeType || (behavior != oldWidget.behavior && behavior.shouldNotify(oldWidget.behavior)); } + + @override + void debugFillDescription(List description) { + super.debugFillDescription(description); + description.add('behavior: $behavior'); + } }