From 9d971fada15eb6570a54b65bbd13bdef4a4a16d5 Mon Sep 17 00:00:00 2001 From: Taha Tesser Date: Thu, 29 Aug 2024 10:02:23 +0300 Subject: [PATCH] Fix `IconButton` doesn't show `Tooltip` when hovering within the button area but outside the `Icon` (#153691) Fixes [Tooltip is not shown consistently on entire IconButton](https://github.com/flutter/flutter/issues/153544) ### Code sample
expand to view the code sample ```dart import 'package:flutter/material.dart'; void main() { runApp( MaterialApp( home: Scaffold( body: Center( child: Row( spacing: 20.0, mainAxisAlignment: MainAxisAlignment.center, children: [ IconButton( padding: const EdgeInsets.all(20.0), icon: const ColoredBox( color: Color(0xFFFF0000), child: Icon(Icons.add), ), onPressed: () {}, tooltip: 'Tooltip', ), IconButton.filled( padding: const EdgeInsets.all(20.0), icon: const ColoredBox( color: Color(0xFFFF0000), child: Icon(Icons.add), ), onPressed: () {}, tooltip: 'Tooltip', ), IconButton.filledTonal( padding: const EdgeInsets.all(20.0), icon: const ColoredBox( color: Color(0xFFFF0000), child: Icon(Icons.add), ), onPressed: () {}, tooltip: 'Tooltip', ), IconButton.outlined( padding: const EdgeInsets.all(20.0), icon: const ColoredBox( color: Color(0xFFFF0000), child: Icon(Icons.add), ), onPressed: () {}, tooltip: 'Tooltip', ), ], ), ), ), ), ); } ```
### Before (Hover outside the red box but within the `IconButton`) Screenshot 2024-08-19 at 15 30 35 ### Before (Hover outside the red box but within the `IconButton`) Screenshot 2024-08-19 at 15 30 09 ### Demo https://github.com/user-attachments/assets/129eb8ee-e132-45c9-80b7-165486c02951 --- .../lib/src/material/button_style_button.dart | 26 +++- .../flutter/lib/src/material/icon_button.dart | 15 +-- .../test/material/icon_button_test.dart | 117 +++++++++++++++--- 3 files changed, 125 insertions(+), 33 deletions(-) diff --git a/packages/flutter/lib/src/material/button_style_button.dart b/packages/flutter/lib/src/material/button_style_button.dart index 6c7146deba..6a1c50cdee 100644 --- a/packages/flutter/lib/src/material/button_style_button.dart +++ b/packages/flutter/lib/src/material/button_style_button.dart @@ -26,6 +26,7 @@ import 'material_state.dart'; import 'outlined_button.dart'; import 'text_button.dart'; import 'theme_data.dart'; +import 'tooltip.dart'; /// {@template flutter.material.ButtonStyleButton.iconAlignment} /// Determines the alignment of the icon within the widgets such as: @@ -85,8 +86,9 @@ abstract class ButtonStyleButton extends StatefulWidget { required this.clipBehavior, this.statesController, this.isSemanticButton = true, - required this.child, this.iconAlignment = IconAlignment.start, + this.tooltip, + required this.child, }); /// Called when the button is tapped or otherwise activated. @@ -155,14 +157,23 @@ abstract class ButtonStyleButton extends StatefulWidget { /// Defaults to true. final bool? isSemanticButton; + /// {@macro flutter.material.ButtonStyleButton.iconAlignment} + final IconAlignment iconAlignment; + + /// Text that describes the action that will occur when the button is pressed or + /// hovered over. + /// + /// This text is displayed when the user long-presses or hovers over the button + /// in a tooltip. This string is also used for accessibility. + /// + /// If null, the button will not display a tooltip. + final String? tooltip; + /// Typically the button's label. /// /// {@macro flutter.widgets.ProxyWidget.child} final Widget? child; - /// {@macro flutter.material.ButtonStyleButton.iconAlignment} - final IconAlignment iconAlignment; - /// Returns a [ButtonStyle] that's based primarily on the [Theme]'s /// [ThemeData.textTheme] and [ThemeData.colorScheme], but has most values /// filled out (non-null). @@ -469,6 +480,13 @@ class _ButtonStyleState extends State with TickerProviderStat effectiveChild = resolvedBackgroundBuilder(context, statesController.value, effectiveChild); } + if (widget.tooltip != null) { + effectiveChild = Tooltip( + message: widget.tooltip, + child: effectiveChild, + ); + } + final Widget result = ConstrainedBox( constraints: effectiveConstraints, child: Material( diff --git a/packages/flutter/lib/src/material/icon_button.dart b/packages/flutter/lib/src/material/icon_button.dart index b122bc25cf..9f321eeef8 100644 --- a/packages/flutter/lib/src/material/icon_button.dart +++ b/packages/flutter/lib/src/material/icon_button.dart @@ -719,14 +719,6 @@ class IconButton extends StatelessWidget { effectiveIcon = selectedIcon!; } - Widget iconButton = effectiveIcon; - if (tooltip != null) { - iconButton = Tooltip( - message: tooltip, - child: effectiveIcon, - ); - } - return _SelectableIconButton( style: adjustedStyle, onPressed: onPressed, @@ -734,7 +726,8 @@ class IconButton extends StatelessWidget { focusNode: focusNode, isSelected: isSelected, variant: _variant, - child: iconButton, + tooltip: tooltip, + child: effectiveIcon, ); } @@ -835,6 +828,7 @@ class _SelectableIconButton extends StatefulWidget { required this.variant, required this.autofocus, required this.onPressed, + this.tooltip, required this.child, }); @@ -844,6 +838,7 @@ class _SelectableIconButton extends StatefulWidget { final _IconButtonVariant variant; final bool autofocus; final VoidCallback? onPressed; + final String? tooltip; final Widget child; @override @@ -891,6 +886,7 @@ class _SelectableIconButtonState extends State<_SelectableIconButton> { onPressed: widget.onPressed, variant: widget.variant, toggleable: toggleable, + tooltip: widget.tooltip, child: Semantics( selected: widget.isSelected, child: widget.child, @@ -914,6 +910,7 @@ class _IconButtonM3 extends ButtonStyleButton { super.statesController, required this.variant, required this.toggleable, + super.tooltip, required Widget super.child, }) : super( onLongPress: null, diff --git a/packages/flutter/test/material/icon_button_test.dart b/packages/flutter/test/material/icon_button_test.dart index cdae2a9431..2088ccfb72 100644 --- a/packages/flutter/test/material/icon_button_test.dart +++ b/packages/flutter/test/material/icon_button_test.dart @@ -32,6 +32,13 @@ void main() { return tester.allRenderObjects.firstWhere((RenderObject object) => object.runtimeType.toString() == '_RenderInkFeatures'); } + Finder findTooltipContainer(String tooltipText) { + return find.ancestor( + of: find.text(tooltipText), + matching: find.byType(Container), + ); + } + testWidgets('test icon is findable by key', (WidgetTester tester) async { const ValueKey key = ValueKey('icon-button'); await tester.pumpWidget( @@ -412,45 +419,43 @@ void main() { }); testWidgets('test tooltip', (WidgetTester tester) async { - await tester.pumpWidget( - MaterialApp( + const String tooltipText = 'Test tooltip'; + Widget buildIconButton({ String? tooltip }) { + return MaterialApp( theme: theme, home: Material( child: Center( child: IconButton( onPressed: mockOnPressedFunction.handler, icon: const Icon(Icons.ac_unit), + tooltip: tooltip, ), ), ), - ), - ); + ); + } + + await tester.pumpWidget(buildIconButton()); expect(find.byType(Tooltip), findsNothing); // Clear the widget tree. await tester.pumpWidget(Container(key: UniqueKey())); - await tester.pumpWidget( - MaterialApp( - theme: theme, - home: Material( - child: Center( - child: IconButton( - onPressed: mockOnPressedFunction.handler, - icon: const Icon(Icons.ac_unit), - tooltip: 'Test tooltip', - ), - ), - ), - ), - ); + await tester.pumpWidget(buildIconButton(tooltip: tooltipText)); expect(find.byType(Tooltip), findsOneWidget); - expect(find.byTooltip('Test tooltip'), findsOneWidget); + expect(find.byTooltip(tooltipText), findsOneWidget); - await tester.tap(find.byTooltip('Test tooltip')); + await tester.tap(find.byTooltip(tooltipText)); expect(mockOnPressedFunction.called, 1); + + // Hovering over the button should show the tooltip. + final TestGesture gesture = await tester.createGesture(kind: PointerDeviceKind.mouse); + await gesture.addPointer(location: tester.getCenter(find.byType(IconButton))); + await tester.pump(); + + expect(findTooltipContainer(tooltipText), findsOneWidget); }); testWidgets('IconButton AppBar size', (WidgetTester tester) async { @@ -2954,6 +2959,78 @@ void main() { // No exception is thrown. }); }); + + // This is a regression test for https://github.com/flutter/flutter/issues/153544. + testWidgets('Tooltip is drawn when hovering within IconButton area but outside the Icon itself', (WidgetTester tester) async { + const String tooltipText = 'Test tooltip'; + await tester.pumpWidget( + MaterialApp( + home: Material( + child: Center( + child: IconButton( + onPressed: () {}, + icon: const Icon(Icons.favorite), + tooltip: tooltipText, + ), + ), + ), + ), + ); + + // Verify that the tooltip is not shown initially. + expect(findTooltipContainer(tooltipText), findsNothing); + + // Start hovering within IconButton area to show the tooltip. + final TestGesture gesture = await tester.createGesture(kind: PointerDeviceKind.mouse); + await gesture.addPointer(); + + final Offset topLeft = tester.getTopLeft(find.byType(Icon)); + // Move the cursor just outside the Icon. + await gesture.moveTo(Offset(topLeft.dx - 1, topLeft.dy - 1)); + await tester.pump(); + + // Verify that the tooltip is shown. + expect(findTooltipContainer(tooltipText), findsOneWidget); + }); + + // This is a regression test for https://github.com/flutter/flutter/issues/153544. + testWidgets('Trigger Ink splash when hovering within layout bounds with tooltip', (WidgetTester tester) async { + await tester.pumpWidget( + MaterialApp( + home: Material( + child: Center( + child: ColoredBox( + color: const Color(0xFFFF0000), + child: IconButton( + onPressed: () {}, + icon: const Icon(Icons.favorite), + tooltip: 'Test tooltip', + style: const ButtonStyle( + overlayColor: WidgetStatePropertyAll(Color(0xFF00FF00)), + padding: WidgetStatePropertyAll(EdgeInsets.all(20)), + ), + ), + ), + ), + ), + ), + ); + + final Offset topLeft = tester.getTopLeft(find.byType(ColoredBox)); + final TestGesture gesture = await tester.createGesture( + kind: PointerDeviceKind.mouse, + ); + await gesture.addPointer(); + await gesture.moveTo(topLeft); + await gesture.down(topLeft); + await tester.pumpAndSettle(); + expect( + getOverlayColor(tester), + paints + ..rect(color: const Color(0xFFFF0000)) // ColoredBox. + ..rect(color: const Color(0xFF00FF00)), // IconButton overlay. + ); + }); } Widget wrap({required Widget child, required bool useMaterial3}) {