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,或者先停止项目,再重新运行