小编典典

如何从Flutter应用程序打开应用程序?

flutter

我正在开发Flutter应用,需要在其中向用户显示导航。因此,如何像在Android中使用外部意图那样从Flutter应用程序中打开地图应用程序?

还是他们需要任何Flutter插件?


阅读 454

收藏
2020-08-13

共1个答案

小编典典

我建议您使用url_launcher飞镖包。

这样,您可以使用所有url模式打开(phone,,sms甚至maps根据您的情况)。

为了在Android和iOS中打开Goog​​le Maps,您可以使用Hemanth Raj建议的常规Android Maps
URI模式

_openMap() async {
    const url = 'https://www.google.com/maps/search/?api=1&query=52.32,4.917';
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      throw 'Could not launch $url';
    }
  }

如果要在Android上做出选择,则可以使用常规geo:URI模式

如果要专门打开iOS Maps API,则可以使用Cupertino Maps
URI模式

如果您选择区分Android和iOS(未在所有平台上都使用Google Maps Api模式),则还必须在打开的地图调用中按以下方式进行操作:

_openMap() async {
    // Android
    const url = 'geo:52.32,4.917';
    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';
      }
    }
  }

或者,您可以在运行时使用dart.ioPlatform检查操作系统:

import 'dart:io';

_openMap() async {
    // Android
    var url = 'geo:52.32,4.917';
    if (Platform.isIOS) {
      // iOS
      url = 'http://maps.apple.com/?ll=52.32,4.917';
    }
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      throw 'Could not launch $url';
    }
  }

现在,我完成了软管维护(真正的任务……没有一些代码重构……^^’),我可以结束我的回答了。

正如我在开始使用url_launcher告诉您的那样,您可以使用所有URI模式来进行呼叫,发送短信,发送电子邮件等。

这里有一些代码可以做到这一点:

_sendMail() async {
    // Android and iOS
    const uri = 'mailto:test@example.org?subject=Greetings&body=Hello%20World';
    if (await canLaunch(uri)) {
      await launch(uri);
    } else {
    throw 'Could not launch $uri';
    }
  }

  _callMe() async {
    // Android
    const uri = 'tel:+1 222 060 888';
    if (await canLaunch(uri)) {
      await launch(uri);
    } else {
      // iOS
      const uri = 'tel:001-22-060-888';
      if (await canLaunch(uri)) {
        await launch(uri);
      } else {
        throw 'Could not launch $uri';
      }
    }
  }

  _textMe() async {
    // Android
    const uri = 'sms:+39 349 060 888';
    if (await canLaunch(uri)) {
      await launch(uri);
    } else {
      // iOS
      const uri = 'sms:0039-222-060-888';
      if (await canLaunch(uri)) {
        await launch(uri);
      } else {
        throw 'Could not launch $uri';
      }
    }
  }

即使URI模式应该是标准(RFC)有时authoritypath他们的部分可能的框架(Android或iOS)之间的差异。

因此,在这里我例外地管理不同的操作系统,但是,您可以使用dart.ioPlatform更好地做到这一点:

import 'dart:io'

然后在代码中:

if (Platform.isAndroid) {

} else if (Platform.isIOS) {

}

我建议您始终在两种环境中对其进行测试。

您可以在此处检查Android和iOS架构文档:

如果您想在Android中类似于startActivity(但仅适用于Android平台),则可以使用dart包android_intent

2020-08-13