用于在移动平台中启动URL的Flutter插件。支持iOS和Android。
添加依赖
dependencies:
url_launcher: ^5.0.1
安装
$ flutter packages get
实例
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
void main() {
runApp(Scaffold(
body: Center(
child: RaisedButton(
onPressed: _launchURL,
child: Text('Show Flutter homepage'),
),
),
));
}
_launchURL() async {
const url = 'https://flutter.io';
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}
支持URL模式
launch方法接受一个包含URL的字符串参数。可以使用许多不同的URL方案格式化此URL。支持的URL方案取决于底层平台和已安装的应用程序。
iOS和Android都支持的常见方案:
模式 | 动作 |
---|---|
http:<URL> , https:<URL> , e.g. http://flutter.io | Open URL in the default browser |
mailto:<email address>?subject=<subject>&body=<body> , e.g. mailto:smith@example.org?subject=News&body=New%20plugin | Create email to <email address> in the default email app |
tel:<phone number> , e.g. tel:+1 555 010 999 | Make a phone call to <phone number> using the default phone app |
sms:<phone number> , e.g. sms:5550101234 | Send an SMS message to <phone number> using the default messaging app |