首页 > 解决方案 > Flutter:如何在 RichText 小部件中插入链接

问题描述

在这段视频中,他们说您可以使用手势识别器在 TextSpan 中创建可点击的链接,但我在 RichText 官方文档和互联网上一般都没有找到示例。有人可以解释一下我该怎么做吗?

标签: androidiosflutterflutter-layout

解决方案


基本上是这样的

RichText(
  text: TextSpan(
    children: [
      TextSpan(text: 'Hello'),
        TextSpan(
          text: 'World',
            recognizer: TapGestureRecognizer()
              ..onTap = () async {
                //Code to launch your URL
              }
        )
      ]
   )
);

您可以使用URL 启动器来管理链接,如下所示:

const url = 'https://flutter.dev';
if (await canLaunch(url)) {
    await launch(url);
} else {
    throw 'Could not launch $url';
}

推荐阅读