url_launcher Package - Lets Launch URL, Send Email, Make Call or Send SMS in Flutter

This Article is posted by seven.srikanth at 7/23/2019 4:38:02 AM



Hi All,

Lets see how to Launch URL, Send Email, Make Call or Send SMS in Flutter. 

Here is what you can expect from this example. 




Add this to your dependencies in pubspec.yaml file.

url_launcher: any

Here is the full code of this sample. In order to run this, copy-paste this code into main.dart file in your newly created flutter project. 

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

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
var title = "UrlLauncher Demo";
return MaterialApp(
title: title,
home: Scaffold(
appBar: AppBar(
title: Text(title),
),
body: Home(),
),
);
}
}

class Home extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(18.0),
child: Center(
child: Column(
children: <Widget>[
RaisedButton(
onPressed: _launchurl,
child: Text('Goto FlutterCentral'),
),
RaisedButton(
onPressed: _createEmail,
child: Text('Email to FlutterCentral'),
),
RaisedButton(
onPressed: _makeCall,
child: Text('Make a Call'),
),
RaisedButton(
onPressed: _sendSMS,
child: Text('Send an SMS'),
),
],
),
),
);
}

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

void _createEmail() async{
const emailaddress = 'mailto:admin@fluttercentral.com?subject=Sample Subject&body=This is a Sample email';

if(await canLaunch(emailaddress)) {
await launch(emailaddress);
} else {
throw 'Could not Email';
}
}

void _makeCall() async{
const phonenumber = "tel:9999999";

if(await canLaunch(phonenumber)) {
await launch(phonenumber);
} else {
throw 'Could not call';
}
}

void _sendSMS() async{
const phonenumber = "sms:9999999";

if(await canLaunch(phonenumber)) {
await launch(phonenumber);
} else {
throw 'Could not SMS';
}
}
}


Hope this is helpful to you.

Thanks,
Srikanth



Tags: How to Launch URL; How to Send Email; How to Make Call; How to Send SMS;








0 Comments
Login to comment.
Recent Comments

Be the first to Comment. You can ask a Query or Share your toughts or Just say thanks.


© 2018 - Fluttercentral | Email to me - seven.srikanth@gmail.com