diff --git a/packages/flutter/lib/src/material/tooltip.dart b/packages/flutter/lib/src/material/tooltip.dart index 2d8bea0a94..3741e6f1a7 100644 --- a/packages/flutter/lib/src/material/tooltip.dart +++ b/packages/flutter/lib/src/material/tooltip.dart @@ -265,7 +265,8 @@ class _TooltipState extends State with SingleTickerProviderStateMixin { /// Shows the tooltip if it is not already visible. /// - /// Returns `false` when the tooltip was already visible. + /// Returns `false` when the tooltip was already visible or if the context has + /// become null. bool ensureTooltipVisible() { _showTimer?.cancel(); _showTimer = null; @@ -336,6 +337,7 @@ class _TooltipState extends State with SingleTickerProviderStateMixin { if (_entry != null) { _hideTooltip(immediately: true); } + _showTimer?.cancel(); super.deactivate(); } diff --git a/packages/flutter/test/material/tooltip_test.dart b/packages/flutter/test/material/tooltip_test.dart index e7bf9215b4..9c8ca308c7 100644 --- a/packages/flutter/test/material/tooltip_test.dart +++ b/packages/flutter/test/material/tooltip_test.dart @@ -821,6 +821,53 @@ void main() { expect(find.text(tooltipText), findsNothing); }); + testWidgets('Tooltip does not attempt to show after unmount', (WidgetTester tester) async { + // Regression test for https://github.com/flutter/flutter/issues/54096. + const Duration waitDuration = Duration(seconds: 1); + final TestGesture gesture = await tester.createGesture(kind: PointerDeviceKind.mouse); + addTearDown(() async { + if (gesture != null) + return gesture.removePointer(); + }); + await gesture.addPointer(); + await gesture.moveTo(const Offset(1.0, 1.0)); + await tester.pump(); + await gesture.moveTo(Offset.zero); + + await tester.pumpWidget( + const MaterialApp( + home: Center( + child: Tooltip( + message: tooltipText, + waitDuration: waitDuration, + child: SizedBox( + width: 100.0, + height: 100.0, + ), + ), + ), + ), + ); + + final Finder tooltip = find.byType(Tooltip); + await gesture.moveTo(Offset.zero); + await tester.pump(); + await gesture.moveTo(tester.getCenter(tooltip)); + await tester.pump(); + + // Pump another random widget to unmount the Tooltip widget. + await tester.pumpWidget( + const MaterialApp( + home: Center( + child: SizedBox(), + ), + ), + ); + + // If the issue regresses, an exception will be thrown while we are waiting. + await tester.pump(waitDuration); + }); + testWidgets('Does tooltip contribute semantics', (WidgetTester tester) async { final SemanticsTester semantics = SemanticsTester(tester);