我們通過url_launcher來實現調起電話、短信、外部瀏覽器、外部APP的功能。
pubspec:
url_launcher: ^5.0.3
get_it: ^1.0.3+2
import 'package:flutter/material.dart'; import 'package:url_launcher/url_launcher.dart'; class SuccessScreen extends StatefulWidget { @override _SuccessScreenState createState() => _SuccessScreenState(); } class _SuccessScreenState extends State<SuccessScreen> { //撥打電話 _call() async { const url = 'tel:10086'; if (await canLaunch(url)) { await launch(url); } else { throw 'Could not launch $url'; } } //發送短信 _message() async { const url = 'sms:133224455'; 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("打開外部應用"), ), ], ), ); } }
另外一個封裝好的,但是沒跑起來