3

我有大段,很多单词都必须有提示信息。当您单击这些单词中的任何一个时,应该会出现工具提示消息。

我尝试使用 RichText 小部件,其中包含许多TextSpan子组件,如下所示:

RichText(
  text: TextSpan(
     children: <TextSpan>[
        TextSpan(text: "Welcome to"),
        TextSpan(text: "Flutter"),
        ... 
     ]),
),

单击时我需要显示工具提示文本TextSpan 我试图TextSpanTooltip小部件包装

RichText(
  text: TextSpan(
     children: <TextSpan>[
        TextSpan(text: "Welcome to"),
        ...
        Tooltip(
            message: "any text here",
            child: TextSpan(text: "Flutter"),
        ),
        ...            
     ]),
),

但这是不可能的,因为孩子们必须是TextSpan唯一的。

有人知道如何实现这一要求吗?

4

3 回答 3

5

使用TextSpan,您有两种方法:使用或不使用children参数。

Widget _toolTipSimple() {
    return Center(
      child: Tooltip(
        message: "Flutter",
        child: RichText(
          text: TextSpan(
              text: "Welcome to", style: TextStyle(fontSize: 70)),
        ),
      ),
   );
}

这是一个没有工具提示的复杂版本,但它处理对特定单词的点击:

Widget _snackBarTextSpanChildren(BuildContext context) {
    return Center(
      child: RichText(
        textAlign: TextAlign.center,
        text: TextSpan(
          children: [
            TextSpan(text: "Welcome to ", style: TextStyle(fontSize: 70)),
            TextSpan(
                text: "Flutter",
                style: TextStyle(fontSize: 70),
                recognizer: TapGestureRecognizer()..onTap = () {
                  Scaffold.of(context).showSnackBar(SnackBar(content: Text('Hello!')));
                }),
          ],
        ),
      ),
    );
  }

这个结果如下:

特定单词上的 Snackbar

于 2020-02-24T18:45:17.057 回答
2

您可以像这样创建自定义WidgetSpan

class TooltipSpan extends WidgetSpan {
  TooltipSpan({
    @required String message,
    @required InlineSpan inlineSpan,
  }) : super(
          child: Tooltip(
            message: message,
            child: Text.rich(
              inlineSpan,
            ),
          ),
        );
}

并用它包装你的 TextSpan:

RichText(
  text: TextSpan(
     children: <TextSpan>[
        TextSpan(text: "Welcome to"),
        ...
        TooltipSpan(
            message: "any text here",
            inlineSpan: TextSpan(text: "Flutter"),
        ),
        ...            
     ]),
),
于 2021-05-08T23:16:17.070 回答
0

我试图做你需要的,但因为 SpanText 只工作,所以没有工作,但如果你检查下面的代码我的工作:)

Center(
          // Center is a layout widget. It takes a single child and positions it
          // in the middle of the parent.
          child: Column(
              // Column is also a layout widget. It takes a list of children and
              // arranges them vertically. By default, it sizes itself to fit its
              // children horizontally, and tries to be as tall as its parent.
              //
              // Invoke "debug painting" (press "p" in the console, choose the
              // "Toggle Debug Paint" action from the Flutter Inspector in Android
              // Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
              // to see the wireframe for each widget.
              //
              // Column has various properties to control how it sizes itself and
              // how it positions its children. Here we use mainAxisAlignment to
              // center the children vertically; the main axis here is the vertical
              // axis because Columns are vertical (the cross axis would be
              // horizontal).
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Text(
                  'You have pushed the button this many times:',
                ),
                RichText(
                  text: TextSpan(
                      style: TextStyle(color: Colors.black),
                      children: <TextSpan>[
                        TextSpan(text: "Welcome to"),
                      ]),
                ),
                Tooltip(
                  message: 'any text here',
                  child: Text('Flutter'),
                  ),
              ]
                ),
              ),

,

于 2020-02-24T18:43:43.037 回答