ExpansionTile Unable to remove right padding from title (#145271)

This PR makes it possible to remove the hard-coded 32.0 unit padding coming from ListTile when trailing is not null. Which it always it as `ExpansionTile` always provides a widget there. Currently there is no way to remove it and people have to hack around it: e.g. 
https://stackoverflow.com/questions/54714836/how-to-remove-default-padding-from-expansiontiles-header

The issue is quite old and I came across it today and decided to put together a quick fix.

Fixes #145268

The change should be non-breaking as the default stays the same. But I have no issue with changing something if requested. (naming-wise, functionality-wise etc...)
This commit is contained in:
Igor Hnízdo 2024-04-05 00:55:20 +02:00 committed by GitHub
parent a12aaa595b
commit 8d928947ca
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 41 additions and 1 deletions

View File

@ -235,6 +235,7 @@ class ExpansionTile extends StatefulWidget {
this.onExpansionChanged,
this.children = const <Widget>[],
this.trailing,
this.showTrailingIcon = true,
this.initiallyExpanded = false,
this.maintainState = false,
this.tilePadding,
@ -322,6 +323,9 @@ class ExpansionTile extends StatefulWidget {
/// may replace the rotating expansion arrow icon.
final Widget? trailing;
/// Specifies if the [ExpansionTile] should build a default trailing icon if [trailing] is null.
final bool showTrailingIcon;
/// Specifies if the list tile is initially expanded (true) or collapsed (false, the default).
final bool initiallyExpanded;
@ -731,7 +735,7 @@ class _ExpansionTileState extends State<ExpansionTile> with SingleTickerProvider
leading: widget.leading ?? _buildLeadingIcon(context),
title: widget.title,
subtitle: widget.subtitle,
trailing: widget.trailing ?? _buildTrailingIcon(context),
trailing: widget.showTrailingIcon ? widget.trailing ?? _buildTrailingIcon(context) : null,
minTileHeight: widget.minTileHeight,
),
),

View File

@ -1532,4 +1532,40 @@ void main() {
expect(find.text('Child 0'), findsOneWidget);
expect(controller.isExpanded, isTrue);
});
testWidgets('ExpansionTile does not include the default trailing icon when showTrailingIcon: false (#145268)', (WidgetTester tester) async {
await tester.pumpWidget(const MaterialApp(
home: Material(
child: ExpansionTile(
enabled: false,
tilePadding: EdgeInsets.zero,
title: ColoredBox(color: Colors.red, child: Text('Title')),
showTrailingIcon: false,
),
),
));
final Size materialAppSize = tester.getSize(find.byType(MaterialApp));
final Size titleSize = tester.getSize(find.byType(ColoredBox));
expect(titleSize.width, materialAppSize.width);
});
testWidgets('ExpansionTile with smaller trailing widget allocates at least 32.0 units of space (preserves original behavior) (#145268)', (WidgetTester tester) async {
await tester.pumpWidget(const MaterialApp(
home: Material(
child: ExpansionTile(
enabled: false,
tilePadding: EdgeInsets.zero,
title: ColoredBox(color: Colors.red, child: Text('Title')),
trailing: SizedBox.shrink(),
),
),
));
final Size materialAppSize = tester.getSize(find.byType(MaterialApp));
final Size titleSize = tester.getSize(find.byType(ColoredBox));
expect(titleSize.width, materialAppSize.width - 32.0);
});
}