Skip to content

Commit cef35e7

Browse files
authored
Update error message for when leading/trailing width exceeds ListTile width and add missing test (flutter#161091)
Fixes [ListTile Crashes When Width Is Reduced with Leading or Trailing Widgets](flutter#159380) ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [ ] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent 1c147a2 commit cef35e7

File tree

2 files changed

+60
-12
lines changed

2 files changed

+60
-12
lines changed

packages/flutter/lib/src/material/list_tile.dart

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1484,18 +1484,36 @@ class _RenderListTile extends RenderBox
14841484
final Size? leadingSize = leading == null ? null : getSize(leading, iconConstraints);
14851485
final Size? trailingSize = trailing == null ? null : getSize(trailing, iconConstraints);
14861486

1487-
assert(
1488-
tileWidth != leadingSize?.width || tileWidth == 0.0,
1489-
'Leading widget consumes entire tile width. Please use a sized widget, '
1490-
'or consider replacing ListTile with a custom widget '
1491-
'(see https://api.flutter.dev/flutter/material/ListTile-class.html#material.ListTile.4)',
1492-
);
1493-
assert(
1494-
tileWidth != trailingSize?.width || tileWidth == 0.0,
1495-
'Trailing widget consumes entire tile width. Please use a sized widget, '
1496-
'or consider replacing ListTile with a custom widget '
1497-
'(see https://api.flutter.dev/flutter/material/ListTile-class.html#material.ListTile.4)',
1498-
);
1487+
assert(() {
1488+
if (tileWidth == 0.0) {
1489+
return true;
1490+
}
1491+
1492+
String? overflowedWidget;
1493+
if (tileWidth == leadingSize?.width) {
1494+
overflowedWidget = 'Leading';
1495+
} else if (tileWidth == trailingSize?.width) {
1496+
overflowedWidget = 'Trailing';
1497+
}
1498+
1499+
if (overflowedWidget == null) {
1500+
return true;
1501+
}
1502+
1503+
throw FlutterError.fromParts(<DiagnosticsNode>[
1504+
ErrorSummary(
1505+
'$overflowedWidget widget consumes the entire tile width (including ListTile.contentPadding).',
1506+
),
1507+
ErrorDescription(
1508+
'Either resize the tile width so that the ${overflowedWidget.toLowerCase()} widget plus any content padding '
1509+
'do not exceed the tile width, or use a sized widget, or consider replacing '
1510+
'ListTile with a custom widget.',
1511+
),
1512+
ErrorHint(
1513+
'See also: https://api.flutter.dev/flutter/material/ListTile-class.html#material.ListTile.4',
1514+
),
1515+
]);
1516+
}());
14991517

15001518
final double titleStart =
15011519
leadingSize == null

packages/flutter/test/material/list_tile_test.dart

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2618,6 +2618,36 @@ void main() {
26182618
expect(trailingOffset.dy - tileOffset.dy, topPosition);
26192619
});
26202620

2621+
testWidgets('Leading/Trailing exceeding list tile width throws exception', (
2622+
WidgetTester tester,
2623+
) async {
2624+
Widget buildListTile({Widget? leading, Widget? trailing}) {
2625+
return MaterialApp(
2626+
home: Material(
2627+
child: Center(
2628+
child: SizedBox(width: 100, child: ListTile(leading: leading, trailing: trailing)),
2629+
),
2630+
),
2631+
);
2632+
}
2633+
2634+
// Test a trailing widget that exceeds the list tile width.
2635+
// 16 (content padding) + 61 (leading width) + 24 (content padding) = 101.
2636+
// List tile width is 100 as a result, an exception should be thrown.
2637+
await tester.pumpWidget(buildListTile(leading: const SizedBox(width: 61)));
2638+
2639+
// Error message cannot be tested as there too many errors thrown.
2640+
expect(tester.takeException(), isNotNull);
2641+
2642+
// Test a trailing widget that exceeds the list tile width.
2643+
// 16 (content padding) + 61 (trailing width) + 24 (content padding) = 101.
2644+
// List tile width is 100 as a result, an exception should be thrown.
2645+
await tester.pumpWidget(buildListTile(trailing: const SizedBox(width: 61)));
2646+
2647+
// Error message cannot tested be as there too many errors thrown.
2648+
expect(tester.takeException(), isNotNull);
2649+
});
2650+
26212651
group('Material 2', () {
26222652
// These tests are only relevant for Material 2. Once Material 2
26232653
// support is deprecated and the APIs are removed, these tests

0 commit comments

Comments
 (0)