From 020b068896d5d522c1003021b7c2d50648e556e2 Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Wed, 1 Jun 2016 08:27:19 -0700 Subject: [PATCH] Use protected and mustCallSuper in more places (#4291) --- packages/flutter/lib/src/rendering/node.dart | 2 + .../flutter/lib/src/rendering/object.dart | 11 ++++- .../flutter/lib/src/rendering/viewport.dart | 10 +++-- .../flutter/lib/src/widgets/framework.dart | 43 ++++++++++++++++--- .../lib/src/markdown_raw.dart | 1 + 5 files changed, 56 insertions(+), 11 deletions(-) diff --git a/packages/flutter/lib/src/rendering/node.dart b/packages/flutter/lib/src/rendering/node.dart index 865e754cfa..2d52e90fb0 100644 --- a/packages/flutter/lib/src/rendering/node.dart +++ b/packages/flutter/lib/src/rendering/node.dart @@ -97,6 +97,7 @@ class AbstractNode { AbstractNode get parent => _parent; /// Subclasses should call this function when they acquire a new child. + @mustCallSuper void adoptChild(AbstractNode child) { assert(child != null); assert(child._parent == null); @@ -114,6 +115,7 @@ class AbstractNode { } /// Subclasses should call this function when they lose a child. + @mustCallSuper void dropChild(AbstractNode child) { assert(child != null); assert(child._parent == this); diff --git a/packages/flutter/lib/src/rendering/object.dart b/packages/flutter/lib/src/rendering/object.dart index a551cfabc7..01b9202bc4 100644 --- a/packages/flutter/lib/src/rendering/object.dart +++ b/packages/flutter/lib/src/rendering/object.dart @@ -1169,19 +1169,24 @@ abstract class RenderObject extends AbstractNode implements HitTestTarget { } } - bool _needsLayout = true; /// Whether this render object's layout information is dirty. bool get needsLayout => _needsLayout; + bool _needsLayout = true; + RenderObject _relayoutSubtreeRoot; bool _doingThisLayoutWithCallback = false; - Constraints _constraints; + /// The layout constraints most recently supplied by the parent. + @protected Constraints get constraints => _constraints; + Constraints _constraints; + /// Verify that the object's constraints are being met. Override /// this function in a subclass to verify that your state matches /// the constraints object. This function is only called in checked /// mode and only when needsLayout is false. If the constraints are /// not met, it should assert or throw an exception. + @protected void debugAssertDoesMeetConstraints(); /// When true, debugAssertDoesMeetConstraints() is currently @@ -1449,6 +1454,7 @@ abstract class RenderObject extends AbstractNode implements HitTestTarget { /// Returning false is always correct, but returning true can be more /// efficient when computing the size of this render object because we don't /// need to recompute the size if the constraints don't change. + @protected bool get sizedByParent => false; /// Updates the render objects size using only the constraints. @@ -1566,6 +1572,7 @@ abstract class RenderObject extends AbstractNode implements HitTestTarget { /// /// You must call markNeedsCompositingBitsUpdate() if the value of this /// getter changes. + @protected bool get alwaysNeedsCompositing => false; OffsetLayer _layer; diff --git a/packages/flutter/lib/src/rendering/viewport.dart b/packages/flutter/lib/src/rendering/viewport.dart index b2d31ea984..129f1a666a 100644 --- a/packages/flutter/lib/src/rendering/viewport.dart +++ b/packages/flutter/lib/src/rendering/viewport.dart @@ -4,6 +4,7 @@ import 'dart:ui' as ui show window; +import 'package:meta/meta.dart'; import 'package:vector_math/vector_math_64.dart'; import 'box.dart'; @@ -355,6 +356,7 @@ abstract class RenderVirtualViewport description) { } + /// Lets [Element.debugFillDescription] call the @protected method + /// [debugFillDescription]. This pattern prevents other unwanted callers + /// outside this library. + void _debugFillDescription(List description) { + debugFillDescription(description); + } + static bool canUpdate(Widget oldWidget, Widget newWidget) { return oldWidget.runtimeType == newWidget.runtimeType && oldWidget.key == newWidget.key; @@ -315,6 +324,7 @@ abstract class StatelessWidget extends Widget { /// The given build context object contains information about the location in /// the tree at which this widget is being built. For example, the context /// provides the set of inherited widgets for this location in the tree. + @protected Widget build(BuildContext context); } @@ -468,11 +478,13 @@ abstract class State { /// The context argument is always the same as [State.context]. This argument /// is provided redundantly here to match the [WidgetBuilder] function /// signature used by [StatelessWidget.build] and other widgets. + @protected Widget build(BuildContext context); /// Called when an Inherited widget in the ancestor chain has changed. Usually /// there is nothing to do here; whenever this is called, build() is also /// called. + @mustCallSuper void dependenciesChanged() { } @override @@ -482,6 +494,8 @@ abstract class State { return '$runtimeType(${data.join("; ")})'; } + @protected + @mustCallSuper void debugFillDescription(List description) { description.add('$hashCode'); assert(() { @@ -961,6 +975,7 @@ abstract class Element implements BuildContext { /// The updateChild() method returns the new child, if it had to create one, /// or the child that was passed in, if it just had to update the child, or /// null, if it removed the child and did not replace it. + @protected Element updateChild(Element child, Widget newWidget, dynamic newSlot) { if (newWidget == null) { if (child != null) @@ -1029,6 +1044,7 @@ abstract class Element implements BuildContext { /// Called by MultiChildRenderObjectElement, and other RenderObjectElement /// subclasses that have multiple children, to update the slot of a particular /// child when the child is moved in its child list. + @protected void updateSlotForChild(Element child, dynamic newSlot) { assert(_debugLifecycleState == _ElementLifecycle.active); assert(child != null); @@ -1088,6 +1104,7 @@ abstract class Element implements BuildContext { return element; } + @protected Element inflateWidget(Widget newWidget, dynamic newSlot) { assert(newWidget != null); Key key = newWidget.key; @@ -1120,6 +1137,7 @@ abstract class Element implements BuildContext { }); } + @protected void deactivateChild(Element child) { assert(child != null); assert(child._parent == this); @@ -1184,6 +1202,7 @@ abstract class Element implements BuildContext { } /// Called after children have been deactivated (see [deactivate]). + @mustCallSuper void debugDeactivated() { assert(_debugLifecycleState == _ElementLifecycle.inactive); } @@ -1271,7 +1290,8 @@ abstract class Element implements BuildContext { ancestor = ancestor._parent; } - void dependenciesChanged(); + @mustCallSuper + void dependenciesChanged() { } String debugGetCreatorChain(int limit) { List chain = []; @@ -1297,6 +1317,8 @@ abstract class Element implements BuildContext { return '$name(${data.join("; ")})'; } + @protected + @mustCallSuper void debugFillDescription(List description) { if (depth == null) description.add('no depth'); @@ -1305,7 +1327,7 @@ abstract class Element implements BuildContext { } else { if (widget.key != null) description.add('${widget.key}'); - widget.debugFillDescription(description); + widget._debugFillDescription(description); } } @@ -1348,6 +1370,7 @@ class ErrorWidget extends LeafRenderObjectWidget { @override void debugFillDescription(List description) { + super.debugFillDescription(description); description.add('message: ' + _stringify(message)); } } @@ -1465,10 +1488,12 @@ abstract class BuildableElement extends Element { } /// Called by rebuild() after the appropriate checks have been made. + @protected void performRebuild(); @override void dependenciesChanged() { + super.dependenciesChanged(); assert(_active); markNeedsBuild(); } @@ -1740,6 +1765,7 @@ abstract class _ProxyElement extends ComponentElement { rebuild(); } + @protected void notifyClients(_ProxyWidget oldWidget); } @@ -1835,6 +1861,7 @@ class InheritedElement extends _ProxyElement { /// returns true. Subclasses of [InheritedElement] might wish to call this /// function at other times if their inherited information changes outside of /// the build phase. + @protected void dispatchDependenciesChanged() { for (Element dependent in _dependents) { assert(() { @@ -1886,7 +1913,7 @@ abstract class RenderObjectElement extends BuildableElement { void mount(Element parent, dynamic newSlot) { super.mount(parent, newSlot); _renderObject = widget.createRenderObject(this); - assert(() { debugUpdateRenderObjectOwner(); return true; }); + assert(() { _debugUpdateRenderObjectOwner(); return true; }); assert(_slot == newSlot); attachRenderObject(newSlot); _dirty = false; @@ -1896,12 +1923,12 @@ abstract class RenderObjectElement extends BuildableElement { void update(RenderObjectWidget newWidget) { super.update(newWidget); assert(widget == newWidget); - assert(() { debugUpdateRenderObjectOwner(); return true; }); + assert(() { _debugUpdateRenderObjectOwner(); return true; }); widget.updateRenderObject(this, renderObject); _dirty = false; } - void debugUpdateRenderObjectOwner() { + void _debugUpdateRenderObjectOwner() { _renderObject.debugCreator = debugGetCreatorChain(10); } @@ -1915,6 +1942,7 @@ abstract class RenderObjectElement extends BuildableElement { /// Attempts to update the given old children list using the given new /// widgets, removing obsolete elements and introducing new ones as necessary, /// and then returns the new child list. + @protected List updateChildren(List oldChildren, List newWidgets, { Set detachedChildren }) { assert(oldChildren != null); assert(newWidgets != null); @@ -2115,8 +2143,13 @@ abstract class RenderObjectElement extends BuildableElement { _slot = null; } + @protected void insertChildRenderObject(RenderObject child, dynamic slot); + + @protected void moveChildRenderObject(RenderObject child, dynamic slot); + + @protected void removeChildRenderObject(RenderObject child); @override diff --git a/packages/flutter_markdown/lib/src/markdown_raw.dart b/packages/flutter_markdown/lib/src/markdown_raw.dart index 80b243d112..10bef55b7f 100644 --- a/packages/flutter_markdown/lib/src/markdown_raw.dart +++ b/packages/flutter_markdown/lib/src/markdown_raw.dart @@ -185,6 +185,7 @@ class _MarkdownBodyRawState extends State { @override void debugFillDescription(List description) { + super.debugFillDescription(description); description.add('cached blocks identity: ${_cachedBlocks.hashCode}'); } }