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

SPl*_*s91 0 android ios flutter flutter-layout

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

Fra*_*ank 7

基本上是这样的

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

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

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