Flutter Tooltip on all widgets

2020-07-07 05:44发布

Is there a way to set a tooltip on a Text widget:

new Text(
    "Some content",
    tooltip: "Displays a message to you"
  )

This does not work. However it does work, as mentioned here, on things like the FloatingActionButton:

new FloatingActionButton(
    onPressed: action,
    tooltip: "Action",
    child: new Icon(Icons.add),
  )

I understand that the Text class does simply not have tooltip implemented. I want to know if there is a way to do it anyway.

标签: dart flutter
2条回答
欢心
2楼-- · 2020-07-07 06:13

You can wrap your text into a Tooltip widget.

new Tooltip(message: "Hello World", child: new Text("foo"));
查看更多
唯我独甜
3楼-- · 2020-07-07 06:20

While Rémi's answer is correct, you can also create your own widget like this:

class TooltipText extends StatelessWidget {
  final String text;
  final String tooltip;

  TooltipText({Key key, this.tooltip, this.text});

  @override
  Widget build(BuildContext context) {
    return Tooltip(
      message: tooltip,
      child: Text(text),
    );
  }
}

And use it like this:

TooltipText(
    text: "Text",
    tooltip: "tiptool",
);
查看更多
登录 后发表回答