如何打开第三方应用?

33 篇文章 2 订阅

1、在flutter里面打开第三方应用可以借助插件url_launcher

这个插件是flutter团队为我们提供的,这个插件是用于在flutter里面打开第三方app的

2、

添加依赖,然后点击 Packages get进行安装

3、

引入头文件

4、

打开浏览器

5、运行结果:

6、

打开第三方应用

在打开第三方应用的时候,首先要知道该app的schema或者它的url

 

添加打开地图的方法:

7、

调用打开地图的方法

8、运行结果:

 

9、

代码:

import 'package:flutter/material.dart';
import 'package:flutter_color_plugin/flutter_color_plugin.dart';
import 'package:url_launcher/url_launcher.dart';
void main() => runApp(LaunchPage());

//如何打开第三方应用?
class LaunchPage extends StatefulWidget {
  @override
  _LaunchPageState createState() => _LaunchPageState();
}

class _LaunchPageState extends State<LaunchPage> {
  
  _launchURL() async {
    const url = 'https://flutter.io';
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      throw 'Could not launch $url';
    }
  }

  _openMap() async{
    //Android
    const url= 'geo:52.32,4.917';//App提供者提供的  schema
    if(await canLaunch(url)){
      await launch(url);
    }else{
      //iOS
      const url='http://maps.apple.com/?ll=52.32,4.917';
      if(await canLaunch(url)){
        await launch(url);
      }else{
        throw 'Could not launch $url';
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('如何打开第三方应用?'),
        leading:GestureDetector(
          onTap: (){
            Navigator.pop(context);
          },
          child: Icon(Icons.arrow_back),
        )
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            RaisedButton(
              onPressed: ()=>_launchURL(),
              child: Text('打开浏览器'),
            ),
            RaisedButton(
              onPressed: ()=>_openMap(),
              child: Text('打开地图'),
            )
          ],
        ),
      ),

    );


  }
}

 

注:

出现的异常:

1、Unhandled Exception: MissingPluginException(No implementation found for method canLaunch on channel plugins.flutter.io/url_launcher)

解决办法:直接重启Android  Studio,或者先停止项目,再重新运行

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值