Flutterexperts

Empowering Vision with FlutterExperts' Expertise
URL launcher in Flutter

While developing mobile applications, there are so many times when we have to interact with outside of your applications. So to achieve this Flutter provides an easy way by usingurl_launcher package.

In this article, we’ll show how to interact with another application from your app

url_launcher | Flutter Package
Flutter plugin for launching a URL on Android and iOS. Supports web, phone, SMS, and email schemes.pub.dev


Table of Contents :

Introduction

Setup

Code Implementation

Code File

Conclusion


Introduction :

Flutter is Google’s UI toolkit for building beautiful, natively compiled applications for mobile, web, and desktop from a single codebase. URL launcher is used to open websites, create mail, open maps, etc.

To interact with outsider apps from your app we need to use a URL launcher package.

Setup

Step1: Add the dependency

Start by adding the dependency to the pubspec.yml file

dependencies:
url_launcher: latest version

Supported URL schemas

The launch method takes a string argument containing a URL. This URL can be formatted using a number of different URL schemes. The supported URL schemes depend on the underlying platform and installed apps.

Code Implementation:

Create three buttons to launch web URL, mail, and map in the MyHomePage.dart class, and on clicking that button showing the corresponding launch.

For opening the web pages, on click of button Open URL call this method,

For Google map and apple map, pass latitude and longitude in the base url,

Sent an email by specifying the receiver’s email, subject, and body of the mail

In these snippets, we are using url_launcher to open outsider app like email, webpage, and map.

Code File

import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';

class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
final String lat = "25.3622";
final String lng = "86.0835";

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Url Launcher Demo'),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Card(
color: Colors.blue,
shape: RoundedRectangleBorder(
side: new BorderSide(color: Colors.blue, width: 2.0),
borderRadius: BorderRadius.circular(15.0),
),
child: Padding(
padding: const EdgeInsets.all(13.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(Icons.open_in_browser, color: Colors.white,),
SizedBox(width: 5,),
InkWell(
onTap: _launchURL,
child: Text('Open Web url',style: TextStyle(
color: Colors.white
),),
),
],
),
),
),

Card(
color: Colors.blue,
shape: RoundedRectangleBorder(
side: new BorderSide(color: Colors.blue, width: 2.0),
borderRadius: BorderRadius.circular(15.0),
),
child: Padding(
padding: const EdgeInsets.all(13.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(Icons.email_outlined, color: Colors.white,),

SizedBox(width: 5,),
InkWell(
onTap: _launchEmail,
child: Text('Open email',style: TextStyle(
color: Colors.white
),),
),
],
),
),
),

Card(
color: Colors.blue,
shape: RoundedRectangleBorder(
side: new BorderSide(color: Colors.blue, width: 2.0),
borderRadius: BorderRadius.circular(15.0),
),
child: Padding(
padding: const EdgeInsets.all(13.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(Icons.navigation_outlined, color: Colors.white,),

SizedBox(width: 5,),
InkWell(
onTap: _launchMap,
child: Text('Open map',style: TextStyle(
color: Colors.white
),),
),
],
),
),
),
],
));
}

_launchMap() 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";
}
}

_launchEmail() async {
launch(
"mailto:rakhi@aeologic.com?subject=TestEmail&body=How are you%20plugin");
}

_launchURL() async {
const url = 'https://flutterdevs.com/';
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}
}

Conclusion

In this article, I have explained URL launcher package demo which you can modify and experiment with according to your own. This little introduction was about to open an outsider app from my app like a map, webpage, and email.

I hope this blog will provide you with sufficient information in trying up to use url_launcher in your flutter projects. We will show this demo program for working url launcher in your flutter applications. So please try it.

❤ ❤ Thanks for reading this article ❤❤

If I got something wrong? Let me know in the comments. I would love to improve.

Clap 👏 If this article helps you.


Feel free to connect with us:
And read more articles from FlutterDevs.com.

FlutterDevs team of Flutter developers to build high-quality and functionally-rich apps. Hire a flutter developer for your cross-platform Flutter mobile app project on an hourly or full-time basis as per your requirement! You can connect with us on Facebook, GitHub, Twitter, and LinkedIn for any flutter related queries.

We welcome feedback and hope that you share what you’re working on using #FlutterDevs. We truly enjoy seeing how you use Flutter to build beautiful, interactive web experiences.


Leave comment

Your email address will not be published. Required fields are marked with *.