如何通过Flutter代码打开Web浏览器(URL)? [英] How do I open a web browser (URL) from my Flutter code?
本文介绍了如何通过Flutter代码打开Web浏览器(URL)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在构建Flutter应用,我想在Web浏览器或浏览器窗口中打开URL(以响应点击按钮)。我该怎么做?
I am building a Flutter app, and I'd like to open a URL into a web browser or browser window (in response to a button tap). How can I do this?
推荐答案
TL; DR
现在已实现为插件
const url = "https://flutter.io";
if (await canLaunch(url))
await launch(url);
else
// can't launch url, there is some error
throw "Could not launch $url";
完整示例:
Full example:
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
void main() {
runApp(new Scaffold(
body: new Center(
child: new RaisedButton(
onPressed: _launchURL,
child: new Text('Show Flutter homepage'),
),
),
));
}
_launchURL() async {
const url = 'https://flutter.io';
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}
在pubspec.yaml
In pubspec.yaml
dependencies:
url_launcher: ^5.4.2
特殊字符:
如果 url
值包含URL中现在允许的空格或其他值,请使用
Special Characters:
If the url
value contains spaces or other values that are now allowed in URLs, use
Uri.encodeFull(urlString)
或 Uri.encodeComponent(urlString)
并通过而是结果值。
Uri.encodeFull(urlString)
or Uri.encodeComponent(urlString)
and pass the resulting value instead.
这篇关于如何通过Flutter代码打开Web浏览器(URL)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文