如何通过Flutter代码打开Web浏览器(URL)?
问题内容:
我正在构建Flutter应用程序,并且想在Web浏览器或浏览器窗口中打开URL(以响应按钮点击)。我怎样才能做到这一点?
问题答案:
TL; DR
现在已实现为插件
const url = "https://flutter.io";
if (await canLaunch(url))
launch(url);
else
// can't launch url, there is some error
完整示例:
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中
dependencies:
url_launcher: ^5.4.2
特殊的角色:
如果该url
值包含网址中现在允许的空格或其他值,请使用
Uri.encodeFull(urlString)
或Uri.encodeComponent(urlString)
,而是传递结果值。