Hi Guys, Welcome to Proto Coders Point.
In this flutter tutorial, we will make use of url_launcher external package to perform below event.
- flutter make a phone call.
- flutter launch URL – Load a Website.
- flutter send SMS with message to a number.
- flutter send email with subject & body message.
So Let’s Begin
Here’s a complete video tutorial of URL_LAUNCHER.
url_launcher flutter package
Flutter url_launcher is a plugin for your flutter project, if you want to add feature in your app such as making a phone call, send a SMS, Email, opening a website(url launch) by taking input from your flutter app users.
Eg:
Make a call: Your app user can simply select a phone number from a list in app and make a direct call in flutter.
Send Email: User can select to whom to send email & enter text in subject & email body and send it.
Open URL from flutter app: Suppose your app display list of website address then user can simply select which website he want to visit and open the website.
Send SMS: A user can send SMS to a number with a message.
Installation of url_launcher
In flutter project open pubspec.yaml file & under dependencies section add url launcher package.
dependencies: url_launcher: # latest version
& hit put get button to download and add it.
IOS Setup
Add LSApplicationQueriesSchemes entries in your Info.plist file.
Project > ios > Runner > Info.plist
<key>LSApplicationQueriesSchemes</key> <array> <string>https</string> <string>http</string> </array>
Android Setup
A <queries> element should be added in AndroidManifest.xml
If your app is using https, tel and mailto then add below queries in manifest file.
Project > Android > app > src > main > AndroidManifest.xml
<queries> <!-- If your app opens https URLs --> <intent> <action android:name="android.intent.action.VIEW" /> <data android:scheme="https" /> </intent> <!-- If your app makes calls --> <intent> <action android:name="android.intent.action.DIAL" /> <data android:scheme="tel" /> </intent> <!-- If your app emails --> <intent> <action android:name="android.intent.action.SEND" /> <data android:mimeType="*/*" /> </intent> </queries>
Import url_launcher.dart
Once you have added the package as external library and make all the platform setup, now you can simply use it just by importing it any where in your project Eg: main.dart
import 'package:url_launcher/url_launcher.dart';
How to use url launcher package in flutter
1] Open a URL in default browser – flutter
http://<url>, https://<url>; eg: https://protocoderspoint.com
launch('https://protocoderspoint.com/');
2] Flutter Make a Phone Call
tel:<phone number>; Eg: tel:+91 8755***8685
launch('tel:+91 88888888888');
3] Flutter send Email with subject and body
mailto:<email address>?subject=<subject>&body=<body>
, e.g. mailto:smith@example.org?subject=News&body=New%20plugin
launch('mailto:rajatrrpalankar@gmail.com?subject=This is Subject Title&body=This is Body of Email');
4] Flutter send SMS with message
sms:<phone number>?body=<message>
, e.g. sms:5550101234
launch('sms:+91888888888?body=Hi Welcome to Proto Coders Point');
Complete Code flutter url launcher
main.dart
import 'package:flutter/material.dart'; import 'package:url_launcher/url_launcher.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { // This widget is the root of your application. @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', debugShowCheckedModeBanner: false, theme: ThemeData( primarySwatch: Colors.blue, ), home: MyHomePage(), ); } } class MyHomePage extends StatefulWidget { const MyHomePage({Key? key}) : super(key: key); @override _MyHomePageState createState() => _MyHomePageState(); } class _MyHomePageState extends State<MyHomePage> { @override Widget build(BuildContext context) { return Scaffold( body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ ElevatedButton( onPressed: (){ launch('tel:+91 88888888888'); },child: Text("Make A Call"), ), ElevatedButton( onPressed: (){ launch('sms:+91888888888?body=Hi Welcome to Proto Coders Point'); },child: Text("Send A SMS"), ), ElevatedButton( onPressed: (){ launch('mailto:rajatrrpalankar@gmail.com?subject=This is Subject Title&body=This is Body of Email'); },child: Text("Send A Email"), ), ElevatedButton( onPressed: (){ launch('https://protocoderspoint.com/'); },child: Text("Open a URL"), ), ], ), ) ); } }