HTML 内容的 Flutter URL 启动器无法正常工作

问题描述 投票:0回答:4

我正在使用 Flutter 开发一个应用程序。现在我需要在我的应用程序中呈现包含锚链接的 HTML 内容。但是HTML内容中的链接有问题,点击时在浏览器中没有打开链接。

这是我的代码。

Container(
                              color: Colors.white,
                              padding: EdgeInsets.only(top: 10, right: 10, bottom: 10, left: 10),
                              child: Html(
                                  data: htmlContent,
                                  onLinkTap: (link) {
                                    launch(link);
                                  },
                              ))

当我点击它时,它不起作用。

我在顶部导入了这个库。

import 'package:url_launcher/url_launcher.dart';

我的代码有什么问题以及如何修复它?

flutter
4个回答
11
投票
 Html(
  data: dataHTML,
  onLinkTap: (url, _, __, ___) async {
    if (await canLaunch(url!)) {
      await launch(
        url,
      );
    } 
  },
);

在最近的版本中,flutter_html 在使用此事件时,会请求更多参数来完成文档中详细说明的特定任务,但如果目的只是启动 URL,他们可以这样做,不要忘记添加如果您的应用程序使用您在使用 url_launcher 时指定的 API 30 或更高版本,则 AndroidManifest 中的

<queries> 


6
投票
打开 HTML 中的链接对我来说是这样的:

Html( data: 'This is some <a href="https://pub.dev/packages/url_launcher">link</a>', onLinkTap: (url) async { if (await canLaunch(url)) { await launch( url, ); } else { throw 'Could not launch $url'; } }, )
请注意,我首先使用 

await

 
canLaunch
 方法,然后再使用 
launch
 打开我的链接。

你的链接是什么?您能给我们举个例子吗?谢谢!


1
投票
onLinkTap: (url) async { if (await canLaunch(url)) { await launch( url, ); } else { throw 'Could not launch $url'; } },
另外不要忘记将其添加到您的清单文件中

<queries> <intent> <action android:name="android.intent.action.VIEW" /> <data android:scheme="https" /> </intent> <intent> <action android:name="android.intent.action.DIAL" /> <data android:scheme="tel" /> </intent> <intent> <action android:name="android.intent.action.SEND" /> <data android:mimeType="*/*" /> </intent> </queries>
    

0
投票
如果您使用

flutter_html Prerelease: 3.0.0-beta.2

,请使用以下代码。

Html( data: loadHtml(currentEvent.description), onLinkTap: (url, _, __) async { final uri = Uri.parse(url!); if (await canLaunchUrl(uri)) { await launchUrl(uri); } }, ),
    
© www.soinside.com 2019 - 2024. All rights reserved.