Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
125 views
in Technique[技术] by (71.8m points)

How to open an application from a Flutter app?

I am developing a Flutter app, in which I need to show navigation to the user for a place. So, how can I open a map application from my Flutter app like we do using external intent in Android?

Or their is any flutter-plugin for it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I suggest you to use url_launcher dart package.

In this way you can use all url schemas to open (phone, sms, and even maps as in your case).

In order to open Google Maps either in Android and iOS you could use the general Android Maps URI schema as suggested by Hemanth Raj.

_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';
    }
  }

If you want to give a choice on Android you could use the general geo: URI schema.

If you want specifically open iOS Maps API you can use Cupertino Maps URI schema.

If you choose to distinguish between Android and iOS (not using Google Maps Api schema for all platform) you have to do it also in your open map call in a way like this:

_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';
      }
    }
  }

Or you can check the OS at runtime with dart.io library Platform class:

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';
    }
  }

Now that I finished hosekeeping (the real one... not some code refactoring... ^^') I can finish my answer.

As I tell you at the beginning with url_launcher you can use all URI Schemas in order to call, send sms, send e-mail etc.

Here some code to do that:

_sendMail() async {
    // Android and iOS
    const uri = 'mailto:[email protected]?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';
      }
    }
  }

Even if URI schema should be standards (RFC) sometimes the authority and path parts of them could differ between frameworks (Android or iOS).

So here I manage the different OSes with exception but, you could do it better with dart.io library Platform class:

import 'dart:io'

and then in code:

if (Platform.isAndroid) {

} else if (Platform.isIOS) {

}

I suggest you always test them in both environments.

You can check the Android and iOS schema documenation here:

If you wanna something similar to startActivity in Android (but that works only for Android platform) you can use the dart package android_intent.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...