How to use tooltip widget in Flutter

The Tooltip widget in Flutter provides a simple and effective way to display a brief description or label when the user hovers over or long-presses on a widget. It is particularly useful for providing additional context or explaining the purpose of a specific UI element in your app. In this Answer, we will walk through the steps to use the Tooltip widget in our Flutter application, along with code examples.

Constructor and parameters

The Tooltip widget provides a constructor with various parameters to customize its behavior and layout.

const Tooltip({
  Key? key,
  String? message,
  InlineSpan? richMessage,
  double? height,
  EdgeInsetsGeometry? padding,
  EdgeInsetsGeometry? margin,
  double? verticalOffset,
  bool? preferBelow,
  bool? excludeFromSemantics,
  Decoration? decoration,
  TextStyle? textStyle,
  TextAlign? textAlign,
  Duration? waitDuration,
  Duration? showDuration,
  TooltipTriggerMode? triggerMode,
  bool? enableFeedback,
  TooltipTriggeredCallback? onTriggered,
  Widget? child
})

Let's take a closer look at each parameter and its purpose:

  • key: An identifier for the widget, used to uniquely identify widgets in the widget tree.

  • message: The text that appears in the tooltip when the user hovers over or long-presses the widget.

  • richMessage: A rich text message that can contain multiple styles and formats.

  • height: The height of the tooltip box.

  • padding: The padding around the tooltip message.

  • margin: The margin around the tooltip.

  • verticalOffset: The vertical distance between the widget and the tooltip box.

  • preferBelow: If true, the tooltip is displayed below the widget; otherwise, it appears above.

  • excludeFromSemantics: Whether the tooltip is excluded from the semantic tree.

  • decoration: The decoration to apply to the tooltip box.

  • textStyle: The text style of the tooltip message.

  • textAlign: The alignment of the tooltip message.

  • waitDuration: The duration the user must wait before the tooltip appears after hovering or long-pressing the widget.

  • showDuration: The duration for which the tooltip remains visible after it's triggered.

  • triggerMode: Specifies when the tooltip should be triggered (long press or hover).

  • enableFeedback: Set to false to disable haptic feedback when the tooltip is triggered.

  • onTriggered: A callback function is called when the tooltip is triggered.

  • child: The widget for which the tooltip is displayed.

Importing necessary libraries

Before using the Tooltip widget, make sure to import the required libraries in our Dart file:

import 'package:flutter/material.dart';

Implementing Tooltip

The Tooltip widget wraps the widget for which we want to provide a tooltip. It can be used with widgets like the IconButton, FloatingActionButton, or even Text and Image widgets. It requires a message parameter, which is the text that appears when the user hovers over or long-presses on the wrapped widget. We can customize the message to clearly and concisely explain the widget's purpose.

Tooltip(
message: 'Click me!',
child: ElevatedButton(
onPressed: () {
// Add your action here
},
child: Text('Educative Button'),
),
),

After running the above code, we can see the following output:

Tooltip added to ElevatedButton
Tooltip added to ElevatedButton

Customizing Tooltip

The Tooltip widget offers some additional properties to control its behavior and appearance:

Tooltip(
message: 'Click me!',
height: 40,
padding: EdgeInsets.all(8),
preferBelow: true,
verticalOffset: 20,
showDuration: Duration(seconds: 2),
waitDuration: Duration(milliseconds: 500),
child: ElevatedButton(
onPressed: () {
// Add your action here
},
child: Text('Educative Button'),
),
),

After running the above code, we can see the following output:

Customized Tooltip wuth red color added to ElevatedButton
Customized Tooltip wuth red color added to ElevatedButton

Code example

We get the following output by putting together the code explained above.

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Educative Tooltip Answer'),
        ),
        body: Center(
          child: Tooltip(
            message: 'Click me!',
            height: 50,
            padding: EdgeInsets.all(8),
            preferBelow: true,
            verticalOffset: 20,
            showDuration: Duration(seconds: 2),
            waitDuration: Duration(milliseconds: 500),
            decoration: BoxDecoration(
                      color: Colors.red, // Set the color of the tooltip here
                    ),
            child: ElevatedButton(
              onPressed: () {
                // Add your action here
              },
              child: Text('Educative Button'),
            ),
          ),
        ),
      ),
    );
  }
}

Conclusion

Using the Tooltip widget in Flutter is a powerful way to provide contextual information and enhance the user experience. We can add brief descriptions, labels, or hints to our app's UI elements by wrapping widgets with the Tooltip widget. Remember to customize the tooltip to fit our design and ensure the messages are clear and concise. With tooltips, we can make our app more user-friendly and intuitive.

Copyright ©2024 Educative, Inc. All rights reserved