Hellom's Studio.

flutter 之如何打开第三方应用

字数统计: 290阅读时长: 1 min
2019/08/02 Share

Flutter 需要打开第三方应用 就需要使用插件

Dart Packages 里 搜索 url_launcher 插件

https://pub.dev/

具体使用可参看官方实例:

https://pub.dev/packages/url_launcher

以下是两个例子:

点击打开浏览器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
e.g.

import 'package:flutter/material.dart';

import 'package:url_launcher/url_launcher.dart'; // 引入第三方插件


class LaunchPage extends StatefulWidget {
_LaunchPageState createState() => _LaunchPageState();
}

class _LaunchPageState extends State<LaunchPage> {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: '如何打开第三方应用',
theme: ThemeData(primarySwatch: Colors.blue),
home: Scaffold(
appBar: AppBar(
title: Text('如何打开第三方应用'),
leading: GestureDetector(
onTap: () {
Navigator.pop(context);
},
child: Icon(Icons.arrow_back),
),
),
body: Column(
children: <Widget>[
RaisedButton(
onPressed: () => _launchURL(),
child: Text('点击打开浏览器'),
),
RaisedButton(
onPressed: () => _openMap(),
child: Text('点击打开地图'),
)
],
),
),
);
}



//打开外部链接
_launchURL() async {
const url = 'https://flutter.dev';
if (await canLaunch(url)) { // 判断是否可以打开
await launch(url);
} else {
throw 'Could not launch $url';
}
}

点击打开地图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
e.g.

//打开地图
_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';
}
}
}
}
CATALOG
  1. 1. 点击打开浏览器
  2. 2. 点击打开地图