From 14b8194bad99c2da899fc402618de178f869e3a7 Mon Sep 17 00:00:00 2001 From: Hans Muller Date: Tue, 16 Mar 2021 11:53:45 -0700 Subject: [PATCH] Added iconColor and textColor properties to ExpansionTile (#78281) --- .../lib/src/material/expansion_tile.dart | 46 ++++++++++++++++--- .../test/material/expansion_tile_test.dart | 38 +++++++++++++++ 2 files changed, 77 insertions(+), 7 deletions(-) diff --git a/packages/flutter/lib/src/material/expansion_tile.dart b/packages/flutter/lib/src/material/expansion_tile.dart index 72bf37e263..c4207e9379 100644 --- a/packages/flutter/lib/src/material/expansion_tile.dart +++ b/packages/flutter/lib/src/material/expansion_tile.dart @@ -20,12 +20,17 @@ const Duration _kExpand = Duration(milliseconds: 200); /// [ExpansionTile] to save and restore its expanded state when it is scrolled /// in and out of view. /// +/// This class overrides the [ListTileTheme.iconColor] and [ListTileTheme.textColor] +/// theme properties for its [ListTile]. These colors animate between values when +/// the tile is expanded and collapsed: between [iconColor], [collapsedIconColor] and +/// between [textColor] and [collapsedTextColor]. +/// /// See also: /// /// * [ListTile], useful for creating expansion tile [children] when the /// expansion tile represents a sublist. -/// * The "Expand/collapse" section of -/// . +/// * The "Expand and collapse" section of +/// class ExpansionTile extends StatefulWidget { /// Creates a single-line [ListTile] with a trailing button that expands or collapses /// the tile to reveal or hide the [children]. The [initiallyExpanded] property must @@ -35,7 +40,6 @@ class ExpansionTile extends StatefulWidget { this.leading, required this.title, this.subtitle, - this.backgroundColor, this.onExpansionChanged, this.children = const [], this.trailing, @@ -45,7 +49,12 @@ class ExpansionTile extends StatefulWidget { this.expandedCrossAxisAlignment, this.expandedAlignment, this.childrenPadding, + this.backgroundColor, this.collapsedBackgroundColor, + this.textColor, + this.collapsedTextColor, + this.iconColor, + this.collapsedIconColor, }) : assert(initiallyExpanded != null), assert(maintainState != null), assert( @@ -147,6 +156,29 @@ class ExpansionTile extends StatefulWidget { /// When the value is null, the value of `childrenPadding` is [EdgeInsets.zero]. final EdgeInsetsGeometry? childrenPadding; + /// The icon color of tile's [trailing] expansion icon when the + /// sublist is expanded. + /// + /// Used to override to the [ListTileTheme.iconColor]. + final Color? iconColor; + + /// The icon color of tile's [trailing] expansion icon when the + /// sublist is collapsed. + /// + /// Used to override to the [ListTileTheme.iconColor]. + final Color? collapsedIconColor; + + + /// The color of the tile's titles when the sublist is expanded. + /// + /// Used to override to the [ListTileTheme.textColor]. + final Color? textColor; + + /// The color of the tile's titles when the sublist is collapsed. + /// + /// Used to override to the [ListTileTheme.textColor]. + final Color? collapsedTextColor; + @override _ExpansionTileState createState() => _ExpansionTileState(); } @@ -259,11 +291,11 @@ class _ExpansionTileState extends State with SingleTickerProvider final ThemeData theme = Theme.of(context); _borderColorTween.end = theme.dividerColor; _headerColorTween - ..begin = theme.textTheme.subtitle1!.color - ..end = theme.accentColor; + ..begin = widget.collapsedTextColor ?? theme.textTheme.subtitle1!.color + ..end = widget.textColor ?? theme.accentColor; _iconColorTween - ..begin = theme.unselectedWidgetColor - ..end = theme.accentColor; + ..begin = widget.collapsedIconColor ?? theme.unselectedWidgetColor + ..end = widget.iconColor ?? theme.accentColor; _backgroundColorTween ..begin = widget.collapsedBackgroundColor ..end = widget.backgroundColor; diff --git a/packages/flutter/test/material/expansion_tile_test.dart b/packages/flutter/test/material/expansion_tile_test.dart index d2b23164ec..3ec7e77c3a 100644 --- a/packages/flutter/test/material/expansion_tile_test.dart +++ b/packages/flutter/test/material/expansion_tile_test.dart @@ -520,4 +520,42 @@ void main() { expect(boxDecoration.color, backgroundColor); }); + + testWidgets('ExpansionTile iconColor, textColor', (WidgetTester tester) async { + // Regression test for https://github.com/flutter/flutter/pull/78281 + + const Color iconColor = Color(0xff00ff00); + const Color collapsedIconColor = Color(0xff0000ff); + const Color textColor = Color(0xff00ffff); + const Color collapsedTextColor = Color(0xffff00ff); + + await tester.pumpWidget(const MaterialApp( + home: Material( + child: ExpansionTile( + iconColor: iconColor, + collapsedIconColor: collapsedIconColor, + textColor: textColor, + collapsedTextColor: collapsedTextColor, + initiallyExpanded: false, + title: TestText('title'), + trailing: TestIcon(), + children: [ + SizedBox(height: 100, width: 100), + ], + ), + ), + )); + + Color getIconColor() => tester.state(find.byType(TestIcon)).iconTheme.color!; + Color getTextColor() => tester.state(find.byType(TestText)).textStyle.color!; + + expect(getIconColor(), collapsedIconColor); + expect(getTextColor(), collapsedTextColor); + + await tester.tap(find.text('title')); + await tester.pumpAndSettle(); + + expect(getIconColor(), iconColor); + expect(getTextColor(), textColor); + }); }