美文网首页技术好文
Flutter --- url_launcher 使用

Flutter --- url_launcher 使用

作者: 失恋的寡妇 | 来源:发表于2019-11-29 12:54 被阅读0次

在原生开发中,我们通常会用户调用底层的拨打电话、发送邮件、发送信息以及打开网址和打开第三方应用等等,在Flutter也同样支持。下面简单的介绍一下url_launcher这款插件的使用(支持IOS和Android)。

1、将此添加到包的pubspec.yaml文件中:

dependencies:
  url_launcher: ^5.2.7

2、安装软件包:

(1)通过命令行安装软件包
    $ flutter pub get

(2)通过编译器安装软件包(部分编译软件)
    flutter pub get

3、代码中导入包:

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、其他实例:

1、在默认浏览器中打开网址
    http:<URL> , https:<URL>
    e.g:
    http://flutter.io
2、发送邮件
    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、发送信息
    sms:<phone number> 
    e.g:
    sms:5550101234

相关文章

网友评论

    本文标题:Flutter --- url_launcher 使用

    本文链接:https://www.haomeiwen.com/subject/myawwctx.html