Flutter --- url_launcher 使用

在原生開發(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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容