flutter 调用 url_launcher 模块打开外部浏 览器 打开外部应用 拨打电话 发送短信

Flutter url_launcher 模块简介

// url_launcher 是一个Flutter模块,用于实现各种外部调用功能。
// 它可以帮助开发者实现如打开外部浏览器、启动其他应用、发送短信、拨打电话等功能。
https://pub.dev/packages/url_launcher

// 关于如何使用 url_launcher 打开其他应用,可以参考下面的帖子。
https://www.cflutter.com/topic/5d0853733b57e317a4d0af01

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 {
                  // 可以通过修改以下URL来启动不同的应用。如:
                  // weixin:// 启动微信
                  // alipays:// 启动支付宝
                  var url = 'alipays://';
                  if (await canLaunch(url)) {
                    await launch(url);
                  } else {
                    throw 'Could not launch $url';
                  }
                },
              )
            ],
          ),
        ),
      ),
    );
  }
}

posted on 2020-02-27 23:08  完美前端  阅读(639)  评论(0)    收藏  举报

导航