Skip to content

Commit d4b1142

Browse files
author
vam
committed
Fix GridTile crash when the incoming constraints are unbounded
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, and the Stack uses StackFit.passthrough so the content still receives the constraints the tile receives. The tile keeps filling the available space when the incoming constraints are tight (the GridView case), and shrink-wraps its content when they are unbounded instead of crashing. Ported from flutter/flutter#191656, which is blocked by the Material code freeze. Fixes flutter/flutter#82055
1 parent 9f595f3 commit d4b1142

3 files changed

Lines changed: 65 additions & 1 deletion

File tree

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,14 @@ class GridTile extends StatelessWidget {
4646
return child;
4747
}
4848

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.
4953
return Stack(
54+
fit: StackFit.passthrough,
5055
children: <Widget>[
51-
Positioned.fill(child: child),
56+
child,
5257
if (header != null) Positioned(top: 0.0, left: 0.0, right: 0.0, child: header!),
5358
if (footer != null) Positioned(left: 0.0, bottom: 0.0, right: 0.0, child: footer!),
5459
],
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: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,62 @@ 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('GridTile with header and footer sizes itself to its child when unconstrained', (
64+
WidgetTester tester,
65+
) 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+
testWidgets('GridTile child fills the tile when the constraints are tight', (
94+
WidgetTester tester,
95+
) async {
96+
final Key childKey = UniqueKey();
97+
98+
await tester.pumpWidget(
99+
MaterialApp(
100+
home: Center(
101+
child: SizedBox(
102+
width: 200.0,
103+
height: 150.0,
104+
child: GridTile(
105+
header: const Text('Header'),
106+
footer: const Text('Footer'),
107+
child: ColoredBox(key: childKey, color: Colors.red),
108+
),
109+
),
110+
),
111+
),
112+
);
113+
114+
expect(tester.getSize(find.byType(GridTile)), const Size(200.0, 150.0));
115+
expect(tester.getSize(find.byKey(childKey)), const Size(200.0, 150.0));
116+
});
117+
62118
testWidgets('GridTileBar does not crash at zero area', (WidgetTester tester) async {
63119
await tester.pumpWidget(
64120
const MaterialApp(

0 commit comments

Comments
 (0)