wrap.dart 29.4 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
Adam Barth's avatar
Adam Barth committed
2 3 4 5 6 7
// 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;

import 'box.dart';
8
import 'layer.dart';
9
import 'layout_helper.dart';
Adam Barth's avatar
Adam Barth committed
10 11 12 13 14 15 16 17
import 'object.dart';

/// How [Wrap] should align objects.
///
/// Used both to align children within a run in the main axis as well as to
/// align the runs themselves in the cross axis.
enum WrapAlignment {
  /// Place the objects as close to the start of the axis as possible.
18 19 20 21 22 23
  ///
  /// If this value is used in a horizontal direction, a [TextDirection] must be
  /// available to determine if the start is the left or the right.
  ///
  /// If this value is used in a vertical direction, a [VerticalDirection] must be
  /// available to determine if the start is the top or the bottom.
Adam Barth's avatar
Adam Barth committed
24 25 26
  start,

  /// Place the objects as close to the end of the axis as possible.
27 28 29 30 31 32
  ///
  /// If this value is used in a horizontal direction, a [TextDirection] must be
  /// available to determine if the end is the left or the right.
  ///
  /// If this value is used in a vertical direction, a [VerticalDirection] must be
  /// available to determine if the end is the top or the bottom.
Adam Barth's avatar
Adam Barth committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
  end,

  /// Place the objects as close to the middle of the axis as possible.
  center,

  /// Place the free space evenly between the objects.
  spaceBetween,

  /// Place the free space evenly between the objects as well as half of that
  /// space before and after the first and last objects.
  spaceAround,

  /// Place the free space evenly between the objects as well as before and
  /// after the first and last objects.
  spaceEvenly,
}

/// Who [Wrap] should align children within a run in the cross axis.
enum WrapCrossAlignment {
  /// Place the children as close to the start of the run in the cross axis as
  /// possible.
54 55 56 57 58 59
  ///
  /// If this value is used in a horizontal direction, a [TextDirection] must be
  /// available to determine if the start is the left or the right.
  ///
  /// If this value is used in a vertical direction, a [VerticalDirection] must be
  /// available to determine if the start is the top or the bottom.
Adam Barth's avatar
Adam Barth committed
60 61 62 63
  start,

  /// Place the children as close to the end of the run in the cross axis as
  /// possible.
64 65 66 67 68 69
  ///
  /// If this value is used in a horizontal direction, a [TextDirection] must be
  /// available to determine if the end is the left or the right.
  ///
  /// If this value is used in a vertical direction, a [VerticalDirection] must be
  /// available to determine if the end is the top or the bottom.
Adam Barth's avatar
Adam Barth committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
  end,

  /// Place the children as close to the middle of the run in the cross axis as
  /// possible.
  center,

  // TODO(ianh): baseline.
}

class _RunMetrics {
  _RunMetrics(this.mainAxisExtent, this.crossAxisExtent, this.childCount);

  final double mainAxisExtent;
  final double crossAxisExtent;
  final int childCount;
}

/// Parent data for use with [RenderWrap].
88
class WrapParentData extends ContainerBoxParentData<RenderBox> {
Adam Barth's avatar
Adam Barth committed
89 90 91
  int _runIndex = 0;
}

92 93 94 95 96 97 98 99 100 101 102 103 104 105
/// Displays its children in multiple horizontal or vertical runs.
///
/// A [RenderWrap] lays out each child and attempts to place the child adjacent
/// to the previous child in the main axis, given by [direction], leaving
/// [spacing] space in between. If there is not enough space to fit the child,
/// [RenderWrap] creates a new _run_ adjacent to the existing children in the
/// cross axis.
///
/// After all the children have been allocated to runs, the children within the
/// runs are positioned according to the [alignment] in the main axis and
/// according to the [crossAxisAlignment] in the cross axis.
///
/// The runs themselves are then positioned in the cross axis according to the
/// [runSpacing] and [runAlignment].
106 107 108
class RenderWrap extends RenderBox
    with ContainerRenderObjectMixin<RenderBox, WrapParentData>,
         RenderBoxContainerDefaultsMixin<RenderBox, WrapParentData> {
109 110 111 112
  /// Creates a wrap render object.
  ///
  /// By default, the wrap layout is horizontal and both the children and the
  /// runs are aligned to the start.
Adam Barth's avatar
Adam Barth committed
113
  RenderWrap({
114
    List<RenderBox>? children,
115 116 117 118 119 120
    Axis direction = Axis.horizontal,
    WrapAlignment alignment = WrapAlignment.start,
    double spacing = 0.0,
    WrapAlignment runAlignment = WrapAlignment.start,
    double runSpacing = 0.0,
    WrapCrossAlignment crossAxisAlignment = WrapCrossAlignment.start,
121
    TextDirection? textDirection,
122
    VerticalDirection verticalDirection = VerticalDirection.down,
123
    Clip clipBehavior = Clip.none,
124 125 126 127 128 129
  }) : assert(direction != null),
       assert(alignment != null),
       assert(spacing != null),
       assert(runAlignment != null),
       assert(runSpacing != null),
       assert(crossAxisAlignment != null),
130
       assert(clipBehavior != null),
131
       _direction = direction,
Adam Barth's avatar
Adam Barth committed
132 133 134 135
       _alignment = alignment,
       _spacing = spacing,
       _runAlignment = runAlignment,
       _runSpacing = runSpacing,
136 137
       _crossAxisAlignment = crossAxisAlignment,
       _textDirection = textDirection,
138 139
       _verticalDirection = verticalDirection,
       _clipBehavior = clipBehavior {
Adam Barth's avatar
Adam Barth committed
140 141 142 143 144 145 146 147 148 149 150 151 152
    addAll(children);
  }

  /// The direction to use as the main axis.
  ///
  /// For example, if [direction] is [Axis.horizontal], the default, the
  /// children are placed adjacent to one another in a horizontal run until the
  /// available horizontal space is consumed, at which point a subsequent
  /// children are placed in a new run vertically adjacent to the previous run.
  Axis get direction => _direction;
  Axis _direction;
  set direction (Axis value) {
    assert(value != null);
153
    if (_direction == value) {
Adam Barth's avatar
Adam Barth committed
154
      return;
155
    }
Adam Barth's avatar
Adam Barth committed
156 157 158 159
    _direction = value;
    markNeedsLayout();
  }

160
  /// How the children within a run should be placed in the main axis.
Adam Barth's avatar
Adam Barth committed
161 162
  ///
  /// For example, if [alignment] is [WrapAlignment.center], the children in
163
  /// each run are grouped together in the center of their run in the main axis.
Adam Barth's avatar
Adam Barth committed
164 165 166 167 168 169 170 171 172 173 174 175 176
  ///
  /// Defaults to [WrapAlignment.start].
  ///
  /// See also:
  ///
  ///  * [runAlignment], which controls how the runs are placed relative to each
  ///    other in the cross axis.
  ///  * [crossAxisAlignment], which controls how the children within each run
  ///    are placed relative to each other in the cross axis.
  WrapAlignment get alignment => _alignment;
  WrapAlignment _alignment;
  set alignment (WrapAlignment value) {
    assert(value != null);
177
    if (_alignment == value) {
Adam Barth's avatar
Adam Barth committed
178
      return;
179
    }
Adam Barth's avatar
Adam Barth committed
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
    _alignment = value;
    markNeedsLayout();
  }

  /// How much space to place between children in a run in the main axis.
  ///
  /// For example, if [spacing] is 10.0, the children will be spaced at least
  /// 10.0 logical pixels apart in the main axis.
  ///
  /// If there is additional free space in a run (e.g., because the wrap has a
  /// minimum size that is not filled or because some runs are longer than
  /// others), the additional free space will be allocated according to the
  /// [alignment].
  ///
  /// Defaults to 0.0.
  double get spacing => _spacing;
  double _spacing;
  set spacing (double value) {
    assert(value != null);
199
    if (_spacing == value) {
Adam Barth's avatar
Adam Barth committed
200
      return;
201
    }
Adam Barth's avatar
Adam Barth committed
202 203 204 205 206 207 208
    _spacing = value;
    markNeedsLayout();
  }

  /// How the runs themselves should be placed in the cross axis.
  ///
  /// For example, if [runAlignment] is [WrapAlignment.center], the runs are
209
  /// grouped together in the center of the overall [RenderWrap] in the cross
Adam Barth's avatar
Adam Barth committed
210 211 212 213 214 215 216 217 218 219 220 221 222 223
  /// axis.
  ///
  /// Defaults to [WrapAlignment.start].
  ///
  /// See also:
  ///
  ///  * [alignment], which controls how the children within each run are placed
  ///    relative to each other in the main axis.
  ///  * [crossAxisAlignment], which controls how the children within each run
  ///    are placed relative to each other in the cross axis.
  WrapAlignment get runAlignment => _runAlignment;
  WrapAlignment _runAlignment;
  set runAlignment (WrapAlignment value) {
    assert(value != null);
224
    if (_runAlignment == value) {
Adam Barth's avatar
Adam Barth committed
225
      return;
226
    }
Adam Barth's avatar
Adam Barth committed
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
    _runAlignment = value;
    markNeedsLayout();
  }

  /// How much space to place between the runs themselves in the cross axis.
  ///
  /// For example, if [runSpacing] is 10.0, the runs will be spaced at least
  /// 10.0 logical pixels apart in the cross axis.
  ///
  /// If there is additional free space in the overall [RenderWrap] (e.g.,
  /// because the wrap has a minimum size that is not filled), the additional
  /// free space will be allocated according to the [runAlignment].
  ///
  /// Defaults to 0.0.
  double get runSpacing => _runSpacing;
  double _runSpacing;
  set runSpacing (double value) {
    assert(value != null);
245
    if (_runSpacing == value) {
Adam Barth's avatar
Adam Barth committed
246
      return;
247
    }
Adam Barth's avatar
Adam Barth committed
248 249 250 251 252 253 254 255
    _runSpacing = value;
    markNeedsLayout();
  }

  /// How the children within a run should be aligned relative to each other in
  /// the cross axis.
  ///
  /// For example, if this is set to [WrapCrossAlignment.end], and the
256
  /// [direction] is [Axis.horizontal], then the children within each
Adam Barth's avatar
Adam Barth committed
257 258 259 260 261 262 263 264 265 266 267 268 269 270
  /// run will have their bottom edges aligned to the bottom edge of the run.
  ///
  /// Defaults to [WrapCrossAlignment.start].
  ///
  /// See also:
  ///
  ///  * [alignment], which controls how the children within each run are placed
  ///    relative to each other in the main axis.
  ///  * [runAlignment], which controls how the runs are placed relative to each
  ///    other in the cross axis.
  WrapCrossAlignment get crossAxisAlignment => _crossAxisAlignment;
  WrapCrossAlignment _crossAxisAlignment;
  set crossAxisAlignment (WrapCrossAlignment value) {
    assert(value != null);
271
    if (_crossAxisAlignment == value) {
Adam Barth's avatar
Adam Barth committed
272
      return;
273
    }
Adam Barth's avatar
Adam Barth committed
274 275 276 277
    _crossAxisAlignment = value;
    markNeedsLayout();
  }

278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
  /// Determines the order to lay children out horizontally and how to interpret
  /// `start` and `end` in the horizontal direction.
  ///
  /// If the [direction] is [Axis.horizontal], this controls the order in which
  /// children are positioned (left-to-right or right-to-left), and the meaning
  /// of the [alignment] property's [WrapAlignment.start] and
  /// [WrapAlignment.end] values.
  ///
  /// If the [direction] is [Axis.horizontal], and either the
  /// [alignment] is either [WrapAlignment.start] or [WrapAlignment.end], or
  /// there's more than one child, then the [textDirection] must not be null.
  ///
  /// If the [direction] is [Axis.vertical], this controls the order in
  /// which runs are positioned, the meaning of the [runAlignment] property's
  /// [WrapAlignment.start] and [WrapAlignment.end] values, as well as the
  /// [crossAxisAlignment] property's [WrapCrossAlignment.start] and
  /// [WrapCrossAlignment.end] values.
  ///
  /// If the [direction] is [Axis.vertical], and either the
  /// [runAlignment] is either [WrapAlignment.start] or [WrapAlignment.end], the
  /// [crossAxisAlignment] is either [WrapCrossAlignment.start] or
  /// [WrapCrossAlignment.end], or there's more than one child, then the
  /// [textDirection] must not be null.
301 302 303
  TextDirection? get textDirection => _textDirection;
  TextDirection? _textDirection;
  set textDirection(TextDirection? value) {
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 335 336 337 338 339 340
    if (_textDirection != value) {
      _textDirection = value;
      markNeedsLayout();
    }
  }

  /// Determines the order to lay children out vertically and how to interpret
  /// `start` and `end` in the vertical direction.
  ///
  /// If the [direction] is [Axis.vertical], this controls which order children
  /// are painted in (down or up), the meaning of the [alignment] property's
  /// [WrapAlignment.start] and [WrapAlignment.end] values.
  ///
  /// If the [direction] is [Axis.vertical], and either the [alignment]
  /// is either [WrapAlignment.start] or [WrapAlignment.end], or there's
  /// more than one child, then the [verticalDirection] must not be null.
  ///
  /// If the [direction] is [Axis.horizontal], this controls the order in which
  /// runs are positioned, the meaning of the [runAlignment] property's
  /// [WrapAlignment.start] and [WrapAlignment.end] values, as well as the
  /// [crossAxisAlignment] property's [WrapCrossAlignment.start] and
  /// [WrapCrossAlignment.end] values.
  ///
  /// If the [direction] is [Axis.horizontal], and either the
  /// [runAlignment] is either [WrapAlignment.start] or [WrapAlignment.end], the
  /// [crossAxisAlignment] is either [WrapCrossAlignment.start] or
  /// [WrapCrossAlignment.end], or there's more than one child, then the
  /// [verticalDirection] must not be null.
  VerticalDirection get verticalDirection => _verticalDirection;
  VerticalDirection _verticalDirection;
  set verticalDirection(VerticalDirection value) {
    if (_verticalDirection != value) {
      _verticalDirection = value;
      markNeedsLayout();
    }
  }

341
  /// {@macro flutter.material.Material.clipBehavior}
342 343 344 345 346 347 348 349 350 351 352 353 354
  ///
  /// Defaults to [Clip.none], and must not be null.
  Clip get clipBehavior => _clipBehavior;
  Clip _clipBehavior = Clip.none;
  set clipBehavior(Clip value) {
    assert(value != null);
    if (value != _clipBehavior) {
      _clipBehavior = value;
      markNeedsPaint();
      markNeedsSemanticsUpdate();
    }
  }

355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
  bool get _debugHasNecessaryDirections {
    assert(direction != null);
    assert(alignment != null);
    assert(runAlignment != null);
    assert(crossAxisAlignment != null);
    if (firstChild != null && lastChild != firstChild) {
      // i.e. there's more than one child
      switch (direction) {
        case Axis.horizontal:
          assert(textDirection != null, 'Horizontal $runtimeType with multiple children has a null textDirection, so the layout order is undefined.');
          break;
        case Axis.vertical:
          assert(verticalDirection != null, 'Vertical $runtimeType with multiple children has a null verticalDirection, so the layout order is undefined.');
          break;
      }
    }
    if (alignment == WrapAlignment.start || alignment == WrapAlignment.end) {
      switch (direction) {
        case Axis.horizontal:
          assert(textDirection != null, 'Horizontal $runtimeType with alignment $alignment has a null textDirection, so the alignment cannot be resolved.');
          break;
        case Axis.vertical:
          assert(verticalDirection != null, 'Vertical $runtimeType with alignment $alignment has a null verticalDirection, so the alignment cannot be resolved.');
          break;
      }
    }
    if (runAlignment == WrapAlignment.start || runAlignment == WrapAlignment.end) {
      switch (direction) {
        case Axis.horizontal:
          assert(verticalDirection != null, 'Horizontal $runtimeType with runAlignment $runAlignment has a null verticalDirection, so the alignment cannot be resolved.');
          break;
        case Axis.vertical:
          assert(textDirection != null, 'Vertical $runtimeType with runAlignment $runAlignment has a null textDirection, so the alignment cannot be resolved.');
          break;
      }
    }
    if (crossAxisAlignment == WrapCrossAlignment.start || crossAxisAlignment == WrapCrossAlignment.end) {
      switch (direction) {
        case Axis.horizontal:
          assert(verticalDirection != null, 'Horizontal $runtimeType with crossAxisAlignment $crossAxisAlignment has a null verticalDirection, so the alignment cannot be resolved.');
          break;
        case Axis.vertical:
          assert(textDirection != null, 'Vertical $runtimeType with crossAxisAlignment $crossAxisAlignment has a null textDirection, so the alignment cannot be resolved.');
          break;
      }
    }
    return true;
  }

Adam Barth's avatar
Adam Barth committed
404 405
  @override
  void setupParentData(RenderBox child) {
406
    if (child.parentData is! WrapParentData) {
407
      child.parentData = WrapParentData();
408
    }
Adam Barth's avatar
Adam Barth committed
409 410 411 412 413 414 415
  }

  @override
  double computeMinIntrinsicWidth(double height) {
    switch (direction) {
      case Axis.horizontal:
        double width = 0.0;
416
        RenderBox? child = firstChild;
Adam Barth's avatar
Adam Barth committed
417
        while (child != null) {
418
          width = math.max(width, child.getMinIntrinsicWidth(double.infinity));
Adam Barth's avatar
Adam Barth committed
419 420 421 422
          child = childAfter(child);
        }
        return width;
      case Axis.vertical:
423
        return computeDryLayout(BoxConstraints(maxHeight: height)).width;
Adam Barth's avatar
Adam Barth committed
424 425 426 427 428 429 430
    }
  }

  @override
  double computeMaxIntrinsicWidth(double height) {
    switch (direction) {
      case Axis.horizontal:
431
        double width = 0.0;
432
        RenderBox? child = firstChild;
Adam Barth's avatar
Adam Barth committed
433
        while (child != null) {
434
          width += child.getMaxIntrinsicWidth(double.infinity);
Adam Barth's avatar
Adam Barth committed
435 436 437 438
          child = childAfter(child);
        }
        return width;
      case Axis.vertical:
439
        return computeDryLayout(BoxConstraints(maxHeight: height)).width;
Adam Barth's avatar
Adam Barth committed
440 441 442 443 444 445 446
    }
  }

  @override
  double computeMinIntrinsicHeight(double width) {
    switch (direction) {
      case Axis.horizontal:
447
        return computeDryLayout(BoxConstraints(maxWidth: width)).height;
Adam Barth's avatar
Adam Barth committed
448 449
      case Axis.vertical:
        double height = 0.0;
450
        RenderBox? child = firstChild;
Adam Barth's avatar
Adam Barth committed
451
        while (child != null) {
452
          height = math.max(height, child.getMinIntrinsicHeight(double.infinity));
Adam Barth's avatar
Adam Barth committed
453 454 455 456 457 458 459 460 461 462
          child = childAfter(child);
        }
        return height;
    }
  }

  @override
  double computeMaxIntrinsicHeight(double width) {
    switch (direction) {
      case Axis.horizontal:
463
        return computeDryLayout(BoxConstraints(maxWidth: width)).height;
Adam Barth's avatar
Adam Barth committed
464
      case Axis.vertical:
465
        double height = 0.0;
466
        RenderBox? child = firstChild;
Adam Barth's avatar
Adam Barth committed
467
        while (child != null) {
468
          height += child.getMaxIntrinsicHeight(double.infinity);
Adam Barth's avatar
Adam Barth committed
469 470 471 472 473 474 475
          child = childAfter(child);
        }
        return height;
    }
  }

  @override
476
  double? computeDistanceToActualBaseline(TextBaseline baseline) {
Adam Barth's avatar
Adam Barth committed
477 478 479
    return defaultComputeDistanceToHighestActualBaseline(baseline);
  }

480
  double _getMainAxisExtent(Size childSize) {
Adam Barth's avatar
Adam Barth committed
481 482
    switch (direction) {
      case Axis.horizontal:
483
        return childSize.width;
Adam Barth's avatar
Adam Barth committed
484
      case Axis.vertical:
485
        return childSize.height;
Adam Barth's avatar
Adam Barth committed
486 487 488
    }
  }

489
  double _getCrossAxisExtent(Size childSize) {
Adam Barth's avatar
Adam Barth committed
490 491
    switch (direction) {
      case Axis.horizontal:
492
        return childSize.height;
Adam Barth's avatar
Adam Barth committed
493
      case Axis.vertical:
494
        return childSize.width;
Adam Barth's avatar
Adam Barth committed
495 496 497 498 499 500
    }
  }

  Offset _getOffset(double mainAxisOffset, double crossAxisOffset) {
    switch (direction) {
      case Axis.horizontal:
501
        return Offset(mainAxisOffset, crossAxisOffset);
Adam Barth's avatar
Adam Barth committed
502
      case Axis.vertical:
503
        return Offset(crossAxisOffset, mainAxisOffset);
Adam Barth's avatar
Adam Barth committed
504 505 506
    }
  }

507 508
  double _getChildCrossAxisOffset(bool flipCrossAxis, double runCrossAxisExtent, double childCrossAxisExtent) {
    final double freeSpace = runCrossAxisExtent - childCrossAxisExtent;
Adam Barth's avatar
Adam Barth committed
509 510
    switch (crossAxisAlignment) {
      case WrapCrossAlignment.start:
511
        return flipCrossAxis ? freeSpace : 0.0;
Adam Barth's avatar
Adam Barth committed
512
      case WrapCrossAlignment.end:
513 514 515
        return flipCrossAxis ? 0.0 : freeSpace;
      case WrapCrossAlignment.center:
        return freeSpace / 2.0;
Adam Barth's avatar
Adam Barth committed
516 517 518 519 520
    }
  }

  bool _hasVisualOverflow = false;

521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
  @override
  Size computeDryLayout(BoxConstraints constraints) {
    return _computeDryLayout(constraints);
  }

  Size _computeDryLayout(BoxConstraints constraints, [ChildLayouter layoutChild = ChildLayoutHelper.dryLayoutChild]) {
    final BoxConstraints childConstraints;
    double mainAxisLimit = 0.0;
    switch (direction) {
      case Axis.horizontal:
        childConstraints = BoxConstraints(maxWidth: constraints.maxWidth);
        mainAxisLimit = constraints.maxWidth;
        break;
      case Axis.vertical:
        childConstraints = BoxConstraints(maxHeight: constraints.maxHeight);
        mainAxisLimit = constraints.maxHeight;
        break;
    }

    double mainAxisExtent = 0.0;
    double crossAxisExtent = 0.0;
    double runMainAxisExtent = 0.0;
    double runCrossAxisExtent = 0.0;
    int childCount = 0;
    RenderBox? child = firstChild;
    while (child != null) {
      final Size childSize = layoutChild(child, childConstraints);
      final double childMainAxisExtent = _getMainAxisExtent(childSize);
      final double childCrossAxisExtent = _getCrossAxisExtent(childSize);
      // There must be at least one child before we move on to the next run.
      if (childCount > 0 && runMainAxisExtent + childMainAxisExtent + spacing > mainAxisLimit) {
        mainAxisExtent = math.max(mainAxisExtent, runMainAxisExtent);
        crossAxisExtent += runCrossAxisExtent + runSpacing;
        runMainAxisExtent = 0.0;
        runCrossAxisExtent = 0.0;
        childCount = 0;
      }
      runMainAxisExtent += childMainAxisExtent;
      runCrossAxisExtent = math.max(runCrossAxisExtent, childCrossAxisExtent);
560
      if (childCount > 0) {
561
        runMainAxisExtent += spacing;
562
      }
563 564 565 566 567 568 569 570 571 572 573 574 575 576
      childCount += 1;
      child = childAfter(child);
    }
    crossAxisExtent += runCrossAxisExtent;
    mainAxisExtent = math.max(mainAxisExtent, runMainAxisExtent);

    switch (direction) {
      case Axis.horizontal:
        return constraints.constrain(Size(mainAxisExtent, crossAxisExtent));
      case Axis.vertical:
        return constraints.constrain(Size(crossAxisExtent, mainAxisExtent));
    }
  }

Adam Barth's avatar
Adam Barth committed
577 578
  @override
  void performLayout() {
579
    final BoxConstraints constraints = this.constraints;
580
    assert(_debugHasNecessaryDirections);
Adam Barth's avatar
Adam Barth committed
581
    _hasVisualOverflow = false;
582
    RenderBox? child = firstChild;
Adam Barth's avatar
Adam Barth committed
583 584 585 586
    if (child == null) {
      size = constraints.smallest;
      return;
    }
587
    final BoxConstraints childConstraints;
Adam Barth's avatar
Adam Barth committed
588
    double mainAxisLimit = 0.0;
589 590
    bool flipMainAxis = false;
    bool flipCrossAxis = false;
Adam Barth's avatar
Adam Barth committed
591 592
    switch (direction) {
      case Axis.horizontal:
593
        childConstraints = BoxConstraints(maxWidth: constraints.maxWidth);
Adam Barth's avatar
Adam Barth committed
594
        mainAxisLimit = constraints.maxWidth;
595
        if (textDirection == TextDirection.rtl) {
596
          flipMainAxis = true;
597 598
        }
        if (verticalDirection == VerticalDirection.up) {
599
          flipCrossAxis = true;
600
        }
Adam Barth's avatar
Adam Barth committed
601 602
        break;
      case Axis.vertical:
603
        childConstraints = BoxConstraints(maxHeight: constraints.maxHeight);
Adam Barth's avatar
Adam Barth committed
604
        mainAxisLimit = constraints.maxHeight;
605
        if (verticalDirection == VerticalDirection.up) {
606
          flipMainAxis = true;
607 608
        }
        if (textDirection == TextDirection.rtl) {
609
          flipCrossAxis = true;
610
        }
Adam Barth's avatar
Adam Barth committed
611 612 613 614
        break;
    }
    assert(childConstraints != null);
    assert(mainAxisLimit != null);
615 616
    final double spacing = this.spacing;
    final double runSpacing = this.runSpacing;
Adam Barth's avatar
Adam Barth committed
617 618 619 620 621 622 623 624
    final List<_RunMetrics> runMetrics = <_RunMetrics>[];
    double mainAxisExtent = 0.0;
    double crossAxisExtent = 0.0;
    double runMainAxisExtent = 0.0;
    double runCrossAxisExtent = 0.0;
    int childCount = 0;
    while (child != null) {
      child.layout(childConstraints, parentUsesSize: true);
625 626
      final double childMainAxisExtent = _getMainAxisExtent(child.size);
      final double childCrossAxisExtent = _getCrossAxisExtent(child.size);
627
      if (childCount > 0 && runMainAxisExtent + spacing + childMainAxisExtent > mainAxisLimit) {
Adam Barth's avatar
Adam Barth committed
628 629
        mainAxisExtent = math.max(mainAxisExtent, runMainAxisExtent);
        crossAxisExtent += runCrossAxisExtent;
630
        if (runMetrics.isNotEmpty) {
Adam Barth's avatar
Adam Barth committed
631
          crossAxisExtent += runSpacing;
632
        }
633
        runMetrics.add(_RunMetrics(runMainAxisExtent, runCrossAxisExtent, childCount));
Adam Barth's avatar
Adam Barth committed
634 635 636 637 638
        runMainAxisExtent = 0.0;
        runCrossAxisExtent = 0.0;
        childCount = 0;
      }
      runMainAxisExtent += childMainAxisExtent;
639
      if (childCount > 0) {
Adam Barth's avatar
Adam Barth committed
640
        runMainAxisExtent += spacing;
641
      }
Adam Barth's avatar
Adam Barth committed
642 643
      runCrossAxisExtent = math.max(runCrossAxisExtent, childCrossAxisExtent);
      childCount += 1;
644
      final WrapParentData childParentData = child.parentData! as WrapParentData;
Adam Barth's avatar
Adam Barth committed
645 646 647 648 649
      childParentData._runIndex = runMetrics.length;
      child = childParentData.nextSibling;
    }
    if (childCount > 0) {
      mainAxisExtent = math.max(mainAxisExtent, runMainAxisExtent);
650
      crossAxisExtent += runCrossAxisExtent;
651
      if (runMetrics.isNotEmpty) {
652
        crossAxisExtent += runSpacing;
653
      }
654
      runMetrics.add(_RunMetrics(runMainAxisExtent, runCrossAxisExtent, childCount));
Adam Barth's avatar
Adam Barth committed
655 656 657 658 659 660 661 662 663 664
    }

    final int runCount = runMetrics.length;
    assert(runCount > 0);

    double containerMainAxisExtent = 0.0;
    double containerCrossAxisExtent = 0.0;

    switch (direction) {
      case Axis.horizontal:
665
        size = constraints.constrain(Size(mainAxisExtent, crossAxisExtent));
Adam Barth's avatar
Adam Barth committed
666 667 668 669
        containerMainAxisExtent = size.width;
        containerCrossAxisExtent = size.height;
        break;
      case Axis.vertical:
670
        size = constraints.constrain(Size(crossAxisExtent, mainAxisExtent));
Adam Barth's avatar
Adam Barth committed
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
        containerMainAxisExtent = size.height;
        containerCrossAxisExtent = size.width;
        break;
    }

    _hasVisualOverflow = containerMainAxisExtent < mainAxisExtent || containerCrossAxisExtent < crossAxisExtent;

    final double crossAxisFreeSpace = math.max(0.0, containerCrossAxisExtent - crossAxisExtent);
    double runLeadingSpace = 0.0;
    double runBetweenSpace = 0.0;
    switch (runAlignment) {
      case WrapAlignment.start:
        break;
      case WrapAlignment.end:
        runLeadingSpace = crossAxisFreeSpace;
        break;
      case WrapAlignment.center:
        runLeadingSpace = crossAxisFreeSpace / 2.0;
        break;
      case WrapAlignment.spaceBetween:
        runBetweenSpace = runCount > 1 ? crossAxisFreeSpace / (runCount - 1) : 0.0;
        break;
      case WrapAlignment.spaceAround:
        runBetweenSpace = crossAxisFreeSpace / runCount;
        runLeadingSpace = runBetweenSpace / 2.0;
        break;
      case WrapAlignment.spaceEvenly:
        runBetweenSpace = crossAxisFreeSpace / (runCount + 1);
        runLeadingSpace = runBetweenSpace;
        break;
    }

703 704 705
    runBetweenSpace += runSpacing;
    double crossAxisOffset = flipCrossAxis ? containerCrossAxisExtent - runLeadingSpace : runLeadingSpace;

Adam Barth's avatar
Adam Barth committed
706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
    child = firstChild;
    for (int i = 0; i < runCount; ++i) {
      final _RunMetrics metrics = runMetrics[i];
      final double runMainAxisExtent = metrics.mainAxisExtent;
      final double runCrossAxisExtent = metrics.crossAxisExtent;
      final int childCount = metrics.childCount;

      final double mainAxisFreeSpace = math.max(0.0, containerMainAxisExtent - runMainAxisExtent);
      double childLeadingSpace = 0.0;
      double childBetweenSpace = 0.0;

      switch (alignment) {
        case WrapAlignment.start:
          break;
        case WrapAlignment.end:
          childLeadingSpace = mainAxisFreeSpace;
          break;
        case WrapAlignment.center:
          childLeadingSpace = mainAxisFreeSpace / 2.0;
          break;
        case WrapAlignment.spaceBetween:
          childBetweenSpace = childCount > 1 ? mainAxisFreeSpace / (childCount - 1) : 0.0;
          break;
        case WrapAlignment.spaceAround:
          childBetweenSpace = mainAxisFreeSpace / childCount;
          childLeadingSpace = childBetweenSpace / 2.0;
          break;
        case WrapAlignment.spaceEvenly:
          childBetweenSpace = mainAxisFreeSpace / (childCount + 1);
          childLeadingSpace = childBetweenSpace;
          break;
      }

739 740 741
      childBetweenSpace += spacing;
      double childMainPosition = flipMainAxis ? containerMainAxisExtent - childLeadingSpace : childLeadingSpace;

742
      if (flipCrossAxis) {
743
        crossAxisOffset -= runCrossAxisExtent;
744
      }
745

Adam Barth's avatar
Adam Barth committed
746
      while (child != null) {
747
        final WrapParentData childParentData = child.parentData! as WrapParentData;
748
        if (childParentData._runIndex != i) {
Adam Barth's avatar
Adam Barth committed
749
          break;
750
        }
751 752
        final double childMainAxisExtent = _getMainAxisExtent(child.size);
        final double childCrossAxisExtent = _getCrossAxisExtent(child.size);
753
        final double childCrossAxisOffset = _getChildCrossAxisOffset(flipCrossAxis, runCrossAxisExtent, childCrossAxisExtent);
754
        if (flipMainAxis) {
755
          childMainPosition -= childMainAxisExtent;
756
        }
757
        childParentData.offset = _getOffset(childMainPosition, crossAxisOffset + childCrossAxisOffset);
758
        if (flipMainAxis) {
759
          childMainPosition -= childBetweenSpace;
760
        } else {
761
          childMainPosition += childMainAxisExtent + childBetweenSpace;
762
        }
Adam Barth's avatar
Adam Barth committed
763 764 765
        child = childParentData.nextSibling;
      }

766
      if (flipCrossAxis) {
767
        crossAxisOffset -= runBetweenSpace;
768
      } else {
769
        crossAxisOffset += runCrossAxisExtent + runBetweenSpace;
770
      }
Adam Barth's avatar
Adam Barth committed
771 772 773 774
    }
  }

  @override
775
  bool hitTestChildren(BoxHitTestResult result, { required Offset position }) {
Adam Barth's avatar
Adam Barth committed
776 777 778 779 780 781 782
    return defaultHitTestChildren(result, position: position);
  }

  @override
  void paint(PaintingContext context, Offset offset) {
    // TODO(ianh): move the debug flex overflow paint logic somewhere common so
    // it can be reused here
783
    if (_hasVisualOverflow && clipBehavior != Clip.none) {
784
      _clipRectLayer.layer = context.pushClipRect(
785 786 787 788 789
        needsCompositing,
        offset,
        Offset.zero & size,
        defaultPaint,
        clipBehavior: clipBehavior,
790
        oldLayer: _clipRectLayer.layer,
791
      );
792
    } else {
793
      _clipRectLayer.layer = null;
Adam Barth's avatar
Adam Barth committed
794
      defaultPaint(context, offset);
795
    }
Adam Barth's avatar
Adam Barth committed
796 797
  }

798 799 800 801 802 803 804
  final LayerHandle<ClipRectLayer> _clipRectLayer = LayerHandle<ClipRectLayer>();

  @override
  void dispose() {
    _clipRectLayer.layer = null;
    super.dispose();
  }
805

Adam Barth's avatar
Adam Barth committed
806
  @override
807 808
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
809 810 811 812 813 814 815 816
    properties.add(EnumProperty<Axis>('direction', direction));
    properties.add(EnumProperty<WrapAlignment>('alignment', alignment));
    properties.add(DoubleProperty('spacing', spacing));
    properties.add(EnumProperty<WrapAlignment>('runAlignment', runAlignment));
    properties.add(DoubleProperty('runSpacing', runSpacing));
    properties.add(DoubleProperty('crossAxisAlignment', runSpacing));
    properties.add(EnumProperty<TextDirection>('textDirection', textDirection, defaultValue: null));
    properties.add(EnumProperty<VerticalDirection>('verticalDirection', verticalDirection, defaultValue: VerticalDirection.down));
Adam Barth's avatar
Adam Barth committed
817 818
  }
}