Skip to content

Commit 68b5150

Browse files
committed
Keep GridTile layout for bounded loose constraints
Only let the tile shrink-wrap its child along unbounded axes, which is the case that previously crashed. Along bounded axes the child keeps filling the available space, and the tile keeps reporting the same intrinsic sizes as before.
1 parent d4b1142 commit 68b5150

2 files changed

Lines changed: 82 additions & 5 deletions

File tree

‎packages/material_ui/lib/src/grid_tile.dart‎

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
/// @docImport 'grid_tile_bar.dart';
66
library;
77

8+
import 'package:flutter/rendering.dart';
89
import 'package:flutter/widgets.dart';
910

1011
/// A tile in a Material Design grid list.
@@ -46,17 +47,69 @@ class GridTile extends StatelessWidget {
4647
return child;
4748
}
4849

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.
5354
return Stack(
5455
fit: StackFit.passthrough,
5556
children: <Widget>[
56-
child,
57+
_FillBoundedAxes(child: child),
5758
if (header != null) Positioned(top: 0.0, left: 0.0, right: 0.0, child: header!),
5859
if (footer != null) Positioned(left: 0.0, bottom: 0.0, right: 0.0, child: footer!),
5960
],
6061
);
6162
}
6263
}
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+
}

‎packages/material_ui/test/grid_title_test.dart‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,30 @@ void main() {
115115
expect(tester.getSize(find.byKey(childKey)), const Size(200.0, 150.0));
116116
});
117117

118+
testWidgets('GridTile child fills the tile when the constraints are bounded but loose', (
119+
WidgetTester tester,
120+
) async {
121+
final Key childKey = UniqueKey();
122+
123+
await tester.pumpWidget(
124+
MaterialApp(
125+
home: Center(
126+
child: ConstrainedBox(
127+
constraints: const BoxConstraints(maxWidth: 200.0, maxHeight: 150.0),
128+
child: GridTile(
129+
header: const Text('Header'),
130+
footer: const Text('Footer'),
131+
child: Container(key: childKey, width: 80.0, height: 100.0, color: Colors.red),
132+
),
133+
),
134+
),
135+
),
136+
);
137+
138+
expect(tester.getSize(find.byType(GridTile)), const Size(200.0, 150.0));
139+
expect(tester.getSize(find.byKey(childKey)), const Size(200.0, 150.0));
140+
});
141+
118142
testWidgets('GridTileBar does not crash at zero area', (WidgetTester tester) async {
119143
await tester.pumpWidget(
120144
const MaterialApp(

0 commit comments

Comments
 (0)