【问题标题】:Flutter Tooltip on all widgets所有小部件上的 Flutter 工具提示
【发布时间】:2023-03-18 23:12:01
【问题描述】:

有没有办法在Text 小部件上设置tooltip:

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

这不起作用。但是它确实像mentioned here 一样在FloatingActionButton 之类的东西上工作:

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

我知道Text 类根本没有实现tooltip。我想知道是否有办法做到这一点。

【问题讨论】:

    标签: dart flutter


    【解决方案1】:

    您可以将文本包装到 Tooltip 小部件中。

    new Tooltip(message: "Hello World", child: new Text("foo"));
    

    【讨论】:

      【解决方案2】:

      Tooltip(message: "This is the tooltip message", child: new Text("Show tooltip"));

      还可以提供高度、装饰等参数。

      【讨论】:

        【解决方案3】:

        虽然 Rémi 的回答是正确的,但您也可以像这样创建自己的小部件:

        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),
            );
          }
        }
        

        并像这样使用它:

        TooltipText(
            text: "Text",
            tooltip: "tiptool",
        );
        

        【讨论】:

        • 这和雷米的回答完全一样,只是包装在一个额外的小部件中。
        猜你喜欢
        • 1970-01-01
        • 2020-02-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-08
        相关资源
        最近更新 更多