zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

Flutter进阶第13篇: 打开外部浏览器、打开外部应用、拨打电话、发送短信

flutter应用浏览器 打开 进阶 发送 13 外部
2023-09-14 09:04:26 时间

效果图:

在这里插入图片描述

打开外部浏览器
在这里插入图片描述
发送短信
在这里插入图片描述
拨打电话
在这里插入图片描述
打开外部应用
在这里插入图片描述

导入第三方库:url_launcher

import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';

class UrlLauncher extends StatefulWidget {
  UrlLauncher({Key key}) : super(key: key);

  _UrlLauncherState createState() => _UrlLauncherState();
}

class _UrlLauncherState extends State<UrlLauncher> {
  @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{                                 
                  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{
                  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{
                 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{
                  /*
                    weixin://
                    alipays://
                  */
                  var url = 'alipays://';	//支付宝的 scheme码
                  if (await canLaunch(url)) {
                    await launch(url);
                  } else {
                    throw 'Could not launch $url';
                  }
                  
              },
            )       
          ]),
        )));
  }
}

打开其他APP的scheme码:
https://www.cflutter.com/topic/5d0853733b57e317a4d0af01