Detailed RenderBox and RenderObject docs (#6745)
This commit is contained in:
@@ -527,22 +527,412 @@ class _IntrinsicDimensionsCacheEntry {
|
||||
|
||||
/// A render object in a 2D cartesian coordinate system.
|
||||
///
|
||||
/// The size of each box is expressed as a width and a height. Each box has its
|
||||
/// own coordinate system in which its upper left corner is placed at (0, 0).
|
||||
/// The lower right corner of the box is therefore at (width, height). The box
|
||||
/// contains all the points including the upper left corner and extending to,
|
||||
/// but not including, the lower right corner.
|
||||
/// The [size] of each box is expressed as a width and a height. Each box has
|
||||
/// its own coordinate system in which its upper left corner is placed at (0,
|
||||
/// 0). The lower right corner of the box is therefore at (width, height). The
|
||||
/// box contains all the points including the upper left corner and extending
|
||||
/// to, but not including, the lower right corner.
|
||||
///
|
||||
/// Box layout is performed by passing a [BoxConstraints] object down the tree.
|
||||
/// The box constraints establish a min and max value for the child's width
|
||||
/// and height. In determining its size, the child must respect the constraints
|
||||
/// The box constraints establish a min and max value for the child's width and
|
||||
/// height. In determining its size, the child must respect the constraints
|
||||
/// given to it by its parent.
|
||||
///
|
||||
/// This protocol is sufficient for expressing a number of common box layout
|
||||
/// data flows. For example, to implement a width-in-height-out data flow, call
|
||||
/// data flows. For example, to implement a width-in-height-out data flow, call
|
||||
/// your child's [layout] function with a set of box constraints with a tight
|
||||
/// width value (and pass true for parentUsesSize). After the child determines
|
||||
/// its height, use the child's height to determine your size.
|
||||
///
|
||||
/// ## Writing a RenderBox subclass
|
||||
///
|
||||
/// One would implement a new [RenderBox] subclass to describe a new layout
|
||||
/// model, new paint model, new hit-testing model, or new semantics model, while
|
||||
/// remaining in the cartesian space defined by the [RenderBox] protocol.
|
||||
///
|
||||
/// To create a new protocol, consider subclassing [RenderObject] instead.
|
||||
///
|
||||
/// ### Constructors and properties of a new RenderBox subclass
|
||||
///
|
||||
/// The constructor will typically take a named argument for each property of
|
||||
/// the class. The value is then passed to a private field of the class and the
|
||||
/// constructor asserts its correctness (e.g. if it should not be null, it
|
||||
/// asserts it's not null).
|
||||
///
|
||||
/// Properties have the form of a getter/setter/field group like the following:
|
||||
///
|
||||
/// ```dart
|
||||
/// AxisDirection get axis => _axis;
|
||||
/// AxisDirection _axis;
|
||||
/// set axis(AxisDirection value) {
|
||||
/// assert(value != null); // same check as in the constructor
|
||||
/// if (value == _axis)
|
||||
/// return;
|
||||
/// _axis = value;
|
||||
/// markNeedsLayout();
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// The setter will typically finish with either a call to [markNeedsLayout], if
|
||||
/// the layout uses this property, or [markNeedsPaint], if only the painter
|
||||
/// function does. (No need to call both, [markNeedsLayout] implies
|
||||
/// [markNeedsPaint].)
|
||||
///
|
||||
/// Consider layout and paint to be expensive; be conservative about calling
|
||||
/// [markNeedsLayout] or [markNeedsPaint]. They should only be called if the
|
||||
/// layout (or paint, respectively) has actually changed.
|
||||
///
|
||||
/// ### Children
|
||||
///
|
||||
/// If a render object is a leaf, that is, it cannot have any children, then
|
||||
/// ignore this section. (Examples of leaf render objects are [RenderImage] and
|
||||
/// [RenderParagraph].)
|
||||
///
|
||||
/// For render objects with children, there are four possible scenarios:
|
||||
///
|
||||
/// * A single [RenderBox] child. In this scenario, consider inheriting from
|
||||
/// [RenderProxyBox] (if the render object sizes itself to match the child) or
|
||||
/// [RenderShiftedBox] (if the child will be smaller than the box and the box
|
||||
/// will align the child inside itself).
|
||||
///
|
||||
/// * A single child, but it isn't a [RenderBox]. Use the
|
||||
/// [RenderObjectWithChildMixin] mixin.
|
||||
///
|
||||
/// * A single list of children. Use the [ContainerRenderObjectMixin] mixin.
|
||||
///
|
||||
/// * A more complicated child model.
|
||||
///
|
||||
/// #### Using RenderProxyBox
|
||||
///
|
||||
/// By default, a [RenderProxyBox] render object sizes itself to fit its child, or
|
||||
/// to be as small as possible if there is no child; it passes all hit testing
|
||||
/// and painting on to the child, and intrinsic dimensions and baseline
|
||||
/// measurements similarly are proxied to the child.
|
||||
///
|
||||
/// A subclass of [RenderProxyBox] just needs to override the parts of the
|
||||
/// [RenderBox] protocol that matter. For example, [RenderOpacity] just
|
||||
/// overrides the paint method (and [alwaysNeedsCompositing] to reflect what the
|
||||
/// paint method does, and the [visitChildrenForSemantics] method so that the
|
||||
/// child is hidden from accessibility tools when it's invisible), and adds an
|
||||
/// [RenderOpacity.opacity] field.
|
||||
///
|
||||
/// [RenderProxyBox] assumes that the child is the size of the parent and
|
||||
/// positioned at 0,0. If this is not true, then use [RenderShiftedBox] instead.
|
||||
///
|
||||
/// See
|
||||
/// [proxy_box.dart](https://github.com/flutter/flutter/blob/master/packages/flutter/lib/src/rendering/proxy_box.dart)
|
||||
/// for examples of inheriting from [RenderProxyBox].
|
||||
///
|
||||
/// #### Using RenderShiftedBox
|
||||
///
|
||||
/// By default, a [RenderShiftedBox] acts much like a [RenderProxyBox] but
|
||||
/// without assuming that the child is positioned at 0,0 (the actual position
|
||||
/// recorded in the child's [parentData] field is used), and without providing a
|
||||
/// default layout algorithm.
|
||||
///
|
||||
/// See
|
||||
/// [shifted_box.dart](https://github.com/flutter/flutter/blob/master/packages/flutter/lib/src/rendering/shifted_box.dart)
|
||||
/// for examples of inheriting from [RenderShiftedBox].
|
||||
///
|
||||
/// #### Kinds of children and child-specific data
|
||||
///
|
||||
/// A [RenderBox] doesn't have to have [RenderBox] children. One can use another
|
||||
/// subclass of [RenderObject] for a [RenderBox]'s children. See the discussion
|
||||
/// at [RenderObject].
|
||||
///
|
||||
/// Children can have additional data owned by the parent but stored on the
|
||||
/// child using the [parentData] field. The class used for that data must
|
||||
/// inherit from [ParentData]. The [setupParentData] method is used to
|
||||
/// initialise the [parentData] field of a child when the child is attached.
|
||||
///
|
||||
/// By convention, [RenderBox] objects that have [RenderBox] children use the
|
||||
/// [BoxParentData] class, which has a [BoxParentData.offset] field to store the
|
||||
/// position of the child relative to the parent. ([RenderProxyBox] does not
|
||||
/// need this offset and therefore is an exception to this rule.)
|
||||
///
|
||||
/// #### Using RenderObjectWithChildMixin
|
||||
///
|
||||
/// If a render object has a single child but it isn't a [RenderBox], then the
|
||||
/// [RenderObjectWithChildMixin] class, which is a mixin that will handle the
|
||||
/// boilerplate of managing a child, will be useful.
|
||||
///
|
||||
/// It's a generic class with one type argument, the type of the child. For
|
||||
/// example, if you are building a `RenderFoo` class which takes a single
|
||||
/// `RenderBar` child, you would use the mixin as follows:
|
||||
///
|
||||
/// ```dart
|
||||
/// class RenderFoo extends RenderBox
|
||||
/// with RenderObjectWithChildMixin<RenderBar> {
|
||||
/// // ...
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// Since the `RenderFoo` class itself is still a [RenderBox] in this case, you
|
||||
/// still have to implement the [RenderBox] layout algorithm, as well as
|
||||
/// features like intrinsics and baselines, painting, and hit testing.
|
||||
///
|
||||
/// #### Using ContainerRenderObjectMixin
|
||||
///
|
||||
/// If a render box can have multiple children, then the
|
||||
/// [ContainerRenderObjectMixin] mixin can be used to handle the boilerplate. It
|
||||
/// uses a linked list to model the children in a manner that is easy to mutate
|
||||
/// dynamically and that can be walked efficiently. Random access is not
|
||||
/// efficient in this model; if you need random access to the children consider
|
||||
/// the next section on more complicated child models.
|
||||
///
|
||||
/// The [ContainerRenderObjectMixin] class has two type arguments. The first is
|
||||
/// the type of the child objects. The second is the type for their
|
||||
/// [parentData]. The class used for [parentData] must itself have the
|
||||
/// [ContainerParentDataMixin] class mixed into it; this is where
|
||||
/// [ContainerRenderObjectMixin] stores the linked list. A [ParentData] class
|
||||
/// can extend [ContainerBoxParentDataMixin]; this is essentially
|
||||
/// [BoxParentData] mixed with [ContainerParentDataMixin]. For example, if a
|
||||
/// `RenderFoo` class wanted to have a linked list of [RenderBox] children, one
|
||||
/// might create a `FooParentData` class as follows:
|
||||
///
|
||||
/// ```dart
|
||||
/// class FooParentData extends ContainerBoxParentDataMixin<RenderBox> {
|
||||
/// // (any fields you might need for these children)
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// When using [ContainerRenderObjectMixin] in a [RenderBox], consider mixing in
|
||||
/// [RenderBoxContainerDefaultsMixin], which provides a collection of utility
|
||||
/// methods that implement common parts of the [RenderBox] protocol (such as
|
||||
/// painting the children).
|
||||
///
|
||||
/// The declaration of the `RenderFoo` class itself would thus look like this:
|
||||
///
|
||||
/// ```dart
|
||||
/// class RenderFlex extends RenderBox with
|
||||
/// ContainerRenderObjectMixin<RenderBox, FooParentData>,
|
||||
/// RenderBoxContainerDefaultsMixin<RenderBox, FooParentData> {
|
||||
/// // ...
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// When walking the children (e.g. during layout), the following pattern is
|
||||
/// commonly used (in this case assuming that the children are all [RenderBox]
|
||||
/// objects and that this render object uses `FooParentData` objects for its
|
||||
/// children's [parentData] fields):
|
||||
///
|
||||
/// ```dart
|
||||
/// RenderBox child = firstChild;
|
||||
/// while (child != null) {
|
||||
/// final FooParentData childParentData = child.parentData;
|
||||
/// // ...operate on child and childParentData...
|
||||
/// assert(child.parentData == childParentData);
|
||||
/// child = childParentData.nextSibling;
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// #### More complicated child models
|
||||
///
|
||||
/// Render objects can have more complicated models, for example a map of
|
||||
/// children keyed on an enum, or a 2D grid of efficiently randomly-accessible
|
||||
/// children, or multiple lists of children, etc. If a render object has a model
|
||||
/// that can't be handled by the mixins above, it must implement the
|
||||
/// [RenderObject] child protocol, as follows:
|
||||
///
|
||||
/// * Any time a child is removed, call [dropChild] with the child.
|
||||
///
|
||||
/// * Any time a child is added, call [adoptChild] with the child.
|
||||
///
|
||||
/// * Implement the [attach] method such that it calls [attach] on each child.
|
||||
///
|
||||
/// * Implement the [detach] method such that it calls [detach] on each child.
|
||||
///
|
||||
/// * Implement the [redepthChildren] method such that it calls [redepthChild]
|
||||
/// on each child.
|
||||
///
|
||||
/// * Implement the [visitChildren] method such that it calls its argument for
|
||||
/// each child, typically in paint order (back-most to front-most).
|
||||
///
|
||||
/// * Implement [debugDescribeChildren] such that it outputs a string that
|
||||
/// describes all the children. In principle, for each child you want to
|
||||
/// include the results of that child's [toStringDeep] function.
|
||||
///
|
||||
/// Implementing these seven bullet points is essentially all that the two
|
||||
/// aforementioned mixins do.
|
||||
///
|
||||
/// ### Layout
|
||||
///
|
||||
/// [RenderBox] classes implement a layout algorithm. They have a set of
|
||||
/// constraints provided to them, and they size themselves based on those
|
||||
/// constraints and whatever other inputs they may have (for example, their
|
||||
/// children or properties).
|
||||
///
|
||||
/// When implementing a [RenderBox] subclass, one must make a choice. Does it
|
||||
/// size itself exclusively based on the constraints, or does it use any other
|
||||
/// information in sizing itself? An example of sizing purely based on the
|
||||
/// constraints would be growing to fit the parent.
|
||||
///
|
||||
/// Sizing purely based on the constraints allows the system to make some
|
||||
/// significant optimisations. Classes that use this approach should override
|
||||
/// [sizedByParent] to return true, and then override [performResize] to set the
|
||||
/// [size] using nothing but the constraints, e.g.:
|
||||
///
|
||||
/// ```dart
|
||||
/// @override
|
||||
/// bool get sizedByParent => true;
|
||||
///
|
||||
/// @override
|
||||
/// void performResize() {
|
||||
/// size = constraints.smallest;
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// Otherwise, the size is set in the [performLayout] function.
|
||||
///
|
||||
/// The [performLayout] function is where render boxes decide, if they are not
|
||||
/// [sizedByParent], what [size] they should be, and also where they decide
|
||||
/// where their children should be.
|
||||
///
|
||||
/// #### Layout of RenderBox children
|
||||
///
|
||||
/// The [performLayout] function should call the [layout] function of each (box)
|
||||
/// child, passing it a [BoxConstraints] object describing the constraints
|
||||
/// within which the child can render. Passing tight constraints (see
|
||||
/// [BoxConstraints.isTight]) to the child will allow the rendering library to
|
||||
/// apply some optimisations, as it knows that if the constraints are tight, the
|
||||
/// child's dimensions cannot change even if the layout of the child itself
|
||||
/// changes.
|
||||
///
|
||||
/// If the [performLayout] function will use the child's size to affect other
|
||||
/// aspects of the layout, for example if the render box sizes itself around the
|
||||
/// child, or positions several children based on the size of those children,
|
||||
/// then it must specify the `parentUsesSize` argument to the child's [layout]
|
||||
/// function, setting it to true.
|
||||
///
|
||||
/// This flag turns off some optimisations; algorithms that do not rely on the
|
||||
/// children's sizes will be more efficient. (In particular, relying on the
|
||||
/// child's [size] means that if the child is marked dirty for layout, the
|
||||
/// parent will probably also be marked dirty for layout, unless the
|
||||
/// [constraints] given by the parent to the child were tight constraints.)
|
||||
///
|
||||
/// For [RenderBox] classes that do not inherit from [RenderProxyBox], once they
|
||||
/// have laid out their children, should also position them, by setting the
|
||||
/// [BoxParentData.offset] field of each child's [parentData] object.
|
||||
///
|
||||
/// #### Layout of non-RenderBox children
|
||||
///
|
||||
/// The children of a [RenderBox] do not have to be [RenderBox]es themselves. If
|
||||
/// they use another protocol (as discussed at [RenderObject]), then instead of
|
||||
/// [BoxConstraints], the parent would pass in the appropriate [Constraints]
|
||||
/// subclass, and instead of reading the child's size, the parent would read
|
||||
/// whatever the output of [layout] is for that layout protocol. The
|
||||
/// `parentUsesSize` flag is still used to indicate whether the parent is going
|
||||
/// to read that output, and optimisations still kick in if the child has tight
|
||||
/// constraints (as defined by [Constraints.isTight]).
|
||||
///
|
||||
/// ### Painting
|
||||
///
|
||||
/// To describe how a render box paints, implement the [paint] method. It is
|
||||
/// given a [PaintingContext] object and an [Offset]. The painting context
|
||||
/// provides methods to affect the layer tree as well as a
|
||||
/// [PaintingContext.canvas] which can be used to add drawing commands. The
|
||||
/// canvas object should not be cached across calls to the [PaintingContext]'s
|
||||
/// methods; every time a method on [PaintingContext] is called, there is a
|
||||
/// chance that the canvas will change identity. The offset specifies the
|
||||
/// position of the top left corner of the box in the coordinate system of the
|
||||
/// [PaintingContext.canvas].
|
||||
///
|
||||
/// To draw text on a canvas, use a [TextPainter].
|
||||
///
|
||||
/// To draw an image to a canvas, use the [paintImage] method.
|
||||
///
|
||||
/// A [RenderBox] that uses methods on [PaintingContext] that introduce new
|
||||
/// layers should override the [alwaysNeedsCompositing] getter and set it to
|
||||
/// true. If the object sometimes does and sometimes does not, it can have that
|
||||
/// getter return true in some cases and false in others. In that case, whenever
|
||||
/// the return value would change, call [markNeedsCompositingBitsUpdate]. (This
|
||||
/// is done automatically when a child is added or removed, so you don't have to
|
||||
/// call it explicitly if the [alwaysNeedsCompositing] getter only changes value
|
||||
/// based on the presence or absence of children.)
|
||||
///
|
||||
/// Anytime anything changes on the object that would cause the [paint] method
|
||||
/// to paint something different (but would not cause the layout to change),
|
||||
/// the object should call [markNeedsPaint].
|
||||
///
|
||||
/// #### Painting children
|
||||
///
|
||||
/// The [paint] method's `context` argument has a [PaintingContext.paintChild]
|
||||
/// method, which should be called for each child that is to be painted. It
|
||||
/// should be given a reference to the child, and an [Offset] giving the
|
||||
/// position of the child relative to the parent.
|
||||
///
|
||||
/// If the [paint] method applies a transform to the painting context before
|
||||
/// painting children (or generally applies an additional offset beyond the
|
||||
/// offset it was itself given as an argument), then the [applyPaintTransform]
|
||||
/// method should also be overridden. That method must adjust the matrix that it
|
||||
/// is given in the same manner as it transformed the painting context and
|
||||
/// offset before painting the given child. This is used by the [globalToLocal]
|
||||
/// and [localToGlobal] methods.
|
||||
///
|
||||
/// #### Hit Tests
|
||||
///
|
||||
/// Hit testing for render boxes is implemented by the [hitTest] method. The
|
||||
/// default implementation of this method defers to [hitTestSelf] and
|
||||
/// [hitTestChildren]. When implementing hit testing, you can either override
|
||||
/// these latter two methods, or ignore them and just override [hitTest].
|
||||
///
|
||||
/// The [hitTest] method itself is given a [Point], and must return true if the
|
||||
/// object or one of its children has absorbed the hit (preventing objects below
|
||||
/// this one from being hit), or false if the hit can continue to other objects
|
||||
/// below this one.
|
||||
///
|
||||
/// For each child [RenderBox], the [hitTest] method on the child should be
|
||||
/// called with the same [HitTestResult] argument and with the point transformed
|
||||
/// into the child's coordinate space (in the same manner that the
|
||||
/// [applyPaintTransform] method would). The default implementation defers to
|
||||
/// [hitTestChildren] to call the children. [RenderBoxContainerDefaultsMixin]
|
||||
/// provides a [RenderBoxContainerDefaultsMixin.defaultHitTestChildren] method
|
||||
/// that does this assuming that the children are axis-aligned, not transformed,
|
||||
/// and positioned according to the [BoxParentData.offset] field of the
|
||||
/// [parentData]; more elaborate boxes can override [hitTestChildren]
|
||||
/// accordingly.
|
||||
///
|
||||
/// If the object is hit, then it should also add itself to the [HitTestResult]
|
||||
/// object that is given as an argument to the [hitTest] method, using
|
||||
/// [HitTestResult.add]. The default implementation defers to [hitTestSelf] to
|
||||
/// determine if the box is hit. If the object adds itself before the children
|
||||
/// can add themselves, then it will be as if the object was above the children.
|
||||
/// If it adds itself after the children, then it will be as if it was below the
|
||||
/// children. Entries added to the [HitTestResult] object should use the
|
||||
/// [BoxHitTestEntry] class. The entries are subsequently walked by the system
|
||||
/// in the order they were added, and for each entry, the target's [handleEvent]
|
||||
/// method is called, passing in the [HitTestEntry] object.
|
||||
///
|
||||
/// Hit testing cannot rely on painting having happened.
|
||||
///
|
||||
/// ### Semantics
|
||||
///
|
||||
/// For a render box to be accessible, implement the
|
||||
/// [describeApproximatePaintClip] and [visitChildrenForSemantics] methods, and
|
||||
/// the [semanticAnnotator] getter. The default implementations are sufficient
|
||||
/// for objects that only affect layout, but nodes that represent interactive
|
||||
/// components or information (diagrams, text, images, etc) should provide more
|
||||
/// complete implementations. For more information, see the documentation for
|
||||
/// these members.
|
||||
///
|
||||
/// ### Intrinsics and Baselines
|
||||
///
|
||||
/// The layout, painting, hit testing, and semantics protocols are common to all
|
||||
/// render objects. [RenderBox] objects must implement two additional protocols:
|
||||
/// intrinsic sizing and baseline measurements.
|
||||
///
|
||||
/// There are four methods to implement for intrinsic sizing, to compute the
|
||||
/// minimum and maximum intrinsic width and height of the box. The documentation
|
||||
/// for these methods discusses the protocol in detail:
|
||||
/// [computeMinIntrinsicWidth], [computeMaxIntrinsicWidth],
|
||||
/// [computeMinIntrinsicHeight], [computeMaxIntrinsicHeight].
|
||||
///
|
||||
/// In addition, if the box has any children, it must implement
|
||||
/// [computeDistanceToActualBaseline]. [RenderProxyBox] provides a simple
|
||||
/// implementation that forwards to the child; [RenderShiftedBox] provides an
|
||||
/// implementation that offsets the child's baseline information by the position
|
||||
/// of the child relative to the parent. If you do not inherited from either of
|
||||
/// these classes, however, you must implement the algorithm yourself.
|
||||
abstract class RenderBox extends RenderObject {
|
||||
@override
|
||||
void setupParentData(RenderObject child) {
|
||||
|
||||
@@ -436,6 +436,48 @@ class PaintingContext {
|
||||
///
|
||||
/// Concrete layout models (such as box) will create concrete subclasses to
|
||||
/// communicate layout constraints between parents and children.
|
||||
///
|
||||
/// ## Writing a Constraints subclass
|
||||
///
|
||||
/// When creating a new [RenderObject] subclass with a new layout protocol, one
|
||||
/// will usually need to create a new [Constraints] subclass to express the
|
||||
/// input to the layout algorithms.
|
||||
///
|
||||
/// A [Constraints] subclass should be immutable (all fields final). There are
|
||||
/// several members to implement, in addition to whatever fields, constructors,
|
||||
/// and helper methods one may find useful for a particular layout protocol:
|
||||
///
|
||||
/// * The [isTight] getter, which should return true if the object represents a
|
||||
/// case where the [RenderObject] class has no choice for how to lay itself
|
||||
/// out. For example, [BoxConstraints] returns true for [isTight] when both
|
||||
/// the minimum and maximum widths and the minimum and maximum heights are
|
||||
/// equal.
|
||||
///
|
||||
/// * The [isNormalized] getter, which should return true if the object
|
||||
/// represents its data in its canonical form. Sometimes, it is possible for
|
||||
/// fields to be redundant with each other, such that several different
|
||||
/// representations have the same implications. For example, a
|
||||
/// [BoxConstraints] instance with its minimum width greater than its maximum
|
||||
/// width is equivalent to one where the maximum width is set to that minimum
|
||||
/// width (`2<w<1` is equivalent to `2<w<2`, since minimum constraints have
|
||||
/// priority). This getter is used by the default implementation of
|
||||
/// [debugAssertIsValid].
|
||||
///
|
||||
/// * The [debugAssertIsValid] method, which should assert if there's anything
|
||||
/// wrong with the constraints object. (We use this approach rather than
|
||||
/// asserting in constructors so that our constructors can be `const` and so
|
||||
/// that it is possible to create invalid constraints temporarily while
|
||||
/// building valid ones.) See the implementation of
|
||||
/// [BoxConstraints.debugAssertIsValid] for an example of the detailed checks
|
||||
/// that can be made.
|
||||
///
|
||||
/// * The [operator ==] and [hashCode] members, so that constraints can be
|
||||
/// compared for equality. If a render object is given constraints that are
|
||||
/// equal, then the rendering library will avoid laying the object out again
|
||||
/// if it is not dirty.
|
||||
///
|
||||
/// * The [toString] method, which should describe the constraints so that they
|
||||
/// appear in a usefully readable form in the output of [debugDumpRenderTree].
|
||||
abstract class Constraints {
|
||||
/// Abstract const constructor. This constructor enables subclasses to provide
|
||||
/// const constructors so that they can be used in const expressions.
|
||||
@@ -1058,24 +1100,111 @@ void _doNothing() { }
|
||||
/// The [RenderObject] class hierarchy is the core of the rendering
|
||||
/// library's reason for being.
|
||||
///
|
||||
/// [RenderObject]s have a [parent], and have a slot called
|
||||
/// [parentData] in which the parent [RenderObject] can store
|
||||
/// child-specific data, for example, the child position. The
|
||||
/// [RenderObject] class also implements the basic layout and paint
|
||||
/// protocols.
|
||||
/// [RenderObject]s have a [parent], and have a slot called [parentData] in
|
||||
/// which the parent [RenderObject] can store child-specific data, for example,
|
||||
/// the child position. The [RenderObject] class also implements the basic
|
||||
/// layout and paint protocols.
|
||||
///
|
||||
/// The [RenderObject] class, however, does not define a child model
|
||||
/// (e.g. whether a node has zero, one, or more children). It also
|
||||
/// doesn't define a coordinate system (e.g. whether children are
|
||||
/// positioned in cartesian coordinates, in polar coordinates, etc) or
|
||||
/// a specific layout protocol (e.g. whether the layout is
|
||||
/// width-in-height-out, or constraint-in-size-out, or whether the
|
||||
/// parent sets the size and position of the child before or after the
|
||||
/// child lays out, etc; or indeed whether the children are allowed to
|
||||
/// read their parent's [parentData] slot).
|
||||
/// The [RenderObject] class, however, does not define a child model (e.g.
|
||||
/// whether a node has zero, one, or more children). It also doesn't define a
|
||||
/// coordinate system (e.g. whether children are positioned in cartesian
|
||||
/// coordinates, in polar coordinates, etc) or a specific layout protocol (e.g.
|
||||
/// whether the layout is width-in-height-out, or constraint-in-size-out, or
|
||||
/// whether the parent sets the size and position of the child before or after
|
||||
/// the child lays out, etc; or indeed whether the children are allowed to read
|
||||
/// their parent's [parentData] slot).
|
||||
///
|
||||
/// The [RenderBox] subclass introduces the opinion that the layout
|
||||
/// system uses cartesian coordinates.
|
||||
///
|
||||
/// ## Writing a RenderObject subclass
|
||||
///
|
||||
/// In most cases, subclassing [RenderObject] itself is overkill, and
|
||||
/// [RenderBox] would be a better starting point. However, if a render object
|
||||
/// doesn't want to use a cartesian coordinate system, then it should indeed
|
||||
/// inherit from [RenderObject] directly. This allows it to define its own
|
||||
/// layout protocol by using a new subclass of [Constraints] rather than using
|
||||
/// [BoxConstraints], and by potentially using an entirely new set of objects
|
||||
/// and values to represent the result of the output rather than just a [Size].
|
||||
/// This increased flexibility comes at the cost of not being able to rely on
|
||||
/// the features of [RenderBox]. For example, [RenderBox] implements an
|
||||
/// intrinsic sizing protocol that allows you to measure a child without fully
|
||||
/// laying it out, in such a way that if that child changes size, the parent
|
||||
/// will be laid out again (to take into account the new dimensions of the
|
||||
/// child). This is a subtle and bug-prone feature to get right.
|
||||
///
|
||||
/// Most aspects of writing a [RenderBox] apply to writing a [RenderObject] as
|
||||
/// well, and therefore the discussion at [RenderBox] is recommended background
|
||||
/// reading. The main differences are around layout and hit testing, since those
|
||||
/// are the aspects that [RenderBox] primarily specializes.
|
||||
///
|
||||
/// ### Layout
|
||||
///
|
||||
/// A layout protocol begins with a subclass of [Constraints]. See the
|
||||
/// discussion at [Constraints] for more information on how to write a
|
||||
/// [Constraints] subclass.
|
||||
///
|
||||
/// The [performLayout] method should take the [constraints], and apply them.
|
||||
/// The output of the layout algorithm is fields set on the object that describe
|
||||
/// the geometry of the object for the purposes of the parent's layout. For
|
||||
/// example, with [RenderBox] the output is the [RenderBox.size] field. This
|
||||
/// output should only be read by the parent if the parent specified
|
||||
/// `parentUsesSize` as true when calling [layout] on the child.
|
||||
///
|
||||
/// Anytime anything changes on a render object that would affect the layout of
|
||||
/// that object, it should call [markNeedsLayout].
|
||||
///
|
||||
/// ### Hit Testing
|
||||
///
|
||||
/// Hit testing is even more open-ended than layout. There is no method to
|
||||
/// override, you are expected to provide one.
|
||||
///
|
||||
/// The general behaviour of your hit-testing method should be similar to the
|
||||
/// behavior described for [RenderBox]. The main difference is that the input
|
||||
/// need not be a [Point]. You are also allowed to use a different subclass of
|
||||
/// [HitTestEntry] when adding entries to the [HitTestResult]. When the
|
||||
/// [handleEvent] method is called, the same object that was added to the
|
||||
/// [HitTestResult] will be passed in, so it can be used to track information
|
||||
/// like the precise coordinate of the hit, in whatever coordinate system is
|
||||
/// used by the new layout protocol.
|
||||
///
|
||||
/// ### Adapting from one protocol to another
|
||||
///
|
||||
/// In general, the root of a Flutter render object tree is a [RenderView]. This
|
||||
/// object has a single child, which must be a [RenderBox]. Thus, if you want to
|
||||
/// have a custom [RenderObject] subclass in the render tree, you have two
|
||||
/// choices: you either need to replace the [RenderView] itself, or you need to
|
||||
/// have a [RenderBox] that has your class as its child. (The latter is the much
|
||||
/// more common case.)
|
||||
///
|
||||
/// This [RenderBox] subclass converts from the box protocol to the protocol of
|
||||
/// your class.
|
||||
///
|
||||
/// In particular, this means that for hit testing it overrides
|
||||
/// [RenderBox.hitTest], and calls whatever method you have in your class for
|
||||
/// hit testing.
|
||||
///
|
||||
/// Similarly, it overrides [performLayout] to create a [Constraints] object
|
||||
/// appropriate for your class and passes that to the child's [layout] method.
|
||||
///
|
||||
/// ### Layout interactions between render objects
|
||||
///
|
||||
/// In general, the layout of a render box should only depend on the output of
|
||||
/// its child's layout, and then only if `parentUsesSize` is set to true in the
|
||||
/// [layout] call. Furthermore, if it is set to true, the parent must call the
|
||||
/// child's [layout] if the child is to be rendered, because otherwise the
|
||||
/// parent will not be notified when the child changes its layout outputs.
|
||||
///
|
||||
/// It is possible to set up render object protocols that transfer additional
|
||||
/// information. For example, in the [RenderBox] protocol you can query your
|
||||
/// children's intrinsic dimensions and baseline geometry. However, if this is
|
||||
/// done then it is imperative that the child call [markNeedsLayout] on the
|
||||
/// parent any time that additional information changes, if the parent used it
|
||||
/// in the last layout phase. For an example of how to implement this, see the
|
||||
/// [RenderBox.markNeedsLayout] method. It overrides
|
||||
/// [RenderObject.markNeedsLayout] so that if a parent has queried the intrinsic
|
||||
/// or baseline information, it gets marked dirty whenever the child's geometry
|
||||
/// changes.
|
||||
abstract class RenderObject extends AbstractNode implements HitTestTarget {
|
||||
/// Initializes internal fields for subclasses.
|
||||
RenderObject() {
|
||||
@@ -1494,7 +1623,7 @@ abstract class RenderObject extends AbstractNode implements HitTestTarget {
|
||||
|
||||
/// Compute the layout for this render object.
|
||||
///
|
||||
/// This function is the main entry point for parents to ask their children to
|
||||
/// This method is the main entry point for parents to ask their children to
|
||||
/// update their layout information. The parent passes a constraints object,
|
||||
/// which informs the child as which layouts are permissible. The child is
|
||||
/// required to obey the given constraints.
|
||||
@@ -1507,11 +1636,12 @@ abstract class RenderObject extends AbstractNode implements HitTestTarget {
|
||||
/// parentUsesSize, the child can change its layout information (subject to
|
||||
/// the given constraints) without informing the parent.
|
||||
///
|
||||
/// Subclasses should not override layout directly. Instead, they should
|
||||
/// override performResize and/or performLayout.
|
||||
/// Subclasses should not override [layout] directly. Instead, they should
|
||||
/// override [performResize] and/or [performLayout]. The [layout] method
|
||||
/// delegates the actual work to [performResize] and [performLayout].
|
||||
///
|
||||
/// The parent's performLayout method should call the layout of all its
|
||||
/// children unconditionally. It is the layout function's responsibility (as
|
||||
/// The parent's performLayout method should call the [layout] of all its
|
||||
/// children unconditionally. It is the [layout] method's responsibility (as
|
||||
/// implemented here) to return early if the child does not need to do any
|
||||
/// work to update its layout information.
|
||||
void layout(Constraints constraints, { bool parentUsesSize: false }) {
|
||||
@@ -1617,12 +1747,12 @@ abstract class RenderObject extends AbstractNode implements HitTestTarget {
|
||||
assert(parent == this.parent);
|
||||
}
|
||||
|
||||
/// If a subclass has a "size" (the state controlled by "parentUsesSize",
|
||||
/// whatever it is in the subclass, e.g. the actual "size" property of
|
||||
/// RenderBox), and the subclass verifies that in checked mode this "size"
|
||||
/// property isn't used when debugCanParentUseSize isn't set, then that
|
||||
/// subclass should override debugResetSize() to reapply the current values of
|
||||
/// debugCanParentUseSize to that state.
|
||||
/// If a subclass has a "size" (the state controlled by `parentUsesSize`,
|
||||
/// whatever it is in the subclass, e.g. the actual `size` property of
|
||||
/// [RenderBox]), and the subclass verifies that in checked mode this "size"
|
||||
/// property isn't used when [debugCanParentUseSize] isn't set, then that
|
||||
/// subclass should override [debugResetSize] to reapply the current values of
|
||||
/// [debugCanParentUseSize] to that state.
|
||||
@protected
|
||||
void debugResetSize() { }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user