我们通过url_launcher来实现调起电话、短信、外部浏览器、外部APP的功能。
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
class SaveLocalDataPage extends StatefulWidget {
SaveLocalDataPage({Key key}) : super(key: key);
_SaveLocalDataPageState createState() => _SaveLocalDataPageState();
}
class _SaveLocalDataPageState extends State<SaveLocalDataPage> {
//拨打电话
_call() async {
const url = 'tel:10000';
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}
//发送短信
_message() async {
const url = 'sms:18868876045';
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}
//打开外部浏览器
_openBrower() async {
const url = 'https://flutter.dev';
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}
//打开外部应运用
_openOtherApp() async {
/**
* weixin://
* alipays://
*/
const url = 'alipays://';
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("url_launchDemo")),
body: Column(
children: <Widget>[
RaisedButton(
onPressed: () {
_call();
},
child: Text("拨打电话"),
),
SizedBox(height: 10),
RaisedButton(
onPressed: () {
_message();
},
child: Text("发送短信"),
),
SizedBox(height: 10),
RaisedButton(
onPressed: () {
_openBrower();
},
child: Text("打开外部浏览器"),
),
SizedBox(height: 10),
RaisedButton(
onPressed: () {
_openOtherApp();
},
child: Text("打开外部应用"),
),
],
),
);
}
}
运行效果如下:
需要注意的是,调起外部APP我没有细讲,大家如果有需求可以参考这篇文章:
https://blog.csdn.net/u011272795/article/details/82786027
以上。