共计 1224 个字符,预计需要花费 4 分钟才能阅读完成。
类似于安卓的通过设置 intent-action,点击按钮就能跳转到系统的拨号,发短信,发邮件等界面
引入类库
url_launcher: ^5.0.3
get_it: ^1.0.3+2
创建一个服务
import ‘package:url_launcher/url_launcher.dart’; class CallsAndMessagesService {void call(String number) => launch(“tel:$number”); void sendSms(String number) => launch(“sms:$number”); void sendEmail(String email) => launch(“mailto:$email”);}
初始化,注册服务,在 app 中任何地方都能调用
GetIt locator = GetIt(); void setupLocator() {locator.registerSingleton(CallsAndMessagesService());}
在主方法中设置
void main() { setupLocator(); runApp(MyApp());}
在需要的用到的地方创建
class _MyHomePageState extends State<MyHomePage> {final CallsAndMessagesService _service = locator<CallsAndMessagesService>(); final String number = “123456789”; final String email = “dancamdev@example.com”; …}
调用代码及界面
@override Widget build(BuildContext context) {return Scaffold( appBar: AppBar( title: Text(‘dancamdev’), ), body: Container(width: double.infinity, child: Column( mainAxisAlignment: MainAxisAlignment.center, mainAxisSize: MainAxisSize.max, children: <Widget>[ RaisedButton( child: Text( “call $number”,), onPressed: () => _service.call(number), ), SizedBox(height: 20), RaisedButton(child: Text( “message $number”,), onPressed: () => _service.sendSms(number), ), SizedBox(height: 20), RaisedButton(child: Text( “email $email”,), onPressed: () => _service.sendEmail(email), ), ], ), ), );