前言
Flutter中的url_launcher
是一個用于打開URL的插件。它允許在Flutter應(yīng)用程序中打開網(wǎng)址、發(fā)送電子郵件、撥打電話等操作。使用url_launcher
插件,可以輕松地在應(yīng)用程序中集成各種URL操作。
官方地址
https://pub-web.flutter-io.cn/packages/url_launcher
安裝
flutter pub add url_launcher
基本使用
打開網(wǎng)址
Center(
child: ElevatedButton(
onPressed: () async{
final Uri url = Uri.parse('https://www.csdn.net/');
if (!await launchUrl(url,mode:LaunchMode.externalApplication)) {
throw Exception('Could not launch $url');
}
},
child: const Text("打開瀏覽器"),
)
)
這里有兩個注意點:
- 模擬器里無法正常運(yùn)行,需要使用真機(jī)
- 必須設(shè)置為
mode:LaunchMode.externalApplication
,否則會在應(yīng)用內(nèi)單獨打開一個界面進(jìn)行顯示
電子郵件
Center(
child: ElevatedButton(
onPressed: () async {
// 收件人郵箱
String recipient = "15065845632@163.com";
//郵件主題
String subject = "郵件主題";
// 郵件內(nèi)容
String body = "郵件內(nèi)容";
String mailtoUri =
"mailto:$recipient?subject=$subject&body=$body";
final Uri url = Uri.parse(mailtoUri);
if (!await launchUrl(url, mode: LaunchMode.externalApplication)) {
throw Exception('Could not launch $mailtoUri');
}
},
child: const Text("發(fā)郵件"),
))
發(fā)短信
Center(
child: ElevatedButton(
onPressed: () async {
// 收件人電話
String recipient = "10086";
// 短信內(nèi)容
String body = "1";
String smsUrl = 'sms:$recipient?body=${Uri.encodeQueryComponent(body)}';
final Uri url = Uri.parse(smsUrl);
if (!await launchUrl(url, mode: LaunchMode.externalApplication)) {
throw Exception('Could not launch $smsUrl');
}
},
child: const Text("發(fā)短信"),
))
打開第三方app
Center(
child: ElevatedButton(
onPressed: () async {
final Uri url = Uri.parse('weixin://');
if (!await launchUrl(url, mode: LaunchMode.externalApplication)) {
throw Exception('Could not launch $url');
}
},
child: const Text("打開微信"),
))
微信可以正常打開,下面這些是從網(wǎng)上搜集的,不清楚是否好用
QQ: mqq://
微信: weixin://
京東: openapp.jdmoble://
淘寶: taobao://
美團(tuán): imeituan://
支付寶: alipay://
微博: sinaweibo://
知乎: zhihu://
豆瓣fm: doubanradio://
網(wǎng)易公開課: ntesopen://
Chrome: googlechrome://
QQ瀏覽器: mqqbrowser://
uc瀏覽器: ucbrowser://
搜狗瀏覽器: SogouMSE://
百度地圖: baidumap:// bdmap://
優(yōu)酷: youku://
有道詞典: yddictproapp://
QQ音樂:qqmusic://
騰訊視頻:tenvideo://
網(wǎng)易云音樂:orpheus://
應(yīng)用商店
小米商店:"mimarket://details?id=com.xX.XX"
華為商店:"appmarket://details?id=com.xx.xx"
oppo商店:"oppomarket://details?packagename=com.xx.XX"
vivo商店:""vivomarket://details?id=com.xx.Xx"
以使用oppo商店打開美團(tuán)為例
ElevatedButton(
onPressed: () async {
final Uri url = Uri.parse('oppomarket://details?packagename=com.sankuai.meituan');
if (!await launchUrl(url, mode: LaunchMode.externalApplication)) {
throw Exception('Could not launch $url');
}
},
child: const Text("下載美團(tuán)"),
))
要想在應(yīng)用商店打開,需要獲取到包名,需要先下載一個應(yīng)用寶(只要你的應(yīng)用市場支持分享就行)
我這里分享到了微信里,如下圖
打開
復(fù)制鏈接,你會得到這樣的鏈接:文章來源:http://www.zghlxwxcb.cn/news/detail-718658.html
https://a.app.qq.com/o/simple.jsp?pkgname=com.sankuai.meituan&fromcase=70051&g_f=1182517&scenevia=XQYFX
com.sankuai.meituan
這就是對應(yīng)的包名文章來源地址http://www.zghlxwxcb.cn/news/detail-718658.html
到了這里,關(guān)于Flutter:使用url_launcher打開外部瀏覽器、撥打電話、發(fā)送短信、打開第三方app、打開應(yīng)用商店下載應(yīng)用的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!