Fix chips delete icon override the default icon size and ignores IconTheme from the chip property and ChipThemeData (#146509)
fixes [Provided delete icon overrides the default delete icon size](https://github.com/flutter/flutter/issues/146404) fixes [Chips delete icon ignores `IconTheme` from `Chip.iconTheme` and `ChipThemeData.iconTheme`](https://github.com/flutter/flutter/issues/146501) ### Code sample <details> <summary>expand to view the code sample</summary> ```dart import 'package:flutter/material.dart'; void main() => runApp(const MyApp()); class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false, theme: ThemeData( chipTheme: const ChipThemeData( iconTheme: IconThemeData( color: Colors.red, ), ), ), home: Scaffold( body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ FilterChip( avatar: const Icon(Icons.favorite), onDeleted: () {}, label: const Text('Filter Chip'), onSelected: (value) {}, ), const SizedBox(height: 10), FilterChip( avatar: const Icon(Icons.favorite), // ignore: prefer_const_constructors deleteIcon: const Icon(Icons.delete), onDeleted: () {}, label: const Text('Filter Chip'), onSelected: (value) {}, ), ], ), ), ), ); } } ``` </details> ### Before  ### After 
This commit is contained in:
@@ -27,7 +27,6 @@ import 'tooltip.dart';
|
||||
|
||||
// Some design constants
|
||||
const double _kChipHeight = 32.0;
|
||||
const double _kDeleteIconSize = 18.0;
|
||||
|
||||
const int _kCheckmarkAlpha = 0xde; // 87%
|
||||
const int _kDisabledAlpha = 0x61; // 38%
|
||||
@@ -41,7 +40,7 @@ const Duration _kReverseDrawerDuration = Duration(milliseconds: 100);
|
||||
const Duration _kDisableDuration = Duration(milliseconds: 75);
|
||||
|
||||
const Color _kSelectScrimColor = Color(0x60191919);
|
||||
const Icon _kDefaultDeleteIcon = Icon(Icons.cancel, size: _kDeleteIconSize);
|
||||
const Icon _kDefaultDeleteIcon = Icon(Icons.cancel);
|
||||
|
||||
/// An interface defining the base attributes for a Material Design chip.
|
||||
///
|
||||
@@ -262,7 +261,17 @@ abstract interface class ChipAttributes {
|
||||
abstract interface class DeletableChipAttributes {
|
||||
/// The icon displayed when [onDeleted] is set.
|
||||
///
|
||||
/// Defaults to an [Icon] widget set to use [Icons.cancel].
|
||||
/// If [deleteIconColor] is provided, it will be used as the color of the
|
||||
/// delete icon. If [deleteIconColor] is null, then the icon will use the
|
||||
/// color specified in the chip [IconTheme]. If the [IconTheme] is null, then
|
||||
/// the icon will use the color specified in the [ThemeData.iconTheme].
|
||||
///
|
||||
/// If a size is specified in the chip [IconTheme], then the delete icon will
|
||||
/// use that size. Otherwise, defaults to 18 pixels.
|
||||
///
|
||||
/// Defaults to an [Icon] widget set to use [Icons.clear].
|
||||
/// If [ThemeData.useMaterial3] is false, then defaults to an [Icon] widget
|
||||
/// set to use [Icons.cancel].
|
||||
Widget? get deleteIcon;
|
||||
|
||||
/// Called when the user taps the [deleteIcon] to delete the chip.
|
||||
@@ -1179,6 +1188,20 @@ class _RawChipState extends State<RawChip> with MaterialStateMixin, TickerProvid
|
||||
if (!hasDeleteButton) {
|
||||
return null;
|
||||
}
|
||||
final IconThemeData iconTheme = widget.iconTheme
|
||||
?? chipTheme.iconTheme
|
||||
?? theme.chipTheme.iconTheme
|
||||
?? _ChipDefaultsM3(context, widget.isEnabled).iconTheme!;
|
||||
final Color? effectiveDeleteIconColor = widget.deleteIconColor
|
||||
?? chipTheme.deleteIconColor
|
||||
?? theme.chipTheme.deleteIconColor
|
||||
?? widget.iconTheme?.color
|
||||
?? chipTheme.iconTheme?.color
|
||||
?? chipDefaults.deleteIconColor;
|
||||
final double effectiveIconSize = widget.iconTheme?.size
|
||||
?? chipTheme.iconTheme?.size
|
||||
?? theme.chipTheme.iconTheme?.size
|
||||
?? _ChipDefaultsM3(context, widget.isEnabled).iconTheme!.size!;
|
||||
return Semantics(
|
||||
container: true,
|
||||
button: true,
|
||||
@@ -1194,11 +1217,9 @@ class _RawChipState extends State<RawChip> with MaterialStateMixin, TickerProvid
|
||||
customBorder: const CircleBorder(),
|
||||
onTap: widget.isEnabled ? widget.onDeleted : null,
|
||||
child: IconTheme(
|
||||
data: theme.iconTheme.copyWith(
|
||||
color: widget.deleteIconColor
|
||||
?? chipTheme.deleteIconColor
|
||||
?? theme.chipTheme.deleteIconColor
|
||||
?? chipDefaults.deleteIconColor,
|
||||
data: iconTheme.copyWith(
|
||||
color: effectiveDeleteIconColor,
|
||||
size: effectiveIconSize,
|
||||
),
|
||||
child: widget.deleteIcon,
|
||||
),
|
||||
|
||||
@@ -57,6 +57,13 @@ DefaultTextStyle getLabelStyle(WidgetTester tester, String labelText) {
|
||||
);
|
||||
}
|
||||
|
||||
TextStyle? getIconStyle(WidgetTester tester, IconData icon) {
|
||||
final RichText iconRichText = tester.widget<RichText>(
|
||||
find.descendant(of: find.byIcon(icon).first, matching: find.byType(RichText)),
|
||||
);
|
||||
return iconRichText.text.style;
|
||||
}
|
||||
|
||||
dynamic getRenderChip(WidgetTester tester) {
|
||||
if (!tester.any(findRenderChipElement())) {
|
||||
return null;
|
||||
@@ -1794,7 +1801,7 @@ void main() {
|
||||
expect(Focus.of(labelKey.currentContext!).hasPrimaryFocus, isTrue);
|
||||
});
|
||||
|
||||
testWidgets('Material2 - Delete button creates non-centered, unique ripple when tapped', (WidgetTester tester) async {
|
||||
testWidgets('Material2 - Delete button creates centered, unique ripple when tapped', (WidgetTester tester) async {
|
||||
final UniqueKey labelKey = UniqueKey();
|
||||
final UniqueKey deleteButtonKey = UniqueKey();
|
||||
|
||||
@@ -1820,8 +1827,8 @@ void main() {
|
||||
await tester.pump(const Duration(milliseconds: 100));
|
||||
|
||||
// There should be one unique ink ripple.
|
||||
expect(box, ripplePattern(const Offset(3.0, 3.0), 1.44));
|
||||
expect(box, uniqueRipplePattern(const Offset(3.0, 3.0), 1.44));
|
||||
expect(box, ripplePattern(Offset.zero, 1.44));
|
||||
expect(box, uniqueRipplePattern(Offset.zero, 1.44));
|
||||
|
||||
// There should be no tooltip.
|
||||
expect(findTooltipContainer('Delete'), findsNothing);
|
||||
@@ -1832,8 +1839,8 @@ void main() {
|
||||
|
||||
// The ripple should grow, but the center should move,
|
||||
// Towards the center of the delete icon.
|
||||
expect(box, ripplePattern(const Offset(5.0, 5.0), 4.32));
|
||||
expect(box, uniqueRipplePattern(const Offset(5.0, 5.0), 4.32));
|
||||
expect(box, ripplePattern(const Offset(2.0, 2.0), 4.32));
|
||||
expect(box, uniqueRipplePattern(const Offset(2.0, 2.0), 4.32));
|
||||
|
||||
// There should be no tooltip.
|
||||
expect(findTooltipContainer('Delete'), findsNothing);
|
||||
@@ -1925,8 +1932,8 @@ void main() {
|
||||
await tester.pump(const Duration(milliseconds: 100));
|
||||
|
||||
// There should be one unique ink ripple.
|
||||
expect(box, ripplePattern(const Offset(3.0, 3.0), 1.44));
|
||||
expect(box, uniqueRipplePattern(const Offset(3.0, 3.0), 1.44));
|
||||
expect(box, ripplePattern(Offset.zero, 1.44));
|
||||
expect(box, uniqueRipplePattern(Offset.zero, 1.44));
|
||||
|
||||
// There should be no tooltip.
|
||||
expect(findTooltipContainer('Delete'), findsNothing);
|
||||
@@ -1937,8 +1944,8 @@ void main() {
|
||||
|
||||
// The ripple should grow, but the center should move,
|
||||
// Towards the center of the delete icon.
|
||||
expect(box, ripplePattern(const Offset(5.0, 5.0), 4.32));
|
||||
expect(box, uniqueRipplePattern(const Offset(5.0, 5.0), 4.32));
|
||||
expect(box, ripplePattern(const Offset(2.0, 2.0), 4.32));
|
||||
expect(box, uniqueRipplePattern(const Offset(2.0, 2.0), 4.32));
|
||||
|
||||
// There should be no tooltip.
|
||||
expect(findTooltipContainer('Delete'), findsNothing);
|
||||
@@ -4476,7 +4483,7 @@ void main() {
|
||||
Rect avatarBox = tester.getRect(find.byKey(avatarKey));
|
||||
expect(box.size, equals(const Size(128, 32.0 + 16.0)));
|
||||
expect(textBox.size, equals(const Size(56, 14)));
|
||||
expect(iconBox.size, equals(const Size(24, 24)));
|
||||
expect(iconBox.size, equals(const Size(18, 18)));
|
||||
expect(avatarBox.size, equals(const Size(24, 24)));
|
||||
expect(textBox.top, equals(17));
|
||||
expect(box.bottom - textBox.bottom, equals(17));
|
||||
@@ -4491,7 +4498,7 @@ void main() {
|
||||
avatarBox = tester.getRect(find.byKey(avatarKey));
|
||||
expect(box.size, equals(const Size(128, 60)));
|
||||
expect(textBox.size, equals(const Size(56, 14)));
|
||||
expect(iconBox.size, equals(const Size(24, 24)));
|
||||
expect(iconBox.size, equals(const Size(18, 18)));
|
||||
expect(avatarBox.size, equals(const Size(24, 24)));
|
||||
expect(textBox.top, equals(23));
|
||||
expect(box.bottom - textBox.bottom, equals(23));
|
||||
@@ -4506,7 +4513,7 @@ void main() {
|
||||
avatarBox = tester.getRect(find.byKey(avatarKey));
|
||||
expect(box.size, equals(const Size(128, 36)));
|
||||
expect(textBox.size, equals(const Size(56, 14)));
|
||||
expect(iconBox.size, equals(const Size(24, 24)));
|
||||
expect(iconBox.size, equals(const Size(18, 18)));
|
||||
expect(avatarBox.size, equals(const Size(24, 24)));
|
||||
expect(textBox.top, equals(11));
|
||||
expect(box.bottom - textBox.bottom, equals(11));
|
||||
@@ -4523,7 +4530,7 @@ void main() {
|
||||
avatarBox = tester.getRect(find.byKey(avatarKey));
|
||||
expect(box.size, equals(const Size(128, 36)));
|
||||
expect(textBox.size, equals(const Size(56, 14)));
|
||||
expect(iconBox.size, equals(const Size(24, 24)));
|
||||
expect(iconBox.size, equals(const Size(18, 18)));
|
||||
expect(avatarBox.size, equals(const Size(24, 24)));
|
||||
expect(textBox.top, equals(11));
|
||||
expect(box.bottom - textBox.bottom, equals(11));
|
||||
@@ -4583,7 +4590,7 @@ void main() {
|
||||
expect(box.size.height, equals(32.0 + 16.0));
|
||||
expect(textBox.size.width, moreOrLessEquals(56.4, epsilon: 0.1));
|
||||
expect(textBox.size.height, equals(20.0));
|
||||
expect(iconBox.size, equals(const Size(20, 20)));
|
||||
expect(iconBox.size, equals(const Size(18, 18)));
|
||||
expect(avatarBox.size, equals(const Size(18, 18)));
|
||||
expect(textBox.top, equals(14));
|
||||
expect(box.bottom - textBox.bottom, equals(14));
|
||||
@@ -4600,7 +4607,7 @@ void main() {
|
||||
expect(box.size.height, equals(60));
|
||||
expect(textBox.size.width, moreOrLessEquals(56.4, epsilon: 0.1));
|
||||
expect(textBox.size.height, equals(20.0));
|
||||
expect(iconBox.size, equals(const Size(20, 20)));
|
||||
expect(iconBox.size, equals(const Size(18, 18)));
|
||||
expect(avatarBox.size, equals(const Size(18, 18)));
|
||||
expect(textBox.top, equals(20));
|
||||
expect(box.bottom - textBox.bottom, equals(20));
|
||||
@@ -4617,7 +4624,7 @@ void main() {
|
||||
expect(box.size.height, equals(36));
|
||||
expect(textBox.size.width, moreOrLessEquals(56.4, epsilon: 0.1));
|
||||
expect(textBox.size.height, equals(20.0));
|
||||
expect(iconBox.size, equals(const Size(20, 20)));
|
||||
expect(iconBox.size, equals(const Size(18, 18)));
|
||||
expect(avatarBox.size, equals(const Size(18, 18)));
|
||||
expect(textBox.top, equals(8));
|
||||
expect(box.bottom - textBox.bottom, equals(8));
|
||||
@@ -4636,7 +4643,7 @@ void main() {
|
||||
expect(box.size.height, equals(36));
|
||||
expect(textBox.size.width, moreOrLessEquals(56.4, epsilon: 0.1));
|
||||
expect(textBox.size.height, equals(20.0));
|
||||
expect(iconBox.size, equals(const Size(20, 20)));
|
||||
expect(iconBox.size, equals(const Size(18, 18)));
|
||||
expect(avatarBox.size, equals(const Size(18, 18)));
|
||||
expect(textBox.top, equals(8));
|
||||
expect(box.bottom - textBox.bottom, equals(8));
|
||||
@@ -5705,6 +5712,75 @@ void main() {
|
||||
tester.getTopLeft(find.text('A').last).dy,
|
||||
);
|
||||
});
|
||||
|
||||
testWidgets('ChipThemeData.iconTheme updates avatar and delete icons', (WidgetTester tester) async {
|
||||
const Color iconColor = Color(0xffff00ff);
|
||||
const double iconSize = 28.0;
|
||||
const IconData avatarIcon = Icons.favorite;
|
||||
const IconData deleteIcon = Icons.delete;
|
||||
|
||||
await tester.pumpWidget(MaterialApp(
|
||||
home: Material(
|
||||
child: Center(
|
||||
child: RawChip(
|
||||
iconTheme: const IconThemeData(
|
||||
color: iconColor,
|
||||
size: iconSize,
|
||||
),
|
||||
avatar: const Icon(Icons.favorite),
|
||||
deleteIcon: const Icon(Icons.delete),
|
||||
onDeleted: () { },
|
||||
label: const SizedBox(height: 100),
|
||||
),
|
||||
),
|
||||
),
|
||||
));
|
||||
|
||||
// Test rendered icon size.
|
||||
final RenderBox avatarIconBox = tester.renderObject(find.byIcon(avatarIcon));
|
||||
final RenderBox deleteIconBox = tester.renderObject(find.byIcon(deleteIcon));
|
||||
expect(avatarIconBox.size.width, equals(iconSize));
|
||||
expect(deleteIconBox.size.width, equals(iconSize));
|
||||
|
||||
// Test rendered icon color.
|
||||
expect(getIconStyle(tester, avatarIcon)?.color, iconColor);
|
||||
expect(getIconStyle(tester, deleteIcon)?.color, iconColor);
|
||||
});
|
||||
|
||||
testWidgets('RawChip.deleteIconColor overrides iconTheme color', (WidgetTester tester) async {
|
||||
const Color iconColor = Color(0xffff00ff);
|
||||
const Color deleteIconColor = Color(0xffff00ff);
|
||||
const IconData deleteIcon = Icons.delete;
|
||||
|
||||
Widget buildChip({ Color? deleteIconColor, Color? iconColor }) {
|
||||
return MaterialApp(
|
||||
home: Material(
|
||||
child: Center(
|
||||
child: RawChip(
|
||||
deleteIconColor: deleteIconColor,
|
||||
iconTheme: IconThemeData(color: iconColor),
|
||||
deleteIcon: const Icon(Icons.delete),
|
||||
onDeleted: () { },
|
||||
label: const SizedBox(height: 100),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
await tester.pumpWidget(buildChip(iconColor: iconColor));
|
||||
|
||||
// Test rendered icon color.
|
||||
expect(getIconStyle(tester, deleteIcon)?.color, iconColor);
|
||||
|
||||
await tester.pumpWidget(buildChip(
|
||||
deleteIconColor: deleteIconColor,
|
||||
iconColor: iconColor,
|
||||
));
|
||||
|
||||
// Test rendered icon color.
|
||||
expect(getIconStyle(tester, deleteIcon)?.color, deleteIconColor);
|
||||
});
|
||||
}
|
||||
|
||||
class _MaterialStateOutlinedBorder extends StadiumBorder implements MaterialStateOutlinedBorder {
|
||||
|
||||
@@ -44,6 +44,13 @@ DefaultTextStyle getLabelStyle(WidgetTester tester) {
|
||||
);
|
||||
}
|
||||
|
||||
TextStyle? getIconStyle(WidgetTester tester, IconData icon) {
|
||||
final RichText iconRichText = tester.widget<RichText>(
|
||||
find.descendant(of: find.byIcon(icon).first, matching: find.byType(RichText)),
|
||||
);
|
||||
return iconRichText.text.style;
|
||||
}
|
||||
|
||||
void main() {
|
||||
test('ChipThemeData copyWith, ==, hashCode basics', () {
|
||||
expect(const ChipThemeData(), const ChipThemeData().copyWith());
|
||||
@@ -1400,6 +1407,82 @@ void main() {
|
||||
final Offset labelTopRight = tester.getTopRight(find.byType(Container));
|
||||
expect(labelTopRight.dx, deleteIconCenter.dx - (iconSize / 2) - labelPadding);
|
||||
});
|
||||
|
||||
testWidgets('ChipThemeData.iconTheme updates avatar and delete icons', (WidgetTester tester) async {
|
||||
const Color iconColor = Color(0xffff0000);
|
||||
const double iconSize = 32.0;
|
||||
const IconData avatarIcon = Icons.favorite;
|
||||
const IconData deleteIcon = Icons.delete;
|
||||
|
||||
await tester.pumpWidget(MaterialApp(
|
||||
theme: ThemeData(chipTheme: const ChipThemeData(
|
||||
iconTheme: IconThemeData(
|
||||
color: iconColor,
|
||||
size: iconSize,
|
||||
),
|
||||
)),
|
||||
home: Material(
|
||||
child: Center(
|
||||
child: RawChip(
|
||||
avatar: const Icon(Icons.favorite),
|
||||
deleteIcon: const Icon(Icons.delete),
|
||||
onDeleted: () { },
|
||||
label: const SizedBox(height: 100),
|
||||
),
|
||||
),
|
||||
),
|
||||
));
|
||||
|
||||
// Test rendered icon size.
|
||||
final RenderBox avatarIconBox = tester.renderObject(find.byIcon(avatarIcon));
|
||||
final RenderBox deleteIconBox = tester.renderObject(find.byIcon(deleteIcon));
|
||||
expect(avatarIconBox.size.width, equals(iconSize));
|
||||
expect(deleteIconBox.size.width, equals(iconSize));
|
||||
|
||||
// Test rendered icon color.
|
||||
expect(getIconStyle(tester, avatarIcon)?.color, iconColor);
|
||||
expect(getIconStyle(tester, deleteIcon)?.color, iconColor);
|
||||
});
|
||||
|
||||
testWidgets('ChipThemeData.deleteIconColor overrides ChipThemeData.iconTheme color', (WidgetTester tester) async {
|
||||
const Color iconColor = Color(0xffff00ff);
|
||||
const Color deleteIconColor = Color(0xffff00ff);
|
||||
const IconData deleteIcon = Icons.delete;
|
||||
|
||||
Widget buildChip({ Color? deleteIconColor, Color? iconColor }) {
|
||||
return MaterialApp(
|
||||
theme: ThemeData(
|
||||
chipTheme: ChipThemeData(
|
||||
deleteIconColor: deleteIconColor,
|
||||
iconTheme: IconThemeData(color: iconColor),
|
||||
)
|
||||
),
|
||||
home: Material(
|
||||
child: Center(
|
||||
child: RawChip(
|
||||
deleteIcon: const Icon(Icons.delete),
|
||||
onDeleted: () { },
|
||||
label: const SizedBox(height: 100),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
await tester.pumpWidget(buildChip(iconColor: iconColor));
|
||||
|
||||
// Test rendered icon color.
|
||||
expect(getIconStyle(tester, deleteIcon)?.color, iconColor);
|
||||
|
||||
await tester.pumpWidget(buildChip(
|
||||
deleteIconColor: deleteIconColor,
|
||||
iconColor: iconColor,
|
||||
));
|
||||
|
||||
// Test rendered icon color.
|
||||
expect(getIconStyle(tester, deleteIcon)?.color, deleteIconColor);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
class _MaterialStateOutlinedBorder extends StadiumBorder implements MaterialStateOutlinedBorder {
|
||||
|
||||
Reference in New Issue
Block a user