Flutter:如何在RichText小部件中插入链接

问题描述

在此video中,他们说您可以使用手势识别器来在TextSpan中创建可点击的链接,但我在RichText官方文档中以及一般在Internet上都找不到示例。有人可以解释一下该怎么做吗?

解决方法

基本上是这样的

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

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

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