Skip to content

Commit

Permalink
Fix TooltipState null check error (#106330)
Browse files Browse the repository at this point in the history
  • Loading branch information
bleroux authored Jun 22, 2022
1 parent 61b3b39 commit 399a649
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/flutter/lib/src/material/tooltip.dart
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ class TooltipState extends State<Tooltip> with SingleTickerProviderStateMixin {
/// Returns `false` when the tooltip shouldn't be shown or when the tooltip
/// was already visible.
bool ensureTooltipVisible() {
if (!_visible) {
if (!_visible || !mounted) {
return false;
}
_showTimer?.cancel();
Expand Down
31 changes: 31 additions & 0 deletions packages/flutter/test/material/tooltip_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1053,6 +1053,37 @@ void main() {
gesture = null;
});

testWidgets('Calling ensureTooltipVisible on an unmounted TooltipState returns false', (WidgetTester tester) async {
// Regression test for https://github.com/flutter/flutter/issues/95851
await tester.pumpWidget(
const MaterialApp(
home: Center(
child: Tooltip(
message: tooltipText,
child: SizedBox(
width: 100.0,
height: 100.0,
),
),
),
),
);

final TooltipState tooltipState = tester.state(find.byType(Tooltip));
expect(tooltipState.ensureTooltipVisible(), true);

// Remove the tooltip.
await tester.pumpWidget(
const MaterialApp(
home: Center(
child: SizedBox.shrink(),
),
),
);

expect(tooltipState.ensureTooltipVisible(), false);
});

testWidgets('Tooltip shows/hides when hovered', (WidgetTester tester) async {
const Duration waitDuration = Duration.zero;
TestGesture? gesture = await tester.createGesture(kind: PointerDeviceKind.mouse);
Expand Down

0 comments on commit 399a649

Please sign in to comment.