URL Launcher Flutter Example

This article helps you to launch url in external applications using url launcher flutter package. Using this package we can easily interact with other apps such as to make phone call, open url in browser, send email etc.

In this example, we will interact various external applications from our flutter app. URL Launcher is used to open websites, create mail, make calls, open maps etc. We can launch external applications using launch method of URL Launcher package.

launch method takes a string argument which contains a url. This url is formatted by different URL schemes. This URL schemes depend on the mobile OS Platform and installed apps.

 

Add required dependency in your app level build.gradle file

url_launcher: ^5.7.10
 
 

URL Launcher Types

SafeArea(
child: Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
SizedBox(
    height: 15,
),
RaisedButton(
    textColor: Colors.white,
    color: Colors.red,
    child: Text(“Web URL”, style: TextStyle(fontSize: 22)),
    onPressed: () {
        openURL();
    },
),
SizedBox(
    height: 15,
),
RaisedButton(
    textColor: Colors.white,
    color: Colors.amber,
    child: Text(“Email”, style: TextStyle(fontSize: 22)),
    onPressed: () {
        openEmail();
    },
),
SizedBox(
    height: 15,
),
RaisedButton(
    textColor: Colors.white,
    color: Colors.blue,
    child: Text(“Phone Call”, style: TextStyle(fontSize: 22)),
    onPressed: () {
        phoneCall();
        },
    ),
SizedBox(
    height: 15,
),
RaisedButton(
    textColor: Colors.white,
    color: Colors.purple,
    child: Text(“Map”, style: TextStyle(fontSize: 22)),
    onPressed: () {
        openMap();
        },
    ),
SizedBox(
    height: 15,
),
]))),
);
 
 
 

Open URL in browser

openURL() async {
const url = ‘https://codingwithdhrumil.com/’;
if (await canLaunch(url)) {
    await launch(url);
} else {
    throw ‘Could not launch $url’;
}
}
 
url launcher flutter
 
 

Create Email using URL Launcher Flutter

openEmail() async {
launch(
    “mailto:codingwithdhrumil@gmail.com?subject=Test Email&body=Test Description”);
}
 
url launcher flutter
 
 

Make Phone Call

phoneCall() async {
final String phoneUrl = “tel:1234567890”;
if (await canLaunch(phoneUrl)) {
    await launch(phoneUrl);
} else {
    throw ‘Could not launch $phoneUrl’;
}
}
 
 
 

Open Map using URL Launcher Flutter

final String lat = “19.123799”;
final String lng = “72.817622”;

openMap() async {
final String googleMapsUrl = “comgooglemaps://?center=$lat,$lng”;
final String appleMapsUrl = “https://maps.apple.com/?q=$lat,$lng”;

if (await canLaunch(googleMapsUrl)) {
    await launch(googleMapsUrl);
}
if (await canLaunch(appleMapsUrl)) {
    await launch(appleMapsUrl, forceSafariVC: false);
} else {
    throw “Couldn’t launch URL”;
}
}

 

Leave a Reply