stack.dart 24.6 KB
Newer Older
1 2 3 4 5
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:math' as math;
6
import 'dart:ui' show lerpDouble, hashValues;
7

8
import 'package:flutter/foundation.dart';
9

10 11
import 'box.dart';
import 'object.dart';
12

Hixie's avatar
Hixie committed
13 14 15 16 17 18
/// An immutable 2D, axis-aligned, floating-point rectangle whose coordinates
/// are given relative to another rectangle's edges, known as the container.
/// Since the dimensions of the rectangle are relative to those of the
/// container, this class has no width and height members. To determine the
/// width or height of the rectangle, convert it to a [Rect] using [toRect()]
/// (passing the container's own Rect), and then examine that object.
Hixie's avatar
Hixie committed
19
///
20
/// The fields [left], [right], [bottom], and [top] must not be null.
21
@immutable
Hixie's avatar
Hixie committed
22 23
class RelativeRect {
  /// Creates a RelativeRect with the given values.
24 25 26 27
  ///
  /// The arguments must not be null.
  const RelativeRect.fromLTRB(this.left, this.top, this.right, this.bottom)
    : assert(left != null && top != null && right != null && bottom != null);
Hixie's avatar
Hixie committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

  /// Creates a RelativeRect from a Rect and a Size. The Rect (first argument)
  /// and the RelativeRect (the output) are in the coordinate space of the
  /// rectangle described by the Size, with 0,0 being at the top left.
  factory RelativeRect.fromSize(Rect rect, Size container) {
    return new RelativeRect.fromLTRB(rect.left, rect.top, container.width - rect.right, container.height - rect.bottom);
  }

  /// Creates a RelativeRect from two Rects. The second Rect provides the
  /// container, the first provides the rectangle, in the same coordinate space,
  /// that is to be converted to a RelativeRect. The output will be in the
  /// container's coordinate space.
  ///
  /// For example, if the top left of the rect is at 0,0, and the top left of
  /// the container is at 100,100, then the top left of the output will be at
  /// -100,-100.
  ///
  /// If the first rect is actually in the container's coordinate space, then
  /// use [RelativeRect.fromSize] and pass the container's size as the second
  /// argument instead.
  factory RelativeRect.fromRect(Rect rect, Rect container) {
    return new RelativeRect.fromLTRB(
      rect.left - container.left,
      rect.top - container.top,
52 53
      container.right - rect.right,
      container.bottom - rect.bottom
Hixie's avatar
Hixie committed
54 55 56
    );
  }

57
  /// A rect that covers the entire container.
58
  static const RelativeRect fill = const RelativeRect.fromLTRB(0.0, 0.0, 0.0, 0.0);
Hixie's avatar
Hixie committed
59 60

  /// Distance from the left side of the container to the left side of this rectangle.
61 62
  ///
  /// May be negative if the left side of the rectangle is outside of the container.
Hixie's avatar
Hixie committed
63 64 65
  final double left;

  /// Distance from the top side of the container to the top side of this rectangle.
66 67
  ///
  /// May be negative if the top side of the rectangle is outside of the container.
Hixie's avatar
Hixie committed
68 69 70
  final double top;

  /// Distance from the right side of the container to the right side of this rectangle.
71 72
  ///
  /// May be negative if the right side of the rectangle is outside of the container.
Hixie's avatar
Hixie committed
73 74 75
  final double right;

  /// Distance from the bottom side of the container to the bottom side of this rectangle.
76 77
  ///
  /// May be negative if the bottom side of the rectangle is outside of the container.
Hixie's avatar
Hixie committed
78 79
  final double bottom;

80 81 82 83 84 85
  /// Returns whether any of the values are greater than zero.
  ///
  /// This corresponds to one of the sides ([left], [top], [right], or [bottom]) having
  /// some positive inset towards the center.
  bool get hasInsets => left > 0.0 || top > 0.0 || right > 0.0 || bottom > 0.0;

Hixie's avatar
Hixie committed
86 87
  /// Returns a new rectangle object translated by the given offset.
  RelativeRect shift(Offset offset) {
88
    return new RelativeRect.fromLTRB(left + offset.dx, top + offset.dy, right - offset.dx, bottom - offset.dy);
Hixie's avatar
Hixie committed
89 90 91 92
  }

  /// Returns a new rectangle with edges moved outwards by the given delta.
  RelativeRect inflate(double delta) {
93
    return new RelativeRect.fromLTRB(left - delta, top - delta, right - delta, bottom - delta);
Hixie's avatar
Hixie committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
  }

  /// Returns a new rectangle with edges moved inwards by the given delta.
  RelativeRect deflate(double delta) {
    return inflate(-delta);
  }

  /// Returns a new rectangle that is the intersection of the given rectangle and this rectangle.
  RelativeRect intersect(RelativeRect other) {
    return new RelativeRect.fromLTRB(
      math.max(left, other.left),
      math.max(top, other.top),
      math.max(right, other.right),
      math.max(bottom, other.bottom)
    );
  }

111
  /// Convert this [RelativeRect] to a [Rect], in the coordinate space of the container.
Ian Hickson's avatar
Ian Hickson committed
112 113 114 115 116
  ///
  /// See also:
  ///
  ///  * [toSize], which returns the size part of the rect, based on the size of
  ///    the container.
Hixie's avatar
Hixie committed
117 118 119 120
  Rect toRect(Rect container) {
    return new Rect.fromLTRB(left, top, container.width - right, container.height - bottom);
  }

Ian Hickson's avatar
Ian Hickson committed
121 122 123 124 125 126 127 128 129
  /// Convert this [RelativeRect] to a [Size], assuming a container with the given size.
  ///
  /// See also:
  ///
  ///  * [toRect], which also computes the position relative to the container.
  Size toSize(Size container) {
    return new Size(container.width - left - right, container.height - top - bottom);
  }

Hixie's avatar
Hixie committed
130 131 132
  /// Linearly interpolate between two RelativeRects.
  ///
  /// If either rect is null, this function interpolates from [RelativeRect.fill].
133 134 135 136 137 138 139 140 141 142 143 144
  ///
  /// The `t` argument represents position on the timeline, with 0.0 meaning
  /// that the interpolation has not started, returning `a` (or something
  /// equivalent to `a`), 1.0 meaning that the interpolation has finished,
  /// returning `b` (or something equivalent to `b`), and values in between
  /// meaning that the interpolation is at the relevant point on the timeline
  /// between `a` and `b`. The interpolation can be extrapolated beyond 0.0 and
  /// 1.0, so negative values and values greater than 1.0 are valid (and can
  /// easily be generated by curves such as [Curves.elasticInOut]).
  ///
  /// Values for `t` are usually obtained from an [Animation<double>], such as
  /// an [AnimationController].
Hixie's avatar
Hixie committed
145
  static RelativeRect lerp(RelativeRect a, RelativeRect b, double t) {
146
    assert(t != null);
Hixie's avatar
Hixie committed
147 148 149 150 151
    if (a == null && b == null)
      return null;
    if (a == null)
      return new RelativeRect.fromLTRB(b.left * t, b.top * t, b.right * t, b.bottom * t);
    if (b == null) {
152
      final double k = 1.0 - t;
Hixie's avatar
Hixie committed
153 154 155 156 157 158
      return new RelativeRect.fromLTRB(b.left * k, b.top * k, b.right * k, b.bottom * k);
    }
    return new RelativeRect.fromLTRB(
      lerpDouble(a.left, b.left, t),
      lerpDouble(a.top, b.top, t),
      lerpDouble(a.right, b.right, t),
159
      lerpDouble(a.bottom, b.bottom, t),
Hixie's avatar
Hixie committed
160 161 162
    );
  }

163
  @override
Hixie's avatar
Hixie committed
164 165 166 167 168 169 170 171 172 173 174 175
  bool operator ==(dynamic other) {
    if (identical(this, other))
      return true;
    if (other is! RelativeRect)
      return false;
    final RelativeRect typedOther = other;
    return left == typedOther.left &&
           top == typedOther.top &&
           right == typedOther.right &&
           bottom == typedOther.bottom;
  }

176
  @override
177
  int get hashCode => hashValues(left, top, right, bottom);
Hixie's avatar
Hixie committed
178

179
  @override
180
  String toString() => 'RelativeRect.fromLTRB(${left?.toStringAsFixed(1)}, ${top?.toStringAsFixed(1)}, ${right?.toStringAsFixed(1)}, ${bottom?.toStringAsFixed(1)})';
Hixie's avatar
Hixie committed
181 182
}

Adam Barth's avatar
Adam Barth committed
183
/// Parent data for use with [RenderStack].
184
class StackParentData extends ContainerBoxParentData<RenderBox> {
Hixie's avatar
Hixie committed
185
  /// The distance by which the child's top edge is inset from the top of the stack.
186
  double top;
187

Hixie's avatar
Hixie committed
188
  /// The distance by which the child's right edge is inset from the right of the stack.
189
  double right;
190

Hixie's avatar
Hixie committed
191
  /// The distance by which the child's bottom edge is inset from the bottom of the stack.
192
  double bottom;
193

Hixie's avatar
Hixie committed
194
  /// The distance by which the child's left edge is inset from the left of the stack.
195 196
  double left;

197 198 199 200 201 202 203 204 205 206
  /// The child's width.
  ///
  /// Ignored if both left and right are non-null.
  double width;

  /// The child's height.
  ///
  /// Ignored if both top and bottom are non-null.
  double height;

Hixie's avatar
Hixie committed
207 208
  /// Get or set the current values in terms of a RelativeRect object.
  RelativeRect get rect => new RelativeRect.fromLTRB(left, top, right, bottom);
209
  set rect(RelativeRect value) {
Hixie's avatar
Hixie committed
210 211 212
    top = value.top;
    right = value.right;
    bottom = value.bottom;
Hixie's avatar
Hixie committed
213
    left = value.left;
Hixie's avatar
Hixie committed
214 215
  }

Hixie's avatar
Hixie committed
216
  /// Whether this child is considered positioned.
217
  ///
Hixie's avatar
Hixie committed
218
  /// A child is positioned if any of the top, right, bottom, or left properties
219 220 221
  /// are non-null. Positioned children do not factor into determining the size
  /// of the stack but are instead placed relative to the non-positioned
  /// children in the stack.
222
  bool get isPositioned => top != null || right != null || bottom != null || left != null || width != null || height != null;
223

224
  @override
Hixie's avatar
Hixie committed
225
  String toString() {
226
    final List<String> values = <String>[];
Hixie's avatar
Hixie committed
227 228 229 230 231 232 233 234 235 236 237 238
    if (top != null)
      values.add('top=$top');
    if (right != null)
      values.add('right=$right');
    if (bottom != null)
      values.add('bottom=$bottom');
    if (left != null)
      values.add('left=$left');
    if (width != null)
      values.add('width=$width');
    if (height != null)
      values.add('height=$height');
239 240 241
    if (values.isEmpty)
      values.add('not positioned');
    values.add(super.toString());
Hixie's avatar
Hixie committed
242 243
    return values.join('; ');
  }
244 245
}

246 247
/// How to size the non-positioned children of a [Stack].
///
248
/// This enum is used with [Stack.fit] and [RenderStack.fit] to control
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
/// how the [BoxConstraints] passed from the stack's parent to the stack's child
/// are adjusted.
///
/// See also:
///
///  * [Stack], the widget that uses this.
///  * [RenderStack], the render object that implements the stack algorithm.
enum StackFit {
  /// The constraints passed to the stack from its parent are loosened.
  ///
  /// For example, if the stack has constraints that force it to 350x600, then
  /// this would allow the non-positioned children of the stack to have any
  /// width from zero to 350 and any height from zero to 600.
  ///
  /// See also:
  ///
  ///  * [Center], which loosens the constraints passed to its child and then
  ///    centers the child in itself.
  ///  * [BoxConstraints.loosen], which implements the loosening of box
  ///    constraints.
  loose,

  /// The constraints passed to the stack from its parent are tightened to the
  /// biggest size allowed.
  ///
  /// For example, if the stack has loose constraints with a width in the range
  /// 10 to 100 and a height in the range 0 to 600, then the non-positioned
  /// children of the stack would all be sized as 100 pixels wide and 600 high.
  expand,

  /// The constraints passed to the stack from its parent are passed unmodified
  /// to the non-positioned children.
  ///
  /// For example, if a [Stack] is an [Expanded] child of a [Row], the
  /// horizontal constraints will be tight and the vertical constraints will be
  /// loose.
  passthrough,
}

288
/// Whether overflowing children should be clipped, or their overflow be
289 290
/// visible.
enum Overflow {
291
  /// Overflowing children will be visible.
292
  visible,
293

294 295
  /// Overflowing children will be clipped to the bounds of their parent.
  clip,
296 297
}

298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
/// Implements the stack layout algorithm
///
/// In a stack layout, the children are positioned on top of each other in the
/// order in which they appear in the child list. First, the non-positioned
/// children (those with null values for top, right, bottom, and left) are
/// laid out and initially placed in the upper-left corner of the stack. The
/// stack is then sized to enclose all of the non-positioned children. If there
/// are no non-positioned children, the stack becomes as large as possible.
///
/// The final location of non-positioned children is determined by the alignment
/// parameter. The left of each non-positioned child becomes the
/// difference between the child's width and the stack's width scaled by
/// alignment.x. The top of each non-positioned child is computed
/// similarly and scaled by alignment.y. So if the alignment x and y properties
/// are 0.0 (the default) then the non-positioned children remain in the
/// upper-left corner. If the alignment x and y properties are 0.5 then the
/// non-positioned children are centered within the stack.
///
/// Next, the positioned children are laid out. If a child has top and bottom
/// values that are both non-null, the child is given a fixed height determined
/// by subtracting the sum of the top and bottom values from the height of the stack.
/// Similarly, if the child has right and left values that are both non-null,
/// the child is given a fixed width derived from the stack's width.
/// Otherwise, the child is given unbounded constraints in the non-fixed dimensions.
///
/// Once the child is laid out, the stack positions the child
/// according to the top, right, bottom, and left properties of their
/// [StackParentData]. For example, if the bottom value is 10.0, the
/// bottom edge of the child will be inset 10.0 pixels from the bottom
/// edge of the stack. If the child extends beyond the bounds of the
/// stack, the stack will clip the child's painting to the bounds of
/// the stack.
///
/// See also:
///
///  * [RenderFlow]
class RenderStack extends RenderBox
Hans Muller's avatar
Hans Muller committed
335 336
    with ContainerRenderObjectMixin<RenderBox, StackParentData>,
         RenderBoxContainerDefaultsMixin<RenderBox, StackParentData> {
337 338 339 340 341
  /// Creates a stack render object.
  ///
  /// By default, the non-positioned children of the stack are aligned by their
  /// top left corners.
  RenderStack({
Hans Muller's avatar
Hans Muller committed
342
    List<RenderBox> children,
343
    AlignmentGeometry alignment = AlignmentDirectional.topStart,
344
    TextDirection textDirection,
345 346
    StackFit fit = StackFit.loose,
    Overflow overflow = Overflow.clip,
347 348 349 350
  }) : assert(alignment != null),
       assert(fit != null),
       assert(overflow != null),
       _alignment = alignment,
351
       _textDirection = textDirection,
352
       _fit = fit,
353
       _overflow = overflow {
354 355 356
    addAll(children);
  }

357 358
  bool _hasVisualOverflow = false;

359
  @override
360 361 362 363 364
  void setupParentData(RenderBox child) {
    if (child.parentData is! StackParentData)
      child.parentData = new StackParentData();
  }

365
  Alignment _resolvedAlignment;
366

Ian Hickson's avatar
Ian Hickson committed
367 368 369 370 371 372 373 374 375
  void _resolve() {
    if (_resolvedAlignment != null)
      return;
    _resolvedAlignment = alignment.resolve(textDirection);
  }

  void _markNeedResolution() {
    _resolvedAlignment = null;
    markNeedsLayout();
376 377
  }

378 379
  /// How to align the non-positioned or partially-positioned children in the
  /// stack.
380 381 382
  ///
  /// The non-positioned children are placed relative to each other such that
  /// the points determined by [alignment] are co-located. For example, if the
383
  /// [alignment] is [Alignment.topLeft], then the top left corner of
384
  /// each non-positioned child will be located at the same global coordinate.
Ian Hickson's avatar
Ian Hickson committed
385
  ///
386 387 388 389 390 391
  /// Partially-positioned children, those that do not specify an alignment in a
  /// particular axis (e.g. that have neither `top` nor `bottom` set), use the
  /// alignment to determine how they should be positioned in that
  /// under-specified axis.
  ///
  /// If this is set to an [AlignmentDirectional] object, then [textDirection]
392 393 394 395
  /// must not be null.
  AlignmentGeometry get alignment => _alignment;
  AlignmentGeometry _alignment;
  set alignment(AlignmentGeometry value) {
396
    assert(value != null);
Ian Hickson's avatar
Ian Hickson committed
397 398 399 400
    if (_alignment == value)
      return;
    _alignment = value;
    _markNeedResolution();
401 402 403
  }

  /// The text direction with which to resolve [alignment].
Ian Hickson's avatar
Ian Hickson committed
404 405 406
  ///
  /// This may be changed to null, but only after the [alignment] has been changed
  /// to a value that does not depend on the direction.
407 408 409
  TextDirection get textDirection => _textDirection;
  TextDirection _textDirection;
  set textDirection(TextDirection value) {
Ian Hickson's avatar
Ian Hickson committed
410 411 412 413
    if (_textDirection == value)
      return;
    _textDirection = value;
    _markNeedResolution();
Hans Muller's avatar
Hans Muller committed
414 415
  }

416 417 418 419 420
  /// How to size the non-positioned children in the stack.
  ///
  /// The constraints passed into the [RenderStack] from its parent are either
  /// loosened ([StackFit.loose]) or tightened to their biggest size
  /// ([StackFit.expand]).
421 422 423
  StackFit get fit => _fit;
  StackFit _fit;
  set fit(StackFit value) {
424
    assert(value != null);
425 426
    if (_fit != value) {
      _fit = value;
427 428 429 430 431 432 433
      markNeedsLayout();
    }
  }

  /// Whether overflowing children should be clipped. See [Overflow].
  ///
  /// Some children in a stack might overflow its box. When this flag is set to
434
  /// [Overflow.clip], children cannot paint outside of the stack's box.
435 436 437 438 439 440 441 442 443 444
  Overflow get overflow => _overflow;
  Overflow _overflow;
  set overflow(Overflow value) {
    assert(value != null);
    if (_overflow != value) {
      _overflow = value;
      markNeedsPaint();
    }
  }

445 446
  double _getIntrinsicDimension(double mainChildSizeGetter(RenderBox child)) {
    double extent = 0.0;
447 448
    RenderBox child = firstChild;
    while (child != null) {
Hixie's avatar
Hixie committed
449 450
      final StackParentData childParentData = child.parentData;
      if (!childParentData.isPositioned)
451
        extent = math.max(extent, mainChildSizeGetter(child));
Hixie's avatar
Hixie committed
452 453
      assert(child.parentData == childParentData);
      child = childParentData.nextSibling;
454
    }
455
    return extent;
456 457
  }

458
  @override
459
  double computeMinIntrinsicWidth(double height) {
460
    return _getIntrinsicDimension((RenderBox child) => child.getMinIntrinsicWidth(height));
461 462
  }

463
  @override
464
  double computeMaxIntrinsicWidth(double height) {
465
    return _getIntrinsicDimension((RenderBox child) => child.getMaxIntrinsicWidth(height));
466 467
  }

468
  @override
469
  double computeMinIntrinsicHeight(double width) {
470 471 472 473
    return _getIntrinsicDimension((RenderBox child) => child.getMinIntrinsicHeight(width));
  }

  @override
474
  double computeMaxIntrinsicHeight(double width) {
475
    return _getIntrinsicDimension((RenderBox child) => child.getMaxIntrinsicHeight(width));
476 477
  }

478
  @override
479 480 481 482
  double computeDistanceToActualBaseline(TextBaseline baseline) {
    return defaultComputeDistanceToHighestActualBaseline(baseline);
  }

483
  @override
484
  void performLayout() {
Ian Hickson's avatar
Ian Hickson committed
485 486
    _resolve();
    assert(_resolvedAlignment != null);
487
    _hasVisualOverflow = false;
488
    bool hasNonPositionedChildren = false;
489 490 491 492 493
    if (childCount == 0) {
      size = constraints.biggest;
      assert(size.isFinite);
      return;
    }
494

495 496 497 498
    double width = constraints.minWidth;
    double height = constraints.minHeight;

    BoxConstraints nonPositionedConstraints;
499 500
    assert(fit != null);
    switch (fit) {
501 502 503 504 505 506 507 508 509 510 511
      case StackFit.loose:
        nonPositionedConstraints = constraints.loosen();
        break;
      case StackFit.expand:
        nonPositionedConstraints = new BoxConstraints.tight(constraints.biggest);
        break;
      case StackFit.passthrough:
        nonPositionedConstraints = constraints;
        break;
    }
    assert(nonPositionedConstraints != null);
512 513 514

    RenderBox child = firstChild;
    while (child != null) {
Hixie's avatar
Hixie committed
515
      final StackParentData childParentData = child.parentData;
516

Hixie's avatar
Hixie committed
517
      if (!childParentData.isPositioned) {
518 519
        hasNonPositionedChildren = true;

520
        child.layout(nonPositionedConstraints, parentUsesSize: true);
521 522 523 524 525 526

        final Size childSize = child.size;
        width = math.max(width, childSize.width);
        height = math.max(height, childSize.height);
      }

Hixie's avatar
Hixie committed
527
      child = childParentData.nextSibling;
528 529
    }

530
    if (hasNonPositionedChildren) {
531
      size = new Size(width, height);
532 533 534
      assert(size.width == constraints.constrainWidth(width));
      assert(size.height == constraints.constrainHeight(height));
    } else {
535
      size = constraints.biggest;
536
    }
537

538
    assert(size.isFinite);
539 540 541

    child = firstChild;
    while (child != null) {
Hixie's avatar
Hixie committed
542
      final StackParentData childParentData = child.parentData;
543

Hixie's avatar
Hixie committed
544
      if (!childParentData.isPositioned) {
545
        childParentData.offset = _resolvedAlignment.alongOffset(size - child.size);
Hans Muller's avatar
Hans Muller committed
546
      } else {
547
        BoxConstraints childConstraints = const BoxConstraints();
548

Hixie's avatar
Hixie committed
549
        if (childParentData.left != null && childParentData.right != null)
550
          childConstraints = childConstraints.tighten(width: size.width - childParentData.right - childParentData.left);
551
        else if (childParentData.width != null)
552
          childConstraints = childConstraints.tighten(width: childParentData.width);
553

Hixie's avatar
Hixie committed
554
        if (childParentData.top != null && childParentData.bottom != null)
555
          childConstraints = childConstraints.tighten(height: size.height - childParentData.bottom - childParentData.top);
556
        else if (childParentData.height != null)
557
          childConstraints = childConstraints.tighten(height: childParentData.height);
558 559 560

        child.layout(childConstraints, parentUsesSize: true);

561 562
        double x;
        if (childParentData.left != null) {
Hixie's avatar
Hixie committed
563
          x = childParentData.left;
564
        } else if (childParentData.right != null) {
Hixie's avatar
Hixie committed
565
          x = size.width - childParentData.right - child.size.width;
566 567 568
        } else {
          x = _resolvedAlignment.alongOffset(size - child.size).dx;
        }
569 570 571

        if (x < 0.0 || x + child.size.width > size.width)
          _hasVisualOverflow = true;
572

573 574
        double y;
        if (childParentData.top != null) {
Hixie's avatar
Hixie committed
575
          y = childParentData.top;
576
        } else if (childParentData.bottom != null) {
Hixie's avatar
Hixie committed
577
          y = size.height - childParentData.bottom - child.size.height;
578 579 580
        } else {
          y = _resolvedAlignment.alongOffset(size - child.size).dy;
        }
581 582 583

        if (y < 0.0 || y + child.size.height > size.height)
          _hasVisualOverflow = true;
584

585
        childParentData.offset = new Offset(x, y);
586 587
      }

Hixie's avatar
Hixie committed
588 589
      assert(child.parentData == childParentData);
      child = childParentData.nextSibling;
590 591 592
    }
  }

593
  @override
594
  bool hitTestChildren(HitTestResult result, { Offset position }) {
Adam Barth's avatar
Adam Barth committed
595
    return defaultHitTestChildren(result, position: position);
596 597
  }

598 599 600 601 602 603 604 605
  /// Override in subclasses to customize how the stack paints.
  ///
  /// By default, the stack uses [defaultPaint]. This function is called by
  /// [paint] after potentially applying a clip to contain visual overflow.
  @protected
  void paintStack(PaintingContext context, Offset offset) {
    defaultPaint(context, offset);
  }
Hans Muller's avatar
Hans Muller committed
606

607
  @override
608
  void paint(PaintingContext context, Offset offset) {
609
    if (_overflow == Overflow.clip && _hasVisualOverflow) {
610
      context.pushClipRect(needsCompositing, offset, Offset.zero & size, paintStack);
611
    } else {
Hans Muller's avatar
Hans Muller committed
612
      paintStack(context, offset);
613
    }
614
  }
Hixie's avatar
Hixie committed
615

616
  @override
617
  Rect describeApproximatePaintClip(RenderObject child) => _hasVisualOverflow ? Offset.zero & size : null;
Adam Barth's avatar
Adam Barth committed
618 619

  @override
620 621 622 623 624 625
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
    properties.add(new DiagnosticsProperty<AlignmentGeometry>('alignment', alignment));
    properties.add(new EnumProperty<TextDirection>('textDirection', textDirection));
    properties.add(new EnumProperty<StackFit>('fit', fit));
    properties.add(new EnumProperty<Overflow>('overflow', overflow));
Adam Barth's avatar
Adam Barth committed
626
  }
627
}
Hans Muller's avatar
Hans Muller committed
628 629 630

/// Implements the same layout algorithm as RenderStack but only paints the child
/// specified by index.
631 632
///
/// Although only one child is displayed, the cost of the layout algorithm is
Hans Muller's avatar
Hans Muller committed
633
/// still O(N), like an ordinary stack.
634 635 636
class RenderIndexedStack extends RenderStack {
  /// Creates a stack render object that paints a single child.
  ///
637
  /// If the [index] parameter is null, nothing is displayed.
Hans Muller's avatar
Hans Muller committed
638 639
  RenderIndexedStack({
    List<RenderBox> children,
640
    AlignmentGeometry alignment = AlignmentDirectional.topStart,
641
    TextDirection textDirection,
642
    int index = 0,
Hans Muller's avatar
Hans Muller committed
643
  }) : _index = index, super(
Adam Barth's avatar
Adam Barth committed
644
    children: children,
645 646
    alignment: alignment,
    textDirection: textDirection,
647
  );
Hans Muller's avatar
Hans Muller committed
648

649 650 651 652 653 654
  @override
  void visitChildrenForSemantics(RenderObjectVisitor visitor) {
    if (index != null)
      visitor(_childAtIndex());
  }

655
  /// The index of the child to show, null if nothing is to be displayed.
Hans Muller's avatar
Hans Muller committed
656 657
  int get index => _index;
  int _index;
658
  set index(int value) {
Hans Muller's avatar
Hans Muller committed
659 660 661 662 663 664 665
    if (_index != value) {
      _index = value;
      markNeedsLayout();
    }
  }

  RenderBox _childAtIndex() {
666
    assert(index != null);
Hans Muller's avatar
Hans Muller committed
667 668 669
    RenderBox child = firstChild;
    int i = 0;
    while (child != null && i < index) {
Hixie's avatar
Hixie committed
670 671
      final StackParentData childParentData = child.parentData;
      child = childParentData.nextSibling;
Hans Muller's avatar
Hans Muller committed
672 673 674 675 676 677 678
      i += 1;
    }
    assert(i == index);
    assert(child != null);
    return child;
  }

679
  @override
680
  bool hitTestChildren(HitTestResult result, { @required Offset position }) {
681
    if (firstChild == null || index == null)
Adam Barth's avatar
Adam Barth committed
682
      return false;
Hans Muller's avatar
Hans Muller committed
683
    assert(position != null);
684
    final RenderBox child = _childAtIndex();
Hixie's avatar
Hixie committed
685
    final StackParentData childParentData = child.parentData;
686
    return child.hitTest(result, position: position - childParentData.offset);
Hans Muller's avatar
Hans Muller committed
687 688
  }

689
  @override
Hans Muller's avatar
Hans Muller committed
690
  void paintStack(PaintingContext context, Offset offset) {
691
    if (firstChild == null || index == null)
Hans Muller's avatar
Hans Muller committed
692
      return;
693
    final RenderBox child = _childAtIndex();
Hixie's avatar
Hixie committed
694
    final StackParentData childParentData = child.parentData;
Adam Barth's avatar
Adam Barth committed
695
    context.paintChild(child, childParentData.offset + offset);
Hans Muller's avatar
Hans Muller committed
696
  }
Adam Barth's avatar
Adam Barth committed
697 698

  @override
699 700 701
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
    properties.add(new IntProperty('index', index));
Adam Barth's avatar
Adam Barth committed
702
  }
Hans Muller's avatar
Hans Muller committed
703
}