diff --git a/packages/flutter/lib/src/material/chip.dart b/packages/flutter/lib/src/material/chip.dart index ad03f01d73..c14e7ec7c0 100644 --- a/packages/flutter/lib/src/material/chip.dart +++ b/packages/flutter/lib/src/material/chip.dart @@ -238,6 +238,9 @@ abstract class DeletableChipAttributes { /// The message to be used for the chip's delete button tooltip. /// /// This will be shown only if [useDeleteButtonTooltip] is true. + /// + /// If not specified, the default [MaterialLocalizations.deleteButtonTooltip] will + /// be used. String? get deleteButtonTooltipMessage; } @@ -1775,11 +1778,8 @@ class _RawChipState extends State with MaterialStateMixin, TickerProvid } } - Widget? _wrapWithTooltip(String? tooltip, VoidCallback? callback, Widget? child) { - if(!widget.useDeleteButtonTooltip) { - return child; - } - if (child == null || callback == null || tooltip == null) { + Widget? _wrapWithTooltip({String? tooltip, bool enabled = true, Widget? child}) { + if (child == null || !enabled || tooltip == null) { return child; } return Tooltip( @@ -1800,9 +1800,9 @@ class _RawChipState extends State with MaterialStateMixin, TickerProvid container: true, button: true, child: _wrapWithTooltip( - widget.deleteButtonTooltipMessage ?? MaterialLocalizations.of(context).deleteButtonTooltip, - widget.onDeleted, - InkWell( + tooltip: widget.useDeleteButtonTooltip ? widget.deleteButtonTooltipMessage ?? MaterialLocalizations.of(context).deleteButtonTooltip : null, + enabled: widget.onDeleted != null, + child: InkWell( // Radius should be slightly less than the full size of the chip. radius: (_kChipHeight + (widget.padding?.vertical ?? 0.0)) * .45, // Keeps the splash from being constrained to the icon alone. @@ -1884,9 +1884,9 @@ class _RawChipState extends State with MaterialStateMixin, TickerProvid ); }, child: _wrapWithTooltip( - widget.tooltip, - widget.onPressed, - _ChipRenderWidget( + tooltip: widget.tooltip, + enabled: widget.onPressed != null || widget.onSelected != null, + child: _ChipRenderWidget( theme: _ChipRenderTheme( label: DefaultTextStyle( overflow: TextOverflow.fade, diff --git a/packages/flutter/test/material/chip_test.dart b/packages/flutter/test/material/chip_test.dart index a4d46669b2..01f9624cbe 100644 --- a/packages/flutter/test/material/chip_test.dart +++ b/packages/flutter/test/material/chip_test.dart @@ -198,7 +198,8 @@ Widget _chipWithOptionalDeleteButton({ Key? labelKey, required bool deletable, TextDirection textDirection = TextDirection.ltr, - bool hasDeleteButtonTooltip = true, + bool useDeleteButtonTooltip = true, + String? chipTooltip, VoidCallback? onPressed = _doNothing, }) { return _wrapForChip( @@ -206,10 +207,11 @@ Widget _chipWithOptionalDeleteButton({ child: Wrap( children: [ RawChip( + tooltip: chipTooltip, onPressed: onPressed, onDeleted: deletable ? _doNothing : null, deleteIcon: Icon(Icons.close, key: deleteButtonKey), - useDeleteButtonTooltip: hasDeleteButtonTooltip, + useDeleteButtonTooltip: useDeleteButtonTooltip, label: Text( deletable ? 'Chip with Delete Button' @@ -3324,7 +3326,7 @@ void main() { await tester.pumpWidget( _chipWithOptionalDeleteButton( deletable: true, - hasDeleteButtonTooltip: false, + useDeleteButtonTooltip: false, ), ); @@ -3345,6 +3347,65 @@ void main() { await tapGesture.up(); }); + testWidgets('useDeleteButtonTooltip only applies to tooltip', (WidgetTester tester) async { + final UniqueKey deleteButtonKey = UniqueKey(); + await tester.pumpWidget( + _chipWithOptionalDeleteButton( + deleteButtonKey: deleteButtonKey, + deletable: true, + useDeleteButtonTooltip: false, + chipTooltip: 'Chip Tooltip', + ), + ); + + // Tap at the delete icon of the chip, which is at the right + // side of the chip + final Offset centerOfDeleteButton = tester.getCenter(find.byKey(deleteButtonKey)); + final TestGesture hoverGesture = await tester.createGesture(kind: PointerDeviceKind.mouse); + await hoverGesture.moveTo(centerOfDeleteButton); + addTearDown(hoverGesture.removePointer); + + await tester.pump(); + + // Wait for some more time while pressing and holding the delete button + await tester.pumpAndSettle(); + + // There should be no delete tooltip + expect(findTooltipContainer('Delete'), findsNothing); + // There should be a chip tooltip, however. + expect(findTooltipContainer('Chip Tooltip'), findsOneWidget); + }); + + testWidgets('Setting useDeleteButtonTooltip also allows Chip tooltip', (WidgetTester tester) async { + final UniqueKey deleteButtonKey = UniqueKey(); + await tester.pumpWidget( + _chipWithOptionalDeleteButton( + deleteButtonKey: deleteButtonKey, + deletable: true, + useDeleteButtonTooltip: true, + chipTooltip: 'Chip Tooltip', + ), + ); + + // Tap at the delete icon of the chip, which is at the right + // side of the chip + final Offset centerOfDeleteButton = tester.getCenter(find.byKey(deleteButtonKey)); + final TestGesture hoverGesture = await tester.createGesture(kind: PointerDeviceKind.mouse); + await hoverGesture.moveTo(centerOfDeleteButton); + addTearDown(hoverGesture.removePointer); + + await tester.pump(); + + // Wait for some more time while pressing and holding the delete button + await tester.pumpAndSettle(); + + // There should also be a chip tooltip + expect(findTooltipContainer('Chip Tooltip'), findsOneWidget); + // There should be a delete tooltip + expect(findTooltipContainer('Delete'), findsOneWidget); + }); + + testWidgets('intrinsicHeight implementation meets constraints', (WidgetTester tester) async { // Regression test for https://github.com/flutter/flutter/issues/49478. await tester.pumpWidget(_wrapForChip(