Flutter:如何在 RichText 中显示 TextSpan 的工具提示
Flutter: How to display Tooltip for TextSpan inside RichText
我有大段,很多字必须有工具提示信息。当您单击这些词中的任何一个时,应该会出现工具提示消息。
我尝试使用 RichText 小部件,它包含许多 TextSpan
children,如下所示:
RichText(
text: TextSpan(
children: <TextSpan>[
TextSpan(text: "Welcome to"),
TextSpan(text: "Flutter"),
...
]),
),
我需要在单击 TextSpan
时显示工具提示文本
我尝试用 Tooltip
widget
包装 TextSpan
RichText(
text: TextSpan(
children: <TextSpan>[
TextSpan(text: "Welcome to"),
...
Tooltip(
message: "any text here",
child: TextSpan(text: "Flutter"),
),
...
]),
),
但这不可能,因为 children 必须是 TextSpan
。
有人知道如何实现这个要求吗?
我试着做你需要的,但没有成功,因为 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'),
),
]
),
),
,
使用 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!')));
}),
],
),
),
);
}
这一项的结果如下:
您可以像这样创建自定义 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"),
),
...
]),
),
我有大段,很多字必须有工具提示信息。当您单击这些词中的任何一个时,应该会出现工具提示消息。
我尝试使用 RichText 小部件,它包含许多 TextSpan
children,如下所示:
RichText(
text: TextSpan(
children: <TextSpan>[
TextSpan(text: "Welcome to"),
TextSpan(text: "Flutter"),
...
]),
),
我需要在单击 TextSpan
时显示工具提示文本
我尝试用 Tooltip
widget
TextSpan
RichText(
text: TextSpan(
children: <TextSpan>[
TextSpan(text: "Welcome to"),
...
Tooltip(
message: "any text here",
child: TextSpan(text: "Flutter"),
),
...
]),
),
但这不可能,因为 children 必须是 TextSpan
。
有人知道如何实现这个要求吗?
我试着做你需要的,但没有成功,因为 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'),
),
]
),
),
,
使用 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!')));
}),
],
),
),
);
}
这一项的结果如下:
您可以像这样创建自定义 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"),
),
...
]),
),