如何获得在RichText颤振字符串的值?



如何获取RichTextWidget中String的值

RichText(
text: TextSpan(
style: TextStyle(color: Colors.black, fontSize: 36),
children: <TextSpan>[
TextSpan(text: 'Woolha ', style: TextStyle(color: Colors.blue)),
TextSpan(text: 'dot '),
TextSpan(
text: 'com',
style: TextStyle(decoration: TextDecoration.underline))
],
),
textScaleFactor: 0.5,
)

我想在RichText中得到字符串的值,有人有办法做到吗?

您可以使用key从RichText中提取文本

final RichText? richText = richTextKey.currentWidget as RichText?;
debugPrint("${richText?.text.toPlainText()}");
class GettingRichText extends StatelessWidget {
GettingRichText({Key? key}) : super(key: key);
final richTextKey = GlobalKey();
@override
Widget build(BuildContext context) {
return Scaffold(
body: RichText(
key: richTextKey,
text: const TextSpan(
style: TextStyle(color: Colors.black, fontSize: 36),
children: <TextSpan>[
TextSpan(text: 'Woolha ', style: TextStyle(color: Colors.blue)),
TextSpan(text: 'dot '),
TextSpan(
text: 'com',
style: TextStyle(decoration: TextDecoration.underline))
],
),
textScaleFactor: 0.5,
),
floatingActionButton: FloatingActionButton(
onPressed: () {
final RichText? richText = richTextKey.currentWidget as RichText;
debugPrint("${richText?.text.toPlainText()}");
},
),
);
}
}

您可以在TextSpantext属性上提供如下字符串变量

String myText = "dot";
TextSpan(text: myText),

相关内容

  • 没有找到相关文章

最新更新