diff --git a/packages/flutter/lib/src/material/expansion_tile.dart b/packages/flutter/lib/src/material/expansion_tile.dart index bf865d39f4..6756931c58 100644 --- a/packages/flutter/lib/src/material/expansion_tile.dart +++ b/packages/flutter/lib/src/material/expansion_tile.dart @@ -668,6 +668,32 @@ class _ExpansionTileState extends State with SingleTickerProvider ); } + @override + void didUpdateWidget(covariant ExpansionTile oldWidget) { + super.didUpdateWidget(oldWidget); + final ThemeData theme = Theme.of(context); + final ExpansionTileThemeData expansionTileTheme = ExpansionTileTheme.of(context); + final ExpansionTileThemeData defaults = theme.useMaterial3 + ? _ExpansionTileDefaultsM3(context) + : _ExpansionTileDefaultsM2(context); + if (widget.collapsedShape != oldWidget.collapsedShape + || widget.shape != oldWidget.shape) { + _updateShapeBorder(expansionTileTheme, theme); + } + if (widget.collapsedTextColor != oldWidget.collapsedTextColor + || widget.textColor != oldWidget.textColor) { + _updateHeaderColor(expansionTileTheme, defaults); + } + if (widget.collapsedIconColor != oldWidget.collapsedIconColor + || widget.iconColor != oldWidget.iconColor) { + _updateIconColor(expansionTileTheme, defaults); + } + if (widget.backgroundColor != oldWidget.backgroundColor + || widget.collapsedBackgroundColor != oldWidget.collapsedBackgroundColor) { + _updateBackgroundColor(expansionTileTheme); + } + } + @override void didChangeDependencies() { final ThemeData theme = Theme.of(context); @@ -675,6 +701,14 @@ class _ExpansionTileState extends State with SingleTickerProvider final ExpansionTileThemeData defaults = theme.useMaterial3 ? _ExpansionTileDefaultsM3(context) : _ExpansionTileDefaultsM2(context); + _updateShapeBorder(expansionTileTheme, theme); + _updateHeaderColor(expansionTileTheme, defaults); + _updateIconColor(expansionTileTheme, defaults); + _updateBackgroundColor(expansionTileTheme); + super.didChangeDependencies(); + } + + void _updateShapeBorder(ExpansionTileThemeData expansionTileTheme, ThemeData theme) { _borderTween ..begin = widget.collapsedShape ?? expansionTileTheme.collapsedShape @@ -688,20 +722,28 @@ class _ExpansionTileState extends State with SingleTickerProvider top: BorderSide(color: theme.dividerColor), bottom: BorderSide(color: theme.dividerColor), ); + } + + void _updateHeaderColor(ExpansionTileThemeData expansionTileTheme, ExpansionTileThemeData defaults) { _headerColorTween ..begin = widget.collapsedTextColor ?? expansionTileTheme.collapsedTextColor ?? defaults.collapsedTextColor ..end = widget.textColor ?? expansionTileTheme.textColor ?? defaults.textColor; + } + + void _updateIconColor(ExpansionTileThemeData expansionTileTheme, ExpansionTileThemeData defaults) { _iconColorTween ..begin = widget.collapsedIconColor ?? expansionTileTheme.collapsedIconColor ?? defaults.collapsedIconColor ..end = widget.iconColor ?? expansionTileTheme.iconColor ?? defaults.iconColor; + } + + void _updateBackgroundColor(ExpansionTileThemeData expansionTileTheme) { _backgroundColorTween ..begin = widget.collapsedBackgroundColor ?? expansionTileTheme.collapsedBackgroundColor ..end = widget.backgroundColor ?? expansionTileTheme.backgroundColor; - super.didChangeDependencies(); } @override diff --git a/packages/flutter/test/material/expansion_tile_test.dart b/packages/flutter/test/material/expansion_tile_test.dart index 3c6d9640a5..9900a1101c 100644 --- a/packages/flutter/test/material/expansion_tile_test.dart +++ b/packages/flutter/test/material/expansion_tile_test.dart @@ -893,6 +893,162 @@ void main() { handle.dispose(); }, variant: const TargetPlatformVariant({ TargetPlatform.iOS, TargetPlatform.macOS })); + testWidgetsWithLeakTracking('Collapsed ExpansionTile properties can be updated with setState', (WidgetTester tester) async { + const Key expansionTileKey = Key('expansionTileKey'); + ShapeBorder collapsedShape = const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(4)), + ); + Color collapsedTextColor = const Color(0xffffffff); + Color collapsedBackgroundColor = const Color(0xffff0000); + Color collapsedIconColor = const Color(0xffffffff); + + await tester.pumpWidget(MaterialApp( + home: Material( + child: StatefulBuilder( + builder: (BuildContext context, StateSetter setState) { + return Column( + children: [ + ExpansionTile( + key: expansionTileKey, + collapsedShape: collapsedShape, + collapsedTextColor: collapsedTextColor, + collapsedBackgroundColor: collapsedBackgroundColor, + collapsedIconColor: collapsedIconColor, + title: const TestText('title'), + trailing: const TestIcon(), + children: const [ + SizedBox(height: 100, width: 100), + ], + ), + // This button is used to update the ExpansionTile properties. + FilledButton( + onPressed: () { + setState(() { + collapsedShape = const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(16)), + ); + collapsedTextColor = const Color(0xff000000); + collapsedBackgroundColor = const Color(0xffffff00); + collapsedIconColor = const Color(0xff000000); + }); + }, + child: const Text('Update collapsed properties'), + ), + ], + ); + } + ), + ), + )); + + ShapeDecoration shapeDecoration = tester.firstWidget(find.descendant( + of: find.byKey(expansionTileKey), + matching: find.byType(Container), + )).decoration! as ShapeDecoration; + + // Test initial ExpansionTile properties. + expect(shapeDecoration.shape, const RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(4)))); + expect(shapeDecoration.color, const Color(0xffff0000)); + expect(tester.state(find.byType(TestIcon)).iconTheme.color, const Color(0xffffffff)); + expect(tester.state(find.byType(TestText)).textStyle.color, const Color(0xffffffff)); + + // Tap the button to update the ExpansionTile properties. + await tester.tap(find.text('Update collapsed properties')); + await tester.pumpAndSettle(); + + shapeDecoration = tester.firstWidget(find.descendant( + of: find.byKey(expansionTileKey), + matching: find.byType(Container), + )).decoration! as ShapeDecoration; + + // Test updated ExpansionTile properties. + expect(shapeDecoration.shape, const RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(16)))); + expect(shapeDecoration.color, const Color(0xffffff00)); + expect(tester.state(find.byType(TestIcon)).iconTheme.color, const Color(0xff000000)); + expect(tester.state(find.byType(TestText)).textStyle.color, const Color(0xff000000)); + }); + + testWidgetsWithLeakTracking('Expanded ExpansionTile properties can be updated with setState', (WidgetTester tester) async { + const Key expansionTileKey = Key('expansionTileKey'); + ShapeBorder shape = const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(12)), + ); + Color textColor = const Color(0xff00ffff); + Color backgroundColor = const Color(0xff0000ff); + Color iconColor = const Color(0xff00ffff); + + await tester.pumpWidget(MaterialApp( + home: Material( + child: StatefulBuilder( + builder: (BuildContext context, StateSetter setState) { + return Column( + children: [ + ExpansionTile( + key: expansionTileKey, + shape: shape, + textColor: textColor, + backgroundColor: backgroundColor, + iconColor: iconColor, + title: const TestText('title'), + trailing: const TestIcon(), + children: const [ + SizedBox(height: 100, width: 100), + ], + ), + // This button is used to update the ExpansionTile properties. + FilledButton( + onPressed: () { + setState(() { + shape = const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(6)), + ); + textColor = const Color(0xffffffff); + backgroundColor = const Color(0xff123456); + iconColor = const Color(0xffffffff); + }); + }, + child: const Text('Update collapsed properties'), + ), + ], + ); + } + ), + ), + )); + + // Tap to expand the ExpansionTile. + await tester.tap(find.text('title')); + await tester.pumpAndSettle(); + + ShapeDecoration shapeDecoration = tester.firstWidget(find.descendant( + of: find.byKey(expansionTileKey), + matching: find.byType(Container), + )).decoration! as ShapeDecoration; + + // Test initial ExpansionTile properties. + expect(shapeDecoration.shape, const RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(12)))); + expect(shapeDecoration.color, const Color(0xff0000ff)); + expect(tester.state(find.byType(TestIcon)).iconTheme.color, const Color(0xff00ffff)); + expect(tester.state(find.byType(TestText)).textStyle.color, const Color(0xff00ffff)); + + // Tap the button to update the ExpansionTile properties. + await tester.tap(find.text('Update collapsed properties')); + await tester.pumpAndSettle(); + + shapeDecoration = tester.firstWidget(find.descendant( + of: find.byKey(expansionTileKey), + matching: find.byType(Container), + )).decoration! as ShapeDecoration; + iconColor = tester.state(find.byType(TestIcon)).iconTheme.color!; + textColor = tester.state(find.byType(TestText)).textStyle.color!; + + // Test updated ExpansionTile properties. + expect(shapeDecoration.shape, const RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(6)))); + expect(shapeDecoration.color, const Color(0xff123456)); + expect(tester.state(find.byType(TestIcon)).iconTheme.color, const Color(0xffffffff)); + expect(tester.state(find.byType(TestText)).textStyle.color, const Color(0xffffffff)); + }); + group('Material 2', () { // These tests are only relevant for Material 2. Once Material 2 // support is deprecated and the APIs are removed, these tests