diff --git a/packages/flutter/lib/src/material/tooltip.dart b/packages/flutter/lib/src/material/tooltip.dart index 5df8ad5589..13cc4a5bbb 100644 --- a/packages/flutter/lib/src/material/tooltip.dart +++ b/packages/flutter/lib/src/material/tooltip.dart @@ -475,6 +475,12 @@ class _TooltipState extends State with SingleTickerProviderStateMixin { @override Widget build(BuildContext context) { + // If message is empty then no need to create a tooltip overlay to show + // the empty black container so just return the wrapped child as is or + // empty container if child is not specified. + if (widget.message.isEmpty) { + return widget.child ?? const SizedBox(); + } assert(Overlay.of(context, debugRequiredFor: widget) != null); final ThemeData theme = Theme.of(context); final TooltipThemeData tooltipTheme = TooltipTheme.of(context); diff --git a/packages/flutter/test/material/tooltip_test.dart b/packages/flutter/test/material/tooltip_test.dart index 601091f9a6..8e7e113168 100644 --- a/packages/flutter/test/material/tooltip_test.dart +++ b/packages/flutter/test/material/tooltip_test.dart @@ -1497,6 +1497,32 @@ void main() { await testGestureLongPress(tester, tooltip); expect(find.text(tooltipText), findsNothing); }); + + testWidgets('Tooltip should not be shown with empty message (with child)', (WidgetTester tester) async { + await tester.pumpWidget( + const MaterialApp( + home: Tooltip( + message: tooltipText, + child: Text(tooltipText), + ), + ), + ); + expect(find.text(tooltipText), findsOneWidget); + }); + + testWidgets('Tooltip should not be shown with empty message (without child)', (WidgetTester tester) async { + await tester.pumpWidget( + const MaterialApp( + home: Tooltip( + message: tooltipText, + ), + ), + ); + expect(find.text(tooltipText), findsNothing); + if (tooltipText.isEmpty) { + expect(find.byType(SizedBox), findsOneWidget); + } + }); } Future setWidgetForTooltipMode(WidgetTester tester, TooltipTriggerMode triggerMode) async {