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

1. 安装插件

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. 引入插件

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

import 'package:url_launcher/url_launcher.dart';

 

3. 使用插件


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 {
    @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:
                                    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:
                                    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:?subject=&body=
                                    var url = 'mailto:[email protected]?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://

效果图如下:

Flutter 中使用url_launcher打开外部浏览器 、打开外部应用、拨打电话、发送短信、发送邮件_第1张图片

参考:

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

你可能感兴趣的:(Flutter)