Skip to content

Commit ddf386a

Browse files
authored
[material_ui] Fix GridTile crash when the incoming constraints are unbounded (flutter#12586)
Ports flutter/flutter#191656, which is blocked by the Material/Cupertino code freeze in flutter/flutter (see flutter/flutter#188444). ## Original description `GridTile` placed every child of its `Stack` in a `Positioned` widget, including the tile content. A `Stack` without any non-positioned child sizes itself to the biggest allowed size, which is infinite along the main axis of an unbounded parent such as a horizontally scrolling `ListView`. `RenderStack` then failed its `size.isFinite` assertion. The tile content is now a non-positioned child of the `Stack` (with `StackFit.passthrough`), wrapped in a small private render object that makes it fill every bounded axis of the incoming constraints and lets it pick its own size only along unbounded axes. As a result, the layout is unchanged whenever the old code did not crash (tight constraints as in `GridView`, and bounded but loose constraints such as `Center` > `ConstrainedBox`), including the reported intrinsic sizes, and the tile shrink-wraps its content along an unbounded axis instead of crashing. Fixes flutter/flutter#82055 ## Test plan ## Pre-Review Checklist The change uses the repository's pending changelog workflow (`packages/material_ui/pending_changelogs/change_2026_08_25_port191656.yaml`). No public API documentation change is needed. [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent 4b4d9e6 commit ddf386a

3 files changed

Lines changed: 234 additions & 1 deletion

File tree

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

Lines changed: 67 additions & 1 deletion
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,12 +47,77 @@ class GridTile extends StatelessWidget {
4647
return child;
4748
}
4849

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.
4954
return Stack(
55+
fit: StackFit.passthrough,
5056
children: <Widget>[
51-
Positioned.fill(child: child),
57+
_FillBoundedAxes(child: child),
5258
if (header != null) Positioned(top: 0.0, left: 0.0, right: 0.0, child: header!),
5359
if (footer != null) Positioned(left: 0.0, bottom: 0.0, right: 0.0, child: footer!),
5460
],
5561
);
5662
}
5763
}
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+
if (childConstraints.isTight) {
100+
return childConstraints.smallest;
101+
}
102+
return constraints.constrain(
103+
child?.getDryLayout(childConstraints) ?? childConstraints.smallest,
104+
);
105+
}
106+
107+
@override
108+
double? computeDryBaseline(BoxConstraints constraints, TextBaseline baseline) {
109+
return child?.getDryBaseline(_fillBoundedAxes(constraints), baseline);
110+
}
111+
112+
@override
113+
void performLayout() {
114+
final BoxConstraints childConstraints = _fillBoundedAxes(constraints);
115+
final RenderBox? child = this.child;
116+
if (child == null) {
117+
size = constraints.constrain(childConstraints.smallest);
118+
return;
119+
}
120+
child.layout(childConstraints, parentUsesSize: true);
121+
size = constraints.constrain(child.size);
122+
}
123+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
changelog: |
2+
- Fix `GridTile` crash when the incoming constraints are unbounded (e.g. inside a horizontally scrolling `ListView`).
3+
version: patch

‎packages/material_ui/test/grid_title_test.dart‎

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,170 @@ void main() {
5959
expect(tester.getSize(find.byType(GridTile)), Size.zero);
6060
});
6161

62+
// Regression test for https://github.com/flutter/flutter/issues/82055
63+
testWidgets(
64+
'GridTile with header and footer sizes itself to its child when the width is unbounded',
65+
(WidgetTester tester) async {
66+
await tester.pumpWidget(
67+
MaterialApp(
68+
home: Center(
69+
child: SizedBox(
70+
height: 100.0,
71+
child: ListView.builder(
72+
scrollDirection: Axis.horizontal,
73+
itemCount: 1,
74+
itemBuilder: (BuildContext context, int index) {
75+
return GridTile(
76+
header: const Text('Header'),
77+
footer: const Text('Footer'),
78+
child: Container(height: 100.0, width: 80.0, color: Colors.red),
79+
);
80+
},
81+
),
82+
),
83+
),
84+
),
85+
);
86+
87+
expect(tester.takeException(), isNull);
88+
expect(tester.getSize(find.byType(GridTile)), const Size(80.0, 100.0));
89+
expect(find.text('Header'), findsOneWidget);
90+
expect(find.text('Footer'), findsOneWidget);
91+
},
92+
);
93+
94+
testWidgets('GridTile child fills the tile when the constraints are tight', (
95+
WidgetTester tester,
96+
) async {
97+
final Key childKey = UniqueKey();
98+
99+
await tester.pumpWidget(
100+
MaterialApp(
101+
home: Center(
102+
child: SizedBox(
103+
width: 200.0,
104+
height: 150.0,
105+
child: GridTile(
106+
header: const Text('Header'),
107+
footer: const Text('Footer'),
108+
child: ColoredBox(key: childKey, color: Colors.red),
109+
),
110+
),
111+
),
112+
),
113+
);
114+
115+
expect(tester.getSize(find.byType(GridTile)), const Size(200.0, 150.0));
116+
expect(tester.getSize(find.byKey(childKey)), const Size(200.0, 150.0));
117+
});
118+
119+
testWidgets('GridTile child fills the tile when the constraints are bounded but loose', (
120+
WidgetTester tester,
121+
) async {
122+
final Key childKey = UniqueKey();
123+
124+
await tester.pumpWidget(
125+
MaterialApp(
126+
home: Center(
127+
child: ConstrainedBox(
128+
constraints: const BoxConstraints(maxWidth: 200.0, maxHeight: 150.0),
129+
child: GridTile(
130+
header: const Text('Header'),
131+
footer: const Text('Footer'),
132+
child: Container(key: childKey, width: 80.0, height: 100.0, color: Colors.red),
133+
),
134+
),
135+
),
136+
),
137+
);
138+
139+
expect(tester.getSize(find.byType(GridTile)), const Size(200.0, 150.0));
140+
expect(tester.getSize(find.byKey(childKey)), const Size(200.0, 150.0));
141+
});
142+
143+
testWidgets(
144+
'GridTile dry baseline matches its baseline when the constraints are bounded but loose',
145+
(WidgetTester tester) async {
146+
const constraints = BoxConstraints(maxWidth: 200.0, maxHeight: 150.0);
147+
148+
await tester.pumpWidget(
149+
MaterialApp(
150+
home: Center(
151+
child: ConstrainedBox(
152+
constraints: constraints,
153+
child: const GridTile(
154+
footer: Text('Footer'),
155+
// Shrink-wraps the text vertically under loose constraints, but
156+
// centers it vertically when forced to fill the tile.
157+
child: Align(alignment: Alignment.centerLeft, heightFactor: 1.0, child: Text('X')),
158+
),
159+
),
160+
),
161+
),
162+
);
163+
164+
// The tile's baseline is the baseline of the child's text, which is above
165+
// the footer's.
166+
final RenderBox text = tester.renderObject<RenderBox>(find.text('X'));
167+
final double expectedBaseline =
168+
tester.getTopLeft(find.text('X')).dy -
169+
tester.getTopLeft(find.byType(GridTile)).dy +
170+
text.getDryBaseline(const BoxConstraints(), TextBaseline.alphabetic)!;
171+
expect(tester.getSize(find.byType(GridTile)), const Size(200.0, 150.0));
172+
173+
final RenderBox tile = tester.renderObject<RenderBox>(
174+
find.descendant(of: find.byType(GridTile), matching: find.byType(Stack)),
175+
);
176+
expect(tile.getDryBaseline(constraints, TextBaseline.alphabetic), expectedBaseline);
177+
},
178+
);
179+
180+
testWidgets('GridTile with header and footer reports zero intrinsic sizes', (
181+
WidgetTester tester,
182+
) async {
183+
await tester.pumpWidget(
184+
const MaterialApp(
185+
home: GridTile(
186+
header: Text('Header'),
187+
footer: Text('Footer'),
188+
child: SizedBox(width: 80.0, height: 100.0),
189+
),
190+
),
191+
);
192+
193+
final RenderBox tile = tester.renderObject<RenderBox>(find.byType(GridTile));
194+
expect(tile.getMinIntrinsicWidth(150.0), 0.0);
195+
expect(tile.getMaxIntrinsicWidth(150.0), 0.0);
196+
expect(tile.getMinIntrinsicHeight(200.0), 0.0);
197+
expect(tile.getMaxIntrinsicHeight(200.0), 0.0);
198+
});
199+
200+
testWidgets('GridTile dry layout does not query its child when both axes are bounded', (
201+
WidgetTester tester,
202+
) async {
203+
await tester.pumpWidget(
204+
MaterialApp(
205+
home: GridTile(
206+
header: const Text('Header'),
207+
footer: const Text('Footer'),
208+
child: LayoutBuilder(
209+
builder: (BuildContext context, BoxConstraints constraints) => const SizedBox(),
210+
),
211+
),
212+
),
213+
);
214+
215+
final RenderBox tile = tester.renderObject<RenderBox>(find.byType(GridTile));
216+
expect(
217+
tile.getDryLayout(const BoxConstraints(maxWidth: 200.0, maxHeight: 150.0)),
218+
const Size(200.0, 150.0),
219+
);
220+
expect(
221+
tile.getDryLayout(const BoxConstraints.tightFor(width: 200.0, height: 150.0)),
222+
const Size(200.0, 150.0),
223+
);
224+
});
225+
62226
testWidgets('GridTileBar does not crash at zero area', (WidgetTester tester) async {
63227
await tester.pumpWidget(
64228
const MaterialApp(

0 commit comments

Comments
 (0)