1 | // Copyright 2014 The Flutter Authors. All rights reserved. |
2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. |
4 | |
5 | import 'dart:math' as math; |
6 | import 'dart:ui' show lerpDouble; |
7 | |
8 | import 'package:flutter/foundation.dart'; |
9 | |
10 | import 'box.dart'; |
11 | import 'layer.dart'; |
12 | import 'layout_helper.dart'; |
13 | import 'object.dart'; |
14 | |
15 | /// An immutable 2D, axis-aligned, floating-point rectangle whose coordinates |
16 | /// are given relative to another rectangle's edges, known as the container. |
17 | /// Since the dimensions of the rectangle are relative to those of the |
18 | /// container, this class has no width and height members. To determine the |
19 | /// width or height of the rectangle, convert it to a [Rect] using [toRect()] |
20 | /// (passing the container's own Rect), and then examine that object. |
21 | @immutable |
22 | class RelativeRect { |
23 | /// Creates a RelativeRect with the given values. |
24 | const RelativeRect.fromLTRB(this.left, this.top, this.right, this.bottom); |
25 | |
26 | /// Creates a RelativeRect from a Rect and a Size. The Rect (first argument) |
27 | /// and the RelativeRect (the output) are in the coordinate space of the |
28 | /// rectangle described by the Size, with 0,0 being at the top left. |
29 | factory RelativeRect.fromSize(Rect rect, Size container) { |
30 | return RelativeRect.fromLTRB(rect.left, rect.top, container.width - rect.right, container.height - rect.bottom); |
31 | } |
32 | |
33 | /// Creates a RelativeRect from two Rects. The second Rect provides the |
34 | /// container, the first provides the rectangle, in the same coordinate space, |
35 | /// that is to be converted to a RelativeRect. The output will be in the |
36 | /// container's coordinate space. |
37 | /// |
38 | /// For example, if the top left of the rect is at 0,0, and the top left of |
39 | /// the container is at 100,100, then the top left of the output will be at |
40 | /// -100,-100. |
41 | /// |
42 | /// If the first rect is actually in the container's coordinate space, then |
43 | /// use [RelativeRect.fromSize] and pass the container's size as the second |
44 | /// argument instead. |
45 | factory RelativeRect.fromRect(Rect rect, Rect container) { |
46 | return RelativeRect.fromLTRB( |
47 | rect.left - container.left, |
48 | rect.top - container.top, |
49 | container.right - rect.right, |
50 | container.bottom - rect.bottom, |
51 | ); |
52 | } |
53 | |
54 | /// Creates a RelativeRect from horizontal position using `start` and `end` |
55 | /// rather than `left` and `right`. |
56 | /// |
57 | /// If `textDirection` is [TextDirection.rtl], then the `start` argument is |
58 | /// used for the [right] property and the `end` argument is used for the |
59 | /// [left] property. Otherwise, if `textDirection` is [TextDirection.ltr], |
60 | /// then the `start` argument is used for the [left] property and the `end` |
61 | /// argument is used for the [right] property. |
62 | factory RelativeRect.fromDirectional({ |
63 | required TextDirection textDirection, |
64 | required double start, |
65 | required double top, |
66 | required double end, |
67 | required double bottom, |
68 | }) { |
69 | double left; |
70 | double right; |
71 | switch (textDirection) { |
72 | case TextDirection.rtl: |
73 | left = end; |
74 | right = start; |
75 | case TextDirection.ltr: |
76 | left = start; |
77 | right = end; |
78 | } |
79 | |
80 | return RelativeRect.fromLTRB(left, top, right, bottom); |
81 | } |
82 | |
83 | /// A rect that covers the entire container. |
84 | static const RelativeRect fill = RelativeRect.fromLTRB(0.0, 0.0, 0.0, 0.0); |
85 | |
86 | /// Distance from the left side of the container to the left side of this rectangle. |
87 | /// |
88 | /// May be negative if the left side of the rectangle is outside of the container. |
89 | final double left; |
90 | |
91 | /// Distance from the top side of the container to the top side of this rectangle. |
92 | /// |
93 | /// May be negative if the top side of the rectangle is outside of the container. |
94 | final double top; |
95 | |
96 | /// Distance from the right side of the container to the right side of this rectangle. |
97 | /// |
98 | /// May be positive if the right side of the rectangle is outside of the container. |
99 | final double right; |
100 | |
101 | /// Distance from the bottom side of the container to the bottom side of this rectangle. |
102 | /// |
103 | /// May be positive if the bottom side of the rectangle is outside of the container. |
104 | final double bottom; |
105 | |
106 | /// Returns whether any of the values are greater than zero. |
107 | /// |
108 | /// This corresponds to one of the sides ([left], [top], [right], or [bottom]) having |
109 | /// some positive inset towards the center. |
110 | bool get hasInsets => left > 0.0 || top > 0.0 || right > 0.0 || bottom > 0.0; |
111 | |
112 | /// Returns a new rectangle object translated by the given offset. |
113 | RelativeRect shift(Offset offset) { |
114 | return RelativeRect.fromLTRB(left + offset.dx, top + offset.dy, right - offset.dx, bottom - offset.dy); |
115 | } |
116 | |
117 | /// Returns a new rectangle with edges moved outwards by the given delta. |
118 | RelativeRect inflate(double delta) { |
119 | return RelativeRect.fromLTRB(left - delta, top - delta, right - delta, bottom - delta); |
120 | } |
121 | |
122 | /// Returns a new rectangle with edges moved inwards by the given delta. |
123 | RelativeRect deflate(double delta) { |
124 | return inflate(-delta); |
125 | } |
126 | |
127 | /// Returns a new rectangle that is the intersection of the given rectangle and this rectangle. |
128 | RelativeRect intersect(RelativeRect other) { |
129 | return RelativeRect.fromLTRB( |
130 | math.max(left, other.left), |
131 | math.max(top, other.top), |
132 | math.max(right, other.right), |
133 | math.max(bottom, other.bottom), |
134 | ); |
135 | } |
136 | |
137 | /// Convert this [RelativeRect] to a [Rect], in the coordinate space of the container. |
138 | /// |
139 | /// See also: |
140 | /// |
141 | /// * [toSize], which returns the size part of the rect, based on the size of |
142 | /// the container. |
143 | Rect toRect(Rect container) { |
144 | return Rect.fromLTRB(left, top, container.width - right, container.height - bottom); |
145 | } |
146 | |
147 | /// Convert this [RelativeRect] to a [Size], assuming a container with the given size. |
148 | /// |
149 | /// See also: |
150 | /// |
151 | /// * [toRect], which also computes the position relative to the container. |
152 | Size toSize(Size container) { |
153 | return Size(container.width - left - right, container.height - top - bottom); |
154 | } |
155 | |
156 | /// Linearly interpolate between two RelativeRects. |
157 | /// |
158 | /// If either rect is null, this function interpolates from [RelativeRect.fill]. |
159 | /// |
160 | /// {@macro dart.ui.shadow.lerp} |
161 | static RelativeRect? lerp(RelativeRect? a, RelativeRect? b, double t) { |
162 | if (identical(a, b)) { |
163 | return a; |
164 | } |
165 | if (a == null) { |
166 | return RelativeRect.fromLTRB(b!.left * t, b.top * t, b.right * t, b.bottom * t); |
167 | } |
168 | if (b == null) { |
169 | final double k = 1.0 - t; |
170 | return RelativeRect.fromLTRB(b!.left * k, b.top * k, b.right * k, b.bottom * k); |
171 | } |
172 | return RelativeRect.fromLTRB( |
173 | lerpDouble(a.left, b.left, t)!, |
174 | lerpDouble(a.top, b.top, t)!, |
175 | lerpDouble(a.right, b.right, t)!, |
176 | lerpDouble(a.bottom, b.bottom, t)!, |
177 | ); |
178 | } |
179 | |
180 | @override |
181 | bool operator ==(Object other) { |
182 | if (identical(this, other)) { |
183 | return true; |
184 | } |
185 | return other is RelativeRect |
186 | && other.left == left |
187 | && other.top == top |
188 | && other.right == right |
189 | && other.bottom == bottom; |
190 | } |
191 | |
192 | @override |
193 | int get hashCode => Object.hash(left, top, right, bottom); |
194 | |
195 | @override |
196 | String toString() => 'RelativeRect.fromLTRB( ${left.toStringAsFixed(1)}, ${top.toStringAsFixed(1)}, ${right.toStringAsFixed(1)}, ${bottom.toStringAsFixed(1)})' ; |
197 | } |
198 | |
199 | /// Parent data for use with [RenderStack]. |
200 | class StackParentData extends ContainerBoxParentData<RenderBox> { |
201 | /// The distance by which the child's top edge is inset from the top of the stack. |
202 | double? top; |
203 | |
204 | /// The distance by which the child's right edge is inset from the right of the stack. |
205 | double? right; |
206 | |
207 | /// The distance by which the child's bottom edge is inset from the bottom of the stack. |
208 | double? bottom; |
209 | |
210 | /// The distance by which the child's left edge is inset from the left of the stack. |
211 | double? left; |
212 | |
213 | /// The child's width. |
214 | /// |
215 | /// Ignored if both left and right are non-null. |
216 | double? width; |
217 | |
218 | /// The child's height. |
219 | /// |
220 | /// Ignored if both top and bottom are non-null. |
221 | double? height; |
222 | |
223 | /// Get or set the current values in terms of a RelativeRect object. |
224 | RelativeRect get rect => RelativeRect.fromLTRB(left!, top!, right!, bottom!); |
225 | set rect(RelativeRect value) { |
226 | top = value.top; |
227 | right = value.right; |
228 | bottom = value.bottom; |
229 | left = value.left; |
230 | } |
231 | |
232 | /// Whether this child is considered positioned. |
233 | /// |
234 | /// A child is positioned if any of the top, right, bottom, or left properties |
235 | /// are non-null. Positioned children do not factor into determining the size |
236 | /// of the stack but are instead placed relative to the non-positioned |
237 | /// children in the stack. |
238 | bool get isPositioned => top != null || right != null || bottom != null || left != null || width != null || height != null; |
239 | |
240 | @override |
241 | String toString() { |
242 | final List<String> values = <String>[ |
243 | if (top != null) 'top= ${debugFormatDouble(top)}' , |
244 | if (right != null) 'right= ${debugFormatDouble(right)}' , |
245 | if (bottom != null) 'bottom= ${debugFormatDouble(bottom)}' , |
246 | if (left != null) 'left= ${debugFormatDouble(left)}' , |
247 | if (width != null) 'width= ${debugFormatDouble(width)}' , |
248 | if (height != null) 'height= ${debugFormatDouble(height)}' , |
249 | ]; |
250 | if (values.isEmpty) { |
251 | values.add('not positioned' ); |
252 | } |
253 | values.add(super.toString()); |
254 | return values.join('; ' ); |
255 | } |
256 | } |
257 | |
258 | /// How to size the non-positioned children of a [Stack]. |
259 | /// |
260 | /// This enum is used with [Stack.fit] and [RenderStack.fit] to control |
261 | /// how the [BoxConstraints] passed from the stack's parent to the stack's child |
262 | /// are adjusted. |
263 | /// |
264 | /// See also: |
265 | /// |
266 | /// * [Stack], the widget that uses this. |
267 | /// * [RenderStack], the render object that implements the stack algorithm. |
268 | enum StackFit { |
269 | /// The constraints passed to the stack from its parent are loosened. |
270 | /// |
271 | /// For example, if the stack has constraints that force it to 350x600, then |
272 | /// this would allow the non-positioned children of the stack to have any |
273 | /// width from zero to 350 and any height from zero to 600. |
274 | /// |
275 | /// See also: |
276 | /// |
277 | /// * [Center], which loosens the constraints passed to its child and then |
278 | /// centers the child in itself. |
279 | /// * [BoxConstraints.loosen], which implements the loosening of box |
280 | /// constraints. |
281 | loose, |
282 | |
283 | /// The constraints passed to the stack from its parent are tightened to the |
284 | /// biggest size allowed. |
285 | /// |
286 | /// For example, if the stack has loose constraints with a width in the range |
287 | /// 10 to 100 and a height in the range 0 to 600, then the non-positioned |
288 | /// children of the stack would all be sized as 100 pixels wide and 600 high. |
289 | expand, |
290 | |
291 | /// The constraints passed to the stack from its parent are passed unmodified |
292 | /// to the non-positioned children. |
293 | /// |
294 | /// For example, if a [Stack] is an [Expanded] child of a [Row], the |
295 | /// horizontal constraints will be tight and the vertical constraints will be |
296 | /// loose. |
297 | passthrough, |
298 | } |
299 | |
300 | /// Implements the stack layout algorithm. |
301 | /// |
302 | /// In a stack layout, the children are positioned on top of each other in the |
303 | /// order in which they appear in the child list. First, the non-positioned |
304 | /// children (those with null values for top, right, bottom, and left) are |
305 | /// laid out and initially placed in the upper-left corner of the stack. The |
306 | /// stack is then sized to enclose all of the non-positioned children. If there |
307 | /// are no non-positioned children, the stack becomes as large as possible. |
308 | /// |
309 | /// The final location of non-positioned children is determined by the alignment |
310 | /// parameter. The left of each non-positioned child becomes the |
311 | /// difference between the child's width and the stack's width scaled by |
312 | /// alignment.x. The top of each non-positioned child is computed |
313 | /// similarly and scaled by alignment.y. So if the alignment x and y properties |
314 | /// are 0.0 (the default) then the non-positioned children remain in the |
315 | /// upper-left corner. If the alignment x and y properties are 0.5 then the |
316 | /// non-positioned children are centered within the stack. |
317 | /// |
318 | /// Next, the positioned children are laid out. If a child has top and bottom |
319 | /// values that are both non-null, the child is given a fixed height determined |
320 | /// by subtracting the sum of the top and bottom values from the height of the stack. |
321 | /// Similarly, if the child has right and left values that are both non-null, |
322 | /// the child is given a fixed width derived from the stack's width. |
323 | /// Otherwise, the child is given unbounded constraints in the non-fixed dimensions. |
324 | /// |
325 | /// Once the child is laid out, the stack positions the child |
326 | /// according to the top, right, bottom, and left properties of their |
327 | /// [StackParentData]. For example, if the bottom value is 10.0, the |
328 | /// bottom edge of the child will be inset 10.0 pixels from the bottom |
329 | /// edge of the stack. If the child extends beyond the bounds of the |
330 | /// stack, the stack will clip the child's painting to the bounds of |
331 | /// the stack. |
332 | /// |
333 | /// See also: |
334 | /// |
335 | /// * [RenderFlow] |
336 | class RenderStack extends RenderBox |
337 | with ContainerRenderObjectMixin<RenderBox, StackParentData>, |
338 | RenderBoxContainerDefaultsMixin<RenderBox, StackParentData> { |
339 | /// Creates a stack render object. |
340 | /// |
341 | /// By default, the non-positioned children of the stack are aligned by their |
342 | /// top left corners. |
343 | RenderStack({ |
344 | List<RenderBox>? children, |
345 | AlignmentGeometry alignment = AlignmentDirectional.topStart, |
346 | TextDirection? textDirection, |
347 | StackFit fit = StackFit.loose, |
348 | Clip clipBehavior = Clip.hardEdge, |
349 | }) : _alignment = alignment, |
350 | _textDirection = textDirection, |
351 | _fit = fit, |
352 | _clipBehavior = clipBehavior { |
353 | addAll(children); |
354 | } |
355 | |
356 | bool _hasVisualOverflow = false; |
357 | |
358 | @override |
359 | void setupParentData(RenderBox child) { |
360 | if (child.parentData is! StackParentData) { |
361 | child.parentData = StackParentData(); |
362 | } |
363 | } |
364 | |
365 | Alignment? _resolvedAlignment; |
366 | |
367 | void _resolve() { |
368 | if (_resolvedAlignment != null) { |
369 | return; |
370 | } |
371 | _resolvedAlignment = alignment.resolve(textDirection); |
372 | } |
373 | |
374 | void _markNeedResolution() { |
375 | _resolvedAlignment = null; |
376 | markNeedsLayout(); |
377 | } |
378 | |
379 | /// How to align the non-positioned or partially-positioned children in the |
380 | /// stack. |
381 | /// |
382 | /// The non-positioned children are placed relative to each other such that |
383 | /// the points determined by [alignment] are co-located. For example, if the |
384 | /// [alignment] is [Alignment.topLeft], then the top left corner of |
385 | /// each non-positioned child will be located at the same global coordinate. |
386 | /// |
387 | /// Partially-positioned children, those that do not specify an alignment in a |
388 | /// particular axis (e.g. that have neither `top` nor `bottom` set), use the |
389 | /// alignment to determine how they should be positioned in that |
390 | /// under-specified axis. |
391 | /// |
392 | /// If this is set to an [AlignmentDirectional] object, then [textDirection] |
393 | /// must not be null. |
394 | AlignmentGeometry get alignment => _alignment; |
395 | AlignmentGeometry _alignment; |
396 | set alignment(AlignmentGeometry value) { |
397 | if (_alignment == value) { |
398 | return; |
399 | } |
400 | _alignment = value; |
401 | _markNeedResolution(); |
402 | } |
403 | |
404 | /// The text direction with which to resolve [alignment]. |
405 | /// |
406 | /// This may be changed to null, but only after the [alignment] has been changed |
407 | /// to a value that does not depend on the direction. |
408 | TextDirection? get textDirection => _textDirection; |
409 | TextDirection? _textDirection; |
410 | set textDirection(TextDirection? value) { |
411 | if (_textDirection == value) { |
412 | return; |
413 | } |
414 | _textDirection = value; |
415 | _markNeedResolution(); |
416 | } |
417 | |
418 | /// How to size the non-positioned children in the stack. |
419 | /// |
420 | /// The constraints passed into the [RenderStack] from its parent are either |
421 | /// loosened ([StackFit.loose]) or tightened to their biggest size |
422 | /// ([StackFit.expand]). |
423 | StackFit get fit => _fit; |
424 | StackFit _fit; |
425 | set fit(StackFit value) { |
426 | if (_fit != value) { |
427 | _fit = value; |
428 | markNeedsLayout(); |
429 | } |
430 | } |
431 | |
432 | /// {@macro flutter.material.Material.clipBehavior} |
433 | /// |
434 | /// Stacks only clip children whose geometry overflow the stack. A child that |
435 | /// paints outside its bounds (e.g. a box with a shadow) will not be clipped, |
436 | /// regardless of the value of this property. Similarly, a child that itself |
437 | /// has a descendant that overflows the stack will not be clipped, as only the |
438 | /// geometry of the stack's direct children are considered. |
439 | /// |
440 | /// To clip children whose geometry does not overflow the stack, consider |
441 | /// using a [RenderClipRect] render object. |
442 | /// |
443 | /// Defaults to [Clip.hardEdge]. |
444 | Clip get clipBehavior => _clipBehavior; |
445 | Clip _clipBehavior = Clip.hardEdge; |
446 | set clipBehavior(Clip value) { |
447 | if (value != _clipBehavior) { |
448 | _clipBehavior = value; |
449 | markNeedsPaint(); |
450 | markNeedsSemanticsUpdate(); |
451 | } |
452 | } |
453 | |
454 | /// Helper function for calculating the intrinsics metrics of a Stack. |
455 | static double getIntrinsicDimension(RenderBox? firstChild, double Function(RenderBox child) mainChildSizeGetter) { |
456 | double extent = 0.0; |
457 | RenderBox? child = firstChild; |
458 | while (child != null) { |
459 | final StackParentData childParentData = child.parentData! as StackParentData; |
460 | if (!childParentData.isPositioned) { |
461 | extent = math.max(extent, mainChildSizeGetter(child)); |
462 | } |
463 | assert(child.parentData == childParentData); |
464 | child = childParentData.nextSibling; |
465 | } |
466 | return extent; |
467 | } |
468 | |
469 | @override |
470 | double computeMinIntrinsicWidth(double height) { |
471 | return getIntrinsicDimension(firstChild, (RenderBox child) => child.getMinIntrinsicWidth(height)); |
472 | } |
473 | |
474 | @override |
475 | double computeMaxIntrinsicWidth(double height) { |
476 | return getIntrinsicDimension(firstChild, (RenderBox child) => child.getMaxIntrinsicWidth(height)); |
477 | } |
478 | |
479 | @override |
480 | double computeMinIntrinsicHeight(double width) { |
481 | return getIntrinsicDimension(firstChild, (RenderBox child) => child.getMinIntrinsicHeight(width)); |
482 | } |
483 | |
484 | @override |
485 | double computeMaxIntrinsicHeight(double width) { |
486 | return getIntrinsicDimension(firstChild, (RenderBox child) => child.getMaxIntrinsicHeight(width)); |
487 | } |
488 | |
489 | @override |
490 | double? computeDistanceToActualBaseline(TextBaseline baseline) { |
491 | return defaultComputeDistanceToHighestActualBaseline(baseline); |
492 | } |
493 | |
494 | /// Lays out the positioned `child` according to `alignment` within a Stack of `size`. |
495 | /// |
496 | /// Returns true when the child has visual overflow. |
497 | static bool layoutPositionedChild(RenderBox child, StackParentData childParentData, Size size, Alignment alignment) { |
498 | assert(childParentData.isPositioned); |
499 | assert(child.parentData == childParentData); |
500 | |
501 | bool hasVisualOverflow = false; |
502 | BoxConstraints childConstraints = const BoxConstraints(); |
503 | |
504 | if (childParentData.left != null && childParentData.right != null) { |
505 | childConstraints = childConstraints.tighten(width: size.width - childParentData.right! - childParentData.left!); |
506 | } else if (childParentData.width != null) { |
507 | childConstraints = childConstraints.tighten(width: childParentData.width); |
508 | } |
509 | |
510 | if (childParentData.top != null && childParentData.bottom != null) { |
511 | childConstraints = childConstraints.tighten(height: size.height - childParentData.bottom! - childParentData.top!); |
512 | } else if (childParentData.height != null) { |
513 | childConstraints = childConstraints.tighten(height: childParentData.height); |
514 | } |
515 | |
516 | child.layout(childConstraints, parentUsesSize: true); |
517 | |
518 | final double x; |
519 | if (childParentData.left != null) { |
520 | x = childParentData.left!; |
521 | } else if (childParentData.right != null) { |
522 | x = size.width - childParentData.right! - child.size.width; |
523 | } else { |
524 | x = alignment.alongOffset(size - child.size as Offset).dx; |
525 | } |
526 | |
527 | if (x < 0.0 || x + child.size.width > size.width) { |
528 | hasVisualOverflow = true; |
529 | } |
530 | |
531 | final double y; |
532 | if (childParentData.top != null) { |
533 | y = childParentData.top!; |
534 | } else if (childParentData.bottom != null) { |
535 | y = size.height - childParentData.bottom! - child.size.height; |
536 | } else { |
537 | y = alignment.alongOffset(size - child.size as Offset).dy; |
538 | } |
539 | |
540 | if (y < 0.0 || y + child.size.height > size.height) { |
541 | hasVisualOverflow = true; |
542 | } |
543 | |
544 | childParentData.offset = Offset(x, y); |
545 | |
546 | return hasVisualOverflow; |
547 | } |
548 | |
549 | @override |
550 | @protected |
551 | Size computeDryLayout(covariant BoxConstraints constraints) { |
552 | return _computeSize( |
553 | constraints: constraints, |
554 | layoutChild: ChildLayoutHelper.dryLayoutChild, |
555 | ); |
556 | } |
557 | |
558 | Size _computeSize({required BoxConstraints constraints, required ChildLayouter layoutChild}) { |
559 | _resolve(); |
560 | assert(_resolvedAlignment != null); |
561 | bool hasNonPositionedChildren = false; |
562 | if (childCount == 0) { |
563 | return (constraints.biggest.isFinite) ? constraints.biggest : constraints.smallest; |
564 | } |
565 | |
566 | double width = constraints.minWidth; |
567 | double height = constraints.minHeight; |
568 | |
569 | final BoxConstraints nonPositionedConstraints = switch (fit) { |
570 | StackFit.loose => constraints.loosen(), |
571 | StackFit.expand => BoxConstraints.tight(constraints.biggest), |
572 | StackFit.passthrough => constraints, |
573 | }; |
574 | |
575 | RenderBox? child = firstChild; |
576 | while (child != null) { |
577 | final StackParentData childParentData = child.parentData! as StackParentData; |
578 | |
579 | if (!childParentData.isPositioned) { |
580 | hasNonPositionedChildren = true; |
581 | |
582 | final Size childSize = layoutChild(child, nonPositionedConstraints); |
583 | |
584 | width = math.max(width, childSize.width); |
585 | height = math.max(height, childSize.height); |
586 | } |
587 | |
588 | child = childParentData.nextSibling; |
589 | } |
590 | |
591 | final Size size; |
592 | if (hasNonPositionedChildren) { |
593 | size = Size(width, height); |
594 | assert(size.width == constraints.constrainWidth(width)); |
595 | assert(size.height == constraints.constrainHeight(height)); |
596 | } else { |
597 | size = constraints.biggest; |
598 | } |
599 | |
600 | assert(size.isFinite); |
601 | return size; |
602 | } |
603 | |
604 | @override |
605 | void performLayout() { |
606 | final BoxConstraints constraints = this.constraints; |
607 | _hasVisualOverflow = false; |
608 | |
609 | size = _computeSize( |
610 | constraints: constraints, |
611 | layoutChild: ChildLayoutHelper.layoutChild, |
612 | ); |
613 | |
614 | assert(_resolvedAlignment != null); |
615 | RenderBox? child = firstChild; |
616 | while (child != null) { |
617 | final StackParentData childParentData = child.parentData! as StackParentData; |
618 | |
619 | if (!childParentData.isPositioned) { |
620 | childParentData.offset = _resolvedAlignment!.alongOffset(size - child.size as Offset); |
621 | } else { |
622 | _hasVisualOverflow = layoutPositionedChild(child, childParentData, size, _resolvedAlignment!) || _hasVisualOverflow; |
623 | } |
624 | |
625 | assert(child.parentData == childParentData); |
626 | child = childParentData.nextSibling; |
627 | } |
628 | } |
629 | |
630 | @override |
631 | bool hitTestChildren(BoxHitTestResult result, { required Offset position }) { |
632 | return defaultHitTestChildren(result, position: position); |
633 | } |
634 | |
635 | /// Override in subclasses to customize how the stack paints. |
636 | /// |
637 | /// By default, the stack uses [defaultPaint]. This function is called by |
638 | /// [paint] after potentially applying a clip to contain visual overflow. |
639 | @protected |
640 | void paintStack(PaintingContext context, Offset offset) { |
641 | defaultPaint(context, offset); |
642 | } |
643 | |
644 | @override |
645 | void paint(PaintingContext context, Offset offset) { |
646 | if (clipBehavior != Clip.none && _hasVisualOverflow) { |
647 | _clipRectLayer.layer = context.pushClipRect( |
648 | needsCompositing, |
649 | offset, |
650 | Offset.zero & size, |
651 | paintStack, |
652 | clipBehavior: clipBehavior, |
653 | oldLayer: _clipRectLayer.layer, |
654 | ); |
655 | } else { |
656 | _clipRectLayer.layer = null; |
657 | paintStack(context, offset); |
658 | } |
659 | } |
660 | |
661 | final LayerHandle<ClipRectLayer> _clipRectLayer = LayerHandle<ClipRectLayer>(); |
662 | |
663 | @override |
664 | void dispose() { |
665 | _clipRectLayer.layer = null; |
666 | super.dispose(); |
667 | } |
668 | |
669 | @override |
670 | Rect? describeApproximatePaintClip(RenderObject child) { |
671 | switch (clipBehavior) { |
672 | case Clip.none: |
673 | return null; |
674 | case Clip.hardEdge: |
675 | case Clip.antiAlias: |
676 | case Clip.antiAliasWithSaveLayer: |
677 | return _hasVisualOverflow ? Offset.zero & size : null; |
678 | } |
679 | } |
680 | |
681 | @override |
682 | void debugFillProperties(DiagnosticPropertiesBuilder properties) { |
683 | super.debugFillProperties(properties); |
684 | properties.add(DiagnosticsProperty<AlignmentGeometry>('alignment' , alignment)); |
685 | properties.add(EnumProperty<TextDirection>('textDirection' , textDirection)); |
686 | properties.add(EnumProperty<StackFit>('fit' , fit)); |
687 | properties.add(EnumProperty<Clip>('clipBehavior' , clipBehavior, defaultValue: Clip.hardEdge)); |
688 | } |
689 | } |
690 | |
691 | /// Implements the same layout algorithm as RenderStack but only paints the child |
692 | /// specified by index. |
693 | /// |
694 | /// Although only one child is displayed, the cost of the layout algorithm is |
695 | /// still O(N), like an ordinary stack. |
696 | class RenderIndexedStack extends RenderStack { |
697 | /// Creates a stack render object that paints a single child. |
698 | /// |
699 | /// If the [index] parameter is null, nothing is displayed. |
700 | RenderIndexedStack({ |
701 | super.children, |
702 | super.alignment, |
703 | super.textDirection, |
704 | super.fit, |
705 | super.clipBehavior, |
706 | int? index = 0, |
707 | }) : _index = index; |
708 | |
709 | @override |
710 | void visitChildrenForSemantics(RenderObjectVisitor visitor) { |
711 | if (index != null && firstChild != null) { |
712 | visitor(_childAtIndex()); |
713 | } |
714 | } |
715 | |
716 | /// The index of the child to show, null if nothing is to be displayed. |
717 | int? get index => _index; |
718 | int? _index; |
719 | set index(int? value) { |
720 | if (_index != value) { |
721 | _index = value; |
722 | markNeedsLayout(); |
723 | } |
724 | } |
725 | |
726 | RenderBox _childAtIndex() { |
727 | assert(index != null); |
728 | RenderBox? child = firstChild; |
729 | int i = 0; |
730 | while (child != null && i < index!) { |
731 | final StackParentData childParentData = child.parentData! as StackParentData; |
732 | child = childParentData.nextSibling; |
733 | i += 1; |
734 | } |
735 | assert(i == index); |
736 | assert(child != null); |
737 | return child!; |
738 | } |
739 | |
740 | @override |
741 | bool hitTestChildren(BoxHitTestResult result, { required Offset position }) { |
742 | if (firstChild == null || index == null) { |
743 | return false; |
744 | } |
745 | final RenderBox child = _childAtIndex(); |
746 | final StackParentData childParentData = child.parentData! as StackParentData; |
747 | return result.addWithPaintOffset( |
748 | offset: childParentData.offset, |
749 | position: position, |
750 | hitTest: (BoxHitTestResult result, Offset transformed) { |
751 | assert(transformed == position - childParentData.offset); |
752 | return child.hitTest(result, position: transformed); |
753 | }, |
754 | ); |
755 | } |
756 | |
757 | @override |
758 | void paintStack(PaintingContext context, Offset offset) { |
759 | if (firstChild == null || index == null) { |
760 | return; |
761 | } |
762 | final RenderBox child = _childAtIndex(); |
763 | final StackParentData childParentData = child.parentData! as StackParentData; |
764 | context.paintChild(child, childParentData.offset + offset); |
765 | } |
766 | |
767 | @override |
768 | void debugFillProperties(DiagnosticPropertiesBuilder properties) { |
769 | super.debugFillProperties(properties); |
770 | properties.add(IntProperty('index' , index)); |
771 | } |
772 | |
773 | @override |
774 | List<DiagnosticsNode> debugDescribeChildren() { |
775 | final List<DiagnosticsNode> children = <DiagnosticsNode>[]; |
776 | int i = 0; |
777 | RenderObject? child = firstChild; |
778 | while (child != null) { |
779 | children.add(child.toDiagnosticsNode( |
780 | name: 'child ${i + 1}' , |
781 | style: i != index ? DiagnosticsTreeStyle.offstage : null, |
782 | )); |
783 | child = (child.parentData! as StackParentData).nextSibling; |
784 | i += 1; |
785 | } |
786 | return children; |
787 | } |
788 | } |
789 | |