在原生開發(fā)中,我們通常會(huì)用戶調(diào)用底層的撥打電話、發(fā)送郵件、發(fā)送信息以及打開網(wǎng)址和打開第三方應(yīng)用等等,在Flutter也同樣支持。下面簡(jiǎn)單的介紹一下url_launcher這款插件的使用(支持IOS和Android)。
1、將此添加到包的pubspec.yaml文件中:
dependencies:
url_launcher: ^5.2.7
2、安裝軟件包:
(1)通過命令行安裝軟件包
$ flutter pub get
(2)通過編譯器安裝軟件包(部分編譯軟件)
flutter pub get
3、代碼中導(dǎo)入包:
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.dev';
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}
4、其他實(shí)例:
1、在默認(rèn)瀏覽器中打開網(wǎng)址
http:<URL> , https:<URL>
e.g:
http://flutter.io
2、發(fā)送郵件
mailto:<email address>subject=<subject>&body=<body>
e.g:
mailto:smith@example.org?subject=News&body=New%20plugin
3、撥打電話
tel:<phone number>
e.g:
tel:+1 555 010 999
3、發(fā)送信息
sms:<phone number>
e.g:
sms:5550101234