|
5 | 5 | /// @docImport 'grid_tile_bar.dart'; |
6 | 6 | library; |
7 | 7 |
|
| 8 | +import 'package:flutter/rendering.dart'; |
8 | 9 | import 'package:flutter/widgets.dart'; |
9 | 10 |
|
10 | 11 | /// A tile in a Material Design grid list. |
@@ -46,17 +47,69 @@ class GridTile extends StatelessWidget { |
46 | 47 | return child; |
47 | 48 | } |
48 | 49 |
|
49 | | - // The tile sizes itself to its child so that it can be used where the |
50 | | - // incoming constraints are unbounded, e.g. in a horizontally scrolling |
51 | | - // ListView. A Stack whose children are all positioned would try to expand |
52 | | - // to the biggest allowed size, which is infinite in that case. |
| 50 | + // The child is not positioned so that the Stack can size itself to it |
| 51 | + // along an unbounded axis (e.g. in a horizontally scrolling ListView), |
| 52 | + // where a Stack of only positioned children would try to be infinitely |
| 53 | + // large. Along bounded axes, the child still fills the available space. |
53 | 54 | return Stack( |
54 | 55 | fit: StackFit.passthrough, |
55 | 56 | children: <Widget>[ |
56 | | - child, |
| 57 | + _FillBoundedAxes(child: child), |
57 | 58 | if (header != null) Positioned(top: 0.0, left: 0.0, right: 0.0, child: header!), |
58 | 59 | if (footer != null) Positioned(left: 0.0, bottom: 0.0, right: 0.0, child: footer!), |
59 | 60 | ], |
60 | 61 | ); |
61 | 62 | } |
62 | 63 | } |
| 64 | + |
| 65 | +/// Makes its child as big as the incoming constraints allow along every |
| 66 | +/// bounded axis, and lets the child pick its own size along unbounded axes. |
| 67 | +class _FillBoundedAxes extends SingleChildRenderObjectWidget { |
| 68 | + const _FillBoundedAxes({required Widget super.child}); |
| 69 | + |
| 70 | + @override |
| 71 | + _RenderFillBoundedAxes createRenderObject(BuildContext context) => _RenderFillBoundedAxes(); |
| 72 | +} |
| 73 | + |
| 74 | +class _RenderFillBoundedAxes extends RenderProxyBox { |
| 75 | + static BoxConstraints _fillBoundedAxes(BoxConstraints constraints) { |
| 76 | + return constraints.copyWith( |
| 77 | + minWidth: constraints.hasBoundedWidth ? constraints.maxWidth : constraints.minWidth, |
| 78 | + minHeight: constraints.hasBoundedHeight ? constraints.maxHeight : constraints.minHeight, |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + // The tile used to report zero intrinsic sizes because its child was |
| 83 | + // positioned, which the Stack ignores for intrinsics. Keep doing so. |
| 84 | + @override |
| 85 | + double computeMinIntrinsicWidth(double height) => 0.0; |
| 86 | + |
| 87 | + @override |
| 88 | + double computeMaxIntrinsicWidth(double height) => 0.0; |
| 89 | + |
| 90 | + @override |
| 91 | + double computeMinIntrinsicHeight(double width) => 0.0; |
| 92 | + |
| 93 | + @override |
| 94 | + double computeMaxIntrinsicHeight(double width) => 0.0; |
| 95 | + |
| 96 | + @override |
| 97 | + Size computeDryLayout(BoxConstraints constraints) { |
| 98 | + final BoxConstraints childConstraints = _fillBoundedAxes(constraints); |
| 99 | + return constraints.constrain( |
| 100 | + child?.getDryLayout(childConstraints) ?? childConstraints.smallest, |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + @override |
| 105 | + void performLayout() { |
| 106 | + final BoxConstraints childConstraints = _fillBoundedAxes(constraints); |
| 107 | + final RenderBox? child = this.child; |
| 108 | + if (child == null) { |
| 109 | + size = constraints.constrain(childConstraints.smallest); |
| 110 | + return; |
| 111 | + } |
| 112 | + child.layout(childConstraints, parentUsesSize: true); |
| 113 | + size = constraints.constrain(child.size); |
| 114 | + } |
| 115 | +} |
0 commit comments