diff --git a/packages/flutter/lib/src/material/filter_chip.dart b/packages/flutter/lib/src/material/filter_chip.dart index 317c9452c6..7770cf23b8 100644 --- a/packages/flutter/lib/src/material/filter_chip.dart +++ b/packages/flutter/lib/src/material/filter_chip.dart @@ -10,6 +10,7 @@ import 'chip_theme.dart'; import 'color_scheme.dart'; import 'colors.dart'; import 'debug.dart'; +import 'icons.dart'; import 'material_state.dart'; import 'text_theme.dart'; import 'theme.dart'; @@ -56,6 +57,7 @@ enum _ChipVariant { flat, elevated } class FilterChip extends StatelessWidget implements ChipAttributes, + DeletableChipAttributes, SelectableChipAttributes, CheckmarkableChipAttributes, DisabledChipAttributes { @@ -72,6 +74,10 @@ class FilterChip extends StatelessWidget this.labelPadding, this.selected = false, required this.onSelected, + this.deleteIcon, + this.onDeleted, + this.deleteIconColor, + this.deleteButtonTooltipMessage, this.pressElevation, this.disabledColor, this.selectedColor, @@ -111,6 +117,10 @@ class FilterChip extends StatelessWidget this.labelPadding, this.selected = false, required this.onSelected, + this.deleteIcon, + this.onDeleted, + this.deleteIconColor, + this.deleteButtonTooltipMessage, this.pressElevation, this.disabledColor, this.selectedColor, @@ -150,6 +160,14 @@ class FilterChip extends StatelessWidget @override final ValueChanged? onSelected; @override + final Widget? deleteIcon; + @override + final VoidCallback? onDeleted; + @override + final Color? deleteIconColor; + @override + final String? deleteButtonTooltipMessage; + @override final double? pressElevation; @override final Color? disabledColor; @@ -205,6 +223,8 @@ class FilterChip extends StatelessWidget final ChipThemeData? defaults = Theme.of(context).useMaterial3 ? _FilterChipDefaultsM3(context, isEnabled, selected, _chipVariant) : null; + final Widget? resolvedDeleteIcon = deleteIcon + ?? (Theme.of(context).useMaterial3 ? const Icon(Icons.clear, size: 18) : null); return RawChip( defaultProperties: defaults, avatar: avatar, @@ -212,6 +232,10 @@ class FilterChip extends StatelessWidget labelStyle: labelStyle, labelPadding: labelPadding, onSelected: onSelected, + deleteIcon: resolvedDeleteIcon, + onDeleted: onDeleted, + deleteIconColor: deleteIconColor, + deleteButtonTooltipMessage: deleteButtonTooltipMessage, pressElevation: pressElevation, selected: selected, tooltip: tooltip, diff --git a/packages/flutter/test/material/filter_chip_test.dart b/packages/flutter/test/material/filter_chip_test.dart index fd69291d3f..e50ecf34a8 100644 --- a/packages/flutter/test/material/filter_chip_test.dart +++ b/packages/flutter/test/material/filter_chip_test.dart @@ -2,10 +2,13 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart'; +import 'feedback_tester.dart'; + /// Adds the basic requirements for a Chip. Widget wrapForChip({ required Widget child, @@ -722,4 +725,198 @@ void main() { expect(getIconData(tester).color, const Color(0xff00ff00)); }); + + testWidgetsWithLeakTracking('Material3 - FilterChip supports delete button', (WidgetTester tester) async { + final ThemeData theme = ThemeData(); + await tester.pumpWidget( + MaterialApp( + theme: theme, + home: Material( + child: Center( + child: FilterChip( + onDeleted: () { }, + onSelected: (bool valueChanged) { }, + label: const Text('FilterChip'), + ), + ), + ), + ), + ); + + // Test the chip size with delete button. + expect(find.text('FilterChip'), findsOneWidget); + expect(tester.getSize(find.byType(FilterChip)), const Size(195.0, 48.0)); + + // Test the delete button icon. + expect(tester.getSize(find.byIcon(Icons.clear)), const Size(18.0, 18.0)); + expect(getIconData(tester).color, theme.colorScheme.onSecondaryContainer); + + await tester.pumpWidget( + MaterialApp( + theme: theme, + home: Material( + child: Center( + child: FilterChip.elevated( + onDeleted: () { }, + onSelected: (bool valueChanged) { }, + label: const Text('Elevated FilterChip'), + ), + ), + ), + ), + ); + + // Test the elevated chip size with delete button. + expect(find.text('Elevated FilterChip'), findsOneWidget); + expect( + tester.getSize(find.byType(FilterChip)), + within(distance: 0.001, from: const Size(321.9, 48.0)), + ); + + // Test the delete button icon. + expect(tester.getSize(find.byIcon(Icons.clear)), const Size(18.0, 18.0)); + expect(getIconData(tester).color, theme.colorScheme.onSecondaryContainer); + }, skip: kIsWeb && !isCanvasKit); // https://github.com/flutter/flutter/issues/99933 + + testWidgetsWithLeakTracking('Material2 - FilterChip supports delete button', (WidgetTester tester) async { + final ThemeData theme = ThemeData(useMaterial3: false); + await tester.pumpWidget( + MaterialApp( + theme: theme, + home: Material( + child: Center( + child: FilterChip( + onDeleted: () { }, + onSelected: (bool valueChanged) { }, + label: const Text('FilterChip'), + ), + ), + ), + ), + ); + + // Test the chip size with delete button. + expect(find.text('FilterChip'), findsOneWidget); + expect(tester.getSize(find.byType(FilterChip)), const Size(188.0, 48.0)); + + // Test the delete button icon. + expect(tester.getSize(find.byIcon(Icons.cancel)), const Size(18.0, 18.0)); + expect(getIconData(tester).color, theme.iconTheme.color?.withAlpha(0xde)); + + await tester.pumpWidget( + MaterialApp( + theme: theme, + home: Material( + child: Center( + child: FilterChip.elevated( + onDeleted: () { }, + onSelected: (bool valueChanged) { }, + label: const Text('Elevated FilterChip'), + ), + ), + ), + ), + ); + + // Test the elevated chip size with delete button. + expect(find.text('Elevated FilterChip'), findsOneWidget); + expect(tester.getSize(find.byType(FilterChip)), const Size(314.0, 48.0)); + + // Test the delete button icon. + expect(tester.getSize(find.byIcon(Icons.cancel)), const Size(18.0, 18.0)); + expect(getIconData(tester).color, theme.iconTheme.color?.withAlpha(0xde)); + }); + + testWidgetsWithLeakTracking('Customize FilterChip delete button', (WidgetTester tester) async { + final ThemeData theme = ThemeData(); + Widget buildChip({ + Widget? deleteIcon, + Color? deleteIconColor, + String? deleteButtonTooltipMessage, + }) { + return MaterialApp( + theme: theme, + home: Material( + child: Center( + child: FilterChip( + deleteIcon: deleteIcon, + deleteIconColor: deleteIconColor, + deleteButtonTooltipMessage: deleteButtonTooltipMessage, + onDeleted: () { }, + onSelected: (bool valueChanged) { }, + label: const Text('FilterChip'), + ), + ), + ), + ); + } + + // Test the custom delete icon. + await tester.pumpWidget(buildChip(deleteIcon: const Icon(Icons.delete))); + + expect(find.byIcon(Icons.clear), findsNothing); + expect(find.byIcon(Icons.delete), findsOneWidget); + + // Test the custom delete icon color. + await tester.pumpWidget(buildChip( + deleteIcon: const Icon(Icons.delete), + deleteIconColor: const Color(0xff00ff00)), + ); + await tester.pumpAndSettle(); + + expect(find.byIcon(Icons.clear), findsNothing); + expect(find.byIcon(Icons.delete), findsOneWidget); + expect(getIconData(tester).color, const Color(0xff00ff00)); + + // Test the custom delete button tooltip message. + await tester.pumpWidget(buildChip(deleteButtonTooltipMessage: 'Delete FilterChip')); + await tester.pumpAndSettle(); + + // Hover over the delete icon of the chip + final TestGesture gesture = await tester.startGesture(tester.getCenter(find.byIcon(Icons.clear))); + + await tester.pumpAndSettle(); + + // Verify the tooltip message is set. + expect(find.widgetWithText(Tooltip, 'Delete FilterChip'), findsOneWidget); + + await gesture.up(); + }); + + testWidgetsWithLeakTracking('FilterChip delete button control test', (WidgetTester tester) async { + final FeedbackTester feedback = FeedbackTester(); + final List deletedButtonStrings = []; + await tester.pumpWidget( + MaterialApp( + home: Material( + child: Center( + child: FilterChip( + onDeleted: () { + deletedButtonStrings.add('A'); + }, + onSelected: (bool valueChanged) { }, + label: const Text('FilterChip'), + ), + ), + ), + ), + ); + + expect(feedback.clickSoundCount, 0); + + expect(deletedButtonStrings, isEmpty); + await tester.tap(find.byIcon(Icons.clear)); + expect(deletedButtonStrings, equals(['A'])); + + await tester.pumpAndSettle(const Duration(seconds: 1)); + expect(feedback.clickSoundCount, 1); + + await tester.tap(find.byIcon(Icons.clear)); + expect(deletedButtonStrings, equals(['A', 'A'])); + + await tester.pumpAndSettle(const Duration(seconds: 1)); + expect(feedback.clickSoundCount, 2); + + feedback.dispose(); + }); }