我正在构建一个 Flutter 应用程序,我想在 Web 浏览器或浏览器窗口中打开一个 URL(以响应按钮点击)。我该怎么做?
最佳答案
TL;DR
现在实现为 Plugin
const url = "https://flutter.io";
if (await canLaunchUrl(url))
await launchUrl(url);
else
// can't launch url, there is some error
throw "Could not launch $url";
完整示例:
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.7.10
如果 url
值包含现在在 URL 中允许的空格或其他值,请使用
Uri.encodeFull(urlString)
或 Uri.encodeComponent(urlString)
并传递结果值。
https://stackoverflow.com/questions/43149055/