首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >Flutter 中使用url_launcher打开外部浏览器 、打开外部应用、拨打电话、发送短信、发送邮件

Flutter 中使用url_launcher打开外部浏览器 、打开外部应用、拨打电话、发送短信、发送邮件

作者头像
越陌度阡
发布2021-02-04 09:59:38
发布2021-02-04 09:59:38
6.7K4
举报

1. 安装插件

代码语言:javascript
复制
dependencies:
  flutter:
    sdk: flutter
  flutter_localizations:
    sdk: flutter
  date_format: ^1.0.6
  flutter_cupertino_date_picker: ^1.0.26+2 
  flutter_swiper: ^1.1.6
  fluttertoast: ^7.1.6
  http: ^0.12.2
  dio: ^3.0.10
  flutter_html: ^1.1.0
  flutter_inappwebview: ^4.0.0+4
  device_info: ^1.0.0
  amap_location: ^0.2.0
  image_picker: ^0.6.7+21
  video_player: ^1.0.1
  chewie: ^0.12.2
  connectivity: ^2.0.2
  shared_preferences: ^0.5.12+4
  barcode_scan_fix: ^1.0.2
  package_info: ^0.4.3+2
  path_provider: ^1.6.27
  open_file: ^3.0.3
  flutter_downloader: ^1.5.2
  
  # 打开外部应用
  url_launcher: ^5.7.10

在pubspec.yaml中配置保存后,在VS Code环境中会自动下载依赖包。

如果无法正常下载,执行 flutter pub get 。

2. 引入插件

在需要用到的该插件的文件中引入插件包。

代码语言:javascript
复制
import 'package:url_launcher/url_launcher.dart';

3. 使用插件

代码语言:javascript
复制
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';

class UrlLauncherPage extends StatefulWidget {
    UrlLauncherPage({Key key}) : super(key: key);
    _UrlLauncherState createState() => _UrlLauncherState();

}

class _UrlLauncherState extends State<UrlLauncherPage> {
    @override
    Widget build(BuildContext context) {
        return Scaffold(
            appBar: AppBar(
                title: Text('UrlLauncher'),
            ),
            body: Center(
                child: Padding(
                    padding: EdgeInsets.all(20),
                    child: ListView(
                        children: [
                            RaisedButton(
                                child: Text('打开外部浏览器'),
                                onPressed: () async{   
                                    // 苹果App升级用的此方式
                                    // 前提是首先获取App在苹果里面的地址                              
                                    const url = 'https://cflutter.com';
                                    if (await canLaunch(url)) {
                                        await launch(url);
                                    } else {
                                        throw 'Could not launch $url';
                                    }
                                },
                            ),
                            SizedBox(height: 10),
                            RaisedButton(
                                child: Text('拨打电话'),
                                onPressed: () async{
                                    // 协议格式:tel:<phone number>
                                    var tel = 'tel:10086';
                                    if (await canLaunch(tel)) {
                                        await launch(tel);
                                    } else {
                                        throw 'Could not launch $tel';
                                    }
                                },
                            ),
                            SizedBox(height: 10),
                            RaisedButton(
                                child: Text('发送短信'),
                                onPressed: () async{
                                    // 协议格式:sms:<phone number>
                                    var tel = 'sms:10086';
                                    if (await canLaunch(tel)) {
                                        await launch(tel);
                                    } else {
                                        throw 'Could not launch $tel';
                                    }
                                },
                            ),
                            SizedBox(height: 10),
                            RaisedButton(
                                child: Text('打开外部应用'),
                                onPressed: () async{
                                    var url = 'alipays://';
                                    if (await canLaunch(url)) {
                                        await launch(url);
                                    } else {
                                        throw 'Could not launch $url';
                                    }
                                },
                            ) ,
                            SizedBox(height: 10),
                            RaisedButton(
                                child: Text('发送邮件'),
                                onPressed: () async{
                                    // 协议格式:mailto:<email address>?subject=<subject>&body=<body>
                                    var url = 'mailto:superluo666@gmail.com?subject=Test&body=测试';
                                    if (await canLaunch(url)) {
                                        await launch(url);
                                    } else {
                                        throw 'Could not launch $url';
                                    }
                                },
                            )        
                        ]
                    ),
                )
            )
        );
    }
}

打开其它应用时,都是改变相应的url协议地址即可,跳转原理参照原生开发使用的url scheme,常用的如下:

微信: weixin://

京东: openapp.jdmoble://

淘宝: taobao://

Chrome: googlechrome://

百度地图: baidumap://

高德地图:androidamap://、iosamap://

效果图如下:

参考:

https://pub.flutter-io.cn/packages/url_launcher

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021/01/31 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 安装插件
  • 2. 引入插件
  • 3. 使用插件
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档