Fix TooltipState null check error (#106330)

This commit is contained in:
Bruno Leroux
2022-06-22 07:19:05 +02:00
committed by GitHub
parent 61b3b3999b
commit 399a649eae
2 changed files with 32 additions and 1 deletions

View File

@@ -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();

View File

@@ -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);