diff --git a/packages/flutter/lib/src/material/expansion_tile.dart b/packages/flutter/lib/src/material/expansion_tile.dart index 83b0161273..6fa484537c 100644 --- a/packages/flutter/lib/src/material/expansion_tile.dart +++ b/packages/flutter/lib/src/material/expansion_tile.dart @@ -46,6 +46,7 @@ class ExpansionTile extends StatefulWidget { this.expandedCrossAxisAlignment, this.expandedAlignment, this.childrenPadding, + this.collapsedBackgroundColor, }) : assert(initiallyExpanded != null), assert(maintainState != null), assert( @@ -85,6 +86,9 @@ class ExpansionTile extends StatefulWidget { /// The color to display behind the sublist when expanded. final Color? backgroundColor; + /// When not null, defines the background color of tile when the sublist is collapsed. + final Color? collapsedBackgroundColor; + /// A widget to display instead of a rotating arrow icon. final Widget? trailing; @@ -261,7 +265,9 @@ class _ExpansionTileState extends State with SingleTickerProvider _iconColorTween ..begin = theme.unselectedWidgetColor ..end = theme.accentColor; - _backgroundColorTween.end = widget.backgroundColor; + _backgroundColorTween + ..begin = widget.collapsedBackgroundColor + ..end = widget.backgroundColor; super.didChangeDependencies(); } diff --git a/packages/flutter/test/material/expansion_tile_test.dart b/packages/flutter/test/material/expansion_tile_test.dart index f6ed737859..2d4f4c5b7f 100644 --- a/packages/flutter/test/material/expansion_tile_test.dart +++ b/packages/flutter/test/material/expansion_tile_test.dart @@ -481,4 +481,40 @@ void main() { expect(columnRect.bottom, paddingRect.bottom - 4); }); + testWidgets('ExpansionTile.collapsedBackgroundColor', (WidgetTester tester) async { + const Key expansionTileKey = Key('expansionTileKey'); + const Color backgroundColor = Colors.red; + const Color collapsedBackgroundColor = Colors.brown; + + await tester.pumpWidget(const MaterialApp( + home: Material( + child: ExpansionTile( + key: expansionTileKey, + title: Text('Title'), + backgroundColor: backgroundColor, + collapsedBackgroundColor: collapsedBackgroundColor, + children: [ + SizedBox(height: 100, width: 100), + ], + ), + ), + )); + + BoxDecoration boxDecoration = tester.firstWidget(find.descendant( + of: find.byKey(expansionTileKey), + matching: find.byType(Container), + )).decoration! as BoxDecoration; + + expect(boxDecoration.color, collapsedBackgroundColor); + + await tester.tap(find.text('Title')); + await tester.pumpAndSettle(); + + boxDecoration = tester.firstWidget(find.descendant( + of: find.byKey(expansionTileKey), + matching: find.byType(Container), + )).decoration! as BoxDecoration; + + expect(boxDecoration.color, backgroundColor); + }); }