flutter 什么可能的原因是网址启动插件不工作?

gijlo24d  于 2022-11-17  发布在  Flutter
关注(0)|答案(2)|浏览(143)

我想在我的flutter页面上使用Url启动插件。当我从网络上点击我的图片时,我想让Url启动插件打开我的Pdf文件,但它没有发生。你能告诉我正确的代码吗?

new GestureDetector(
                onTap:(){ _launchURL();
                                },
                                child: Container(
                                  decoration: new BoxDecoration(

                                    color: Colors.transparent,
                                    image: new DecorationImage(
                                      image: new NetworkImage(
                                          "https://g.fmanager.net/Image/objects/1-kitaplar/evrimAldatmacasi_8b_tr.jpg"),
                                      fit: BoxFit.scaleDown,
                                    ),
                                  ),
                                  padding: EdgeInsets.all(2.0),
                                  alignment: Alignment.bottomCenter,
                                  child: Transform(
                                    alignment: Alignment.bottomCenter,
                                    transform: Matrix4.skewY(
                                      0.0,
                                    )..rotateZ(0.0),
                                    child: Container(
                                      padding: EdgeInsets.all(8.0),
                                      color: Color(0x99FFFFFF),
                                      child: new Text(
                                        "Evrim Aldatmacası",
                                        textAlign: TextAlign.center,
                                        style: TextStyle(
                                          fontStyle: FontStyle.italic,
                                        ),
                                      ),
                                    ),

                  }
                }


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

rdlzhqv91#

原因是默认情况下它使用LaunchMode.inAppWebView模式。请将launch视为弃用。
所以答案应该是

Uri uri = Uri.parse("https://www.orimi.com/pdf-test.pdf");
if (await canLaunchUrl(uri)){
    await launchUrl(uri, mode: LaunchMode.externalApplication);
}

这以正确的方式打开一个pdf文件。

xzv2uavs

xzv2uavs2#

您可以使用以下代码片段打开PDF文件
请将http://test.com/Android/sample.pdf更改为您的pdf链接位置

Future<void> _launchInBrowser(String url) async {
    if (await canLaunch(url)) {
      await launch(url, forceSafariVC: false, forceWebView: false);
    } else {
      throw 'Could not launch $url';
    }
  }
...

RaisedButton(
                onPressed: () => setState(() {
                  _launched = _launchInBrowser("http://test.com/Android/sample.pdf");
                }),
                child: const Text('Launch in browser'),
              ),

执行结果

相关问题