Fix TooltipState null check error (#106330)
This commit is contained in:
@@ -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();
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user