Flutter: Use url_launcher to open an external browser, make a call, send a text message, open a third-party app, open an app store to download an app

foreword

In Flutter url_launcheris a plugin for opening URLs. It allows actions such as opening URLs, sending emails, making phone calls, etc. within a Flutter application. Using url_launcherplugins, it is easy to integrate various URL manipulations in your application.

Official address
https://pub-web.flutter-io.cn/packages/url_launcher

Install

flutter pub add url_launcher

basic use

open url

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("打开浏览器"),
        )
)

There are two caveats here:

  • Can't run normally in the simulator, you need to use a real machine
  • Must be set to mode:LaunchMode.externalApplication, otherwise a separate interface will be opened in the application for display

e-mail

Center(
          child: ElevatedButton(
        onPressed: () async {
    
    
          // 收件人邮箱
          String recipient = "[email protected]";
          //邮件主题
          String subject = "邮件主题";
          // 邮件内容
          String body = "邮件内容";
          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("发邮件"),
      ))

insert image description here
insert image description here
text message

Center(
          child: ElevatedButton(
        onPressed: () async {
    
    
          // 收件人电话
          String recipient = "10086";
          // 短信内容
          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("发短信"),
      ))

insert image description hereinsert image description here

Open third-party 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("打开微信"),
      ))

WeChat can be opened normally. The following are collected from the Internet. I don’t know if it is easy to use.

QQ: mqq:// 
微信: weixin:// 
京东: openapp.jdmoble:// 
淘宝: taobao:// 
美团: imeituan:// 
支付宝: alipay:// 
微博: sinaweibo:// 
知乎: zhihu:// 
豆瓣fm: doubanradio:// 
网易公开课: ntesopen:// 
Chrome: googlechrome:// 
QQ浏览器: mqqbrowser:// 
uc浏览器: ucbrowser:// 
搜狗浏览器: SogouMSE:// 
百度地图: baidumap:// bdmap:// 
优酷: youku:// 
有道词典: yddictproapp:// 
QQ音乐:qqmusic://
腾讯视频:tenvideo://
网易云音乐:orpheus://

app Store

小米商店:"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"

Take using the oppo store to open meituan as an example

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("下载美团"),
      ))

insert image description here

If you want to open it in the app store, you need to get the package name, and you need to download a App Store first (as long as your app market supports sharing).
insert image description here
I shared it in WeChat here, as shown in the figure below
insert image description here
to open

insert image description here
Copy the link and you'll get a link like this:

https://a.app.qq.com/o/simple.jsp?pkgname=com.sankuai.meituan&fromcase=70051&g_f=1182517&scenevia=XQYFX

com.sankuai.meituanThis is the corresponding package name

Guess you like

Origin blog.csdn.net/weixin_41897680/article/details/131991811