X
    Categories: Flutter

Flutter URL launcher | Website, Call, SMS, Email

 

Flutter URL launcher :

Flutter URL launcher is used to open websites, create a email, send a sms and also can make a call.We use them in our applications every now and then.

In General a launcher is a program or tool that allows users to easily and quickly launch other programs or applications on their device. Launchers are commonly used on desktop and mobile operating systems to provide a centralized location for accessing frequently used applications, settings, and other features.

Now a days every app will notify the user with the task updates using  sms and email’s.We can integrate sms and email within the app to send them directly to users.

Just like you move in between screens in your app you can make use of this plugin to make the required functionality work with flutter url launcher.

We can make the use of predefined library flutter url launcher so as to make the launchers much easily implemented in our app and can get the tested code instead of re coding.

In this tutorial we will be dealing opening a website, making a call, sending an email and also a sending a email in detail can also refer below video tutorial.

 

Flutter URL launcher video tutorial :

Check out the tutorial below to grasp the usage more easily.

 

Project Structure :

This image illustrates the project structure of URL Launcher.

 

pubspec.yaml :

Add url_launcher dependency and update the version accordingly to avoid any unnecessary code level issues.

dependencies:
  flutter:
    sdk: flutter
  url_launcher: ^5.5.0

 

 

main.dart :

Open a website :

Using a launch we can specify the website to be opened

launch("http://google.com"),

 

 

Make a call :

Launch dialer with the provided number, specify tel:// to make the launcher as call

launch("tel://123"),

 

 

Send a Email :

Send a email by specifying email address of the receiver, subject and message included in body.

launch( "mailto:abhi@androidcoding.in?subject=Hi&body=How are you%20plugin"),

 

 

Send a SMS :

Send a sms using launch by specifying SMS and providing the number.

launch("sms:123456789"),

 

 

Card Design :

Card(
  shape: RoundedRectangleBorder(
    side: BorderSide(color: Colors.white, width: 2.0),
    borderRadius: BorderRadius.circular(25.0),
  ),
),

 

 

Specifying components inside the Card as

child: Column(
  crossAxisAlignment: CrossAxisAlignment.stretch,
  children: <Widget>[
    Padding(
      padding: const EdgeInsets.all(30.0),
      child: FlatButton.icon(
        icon: Icon(Icons.sms),
        label: Text(
          "Write a SMS",
          style: TextStyle(fontSize: 25),
        ),
        onPressed: () => launch("sms:123456789"),
      ),
    ),
  ],
),

 

 

Flutter URL launcher Fullcode :

Find the below code to implement url launcher in your app.

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

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: Home(),
    );
  }
}

class Home extends StatelessWidget {
  Home({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Launchers"),
      ),
      body: Center(
        child: Padding(
          padding: const EdgeInsets.all(20.0),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Card(
                color: Colors.white70,
                shape: RoundedRectangleBorder(
                  side: BorderSide(color: Colors.white, width: 2.0),
                  borderRadius: BorderRadius.circular(25.0),
                ),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.stretch,
                  children: <Widget>[
                    Padding(
                      padding: const EdgeInsets.all(30.0),
                      child: FlatButton.icon(
                        icon: Icon(Icons.add_to_home_screen),
                        label: Text(
                          "Open Website",
                          style: TextStyle(fontSize: 25),
                        ),
                        onPressed: () => launch("http://google.com"),
                      ),
                    ),
                  ],
                ),
              ),
              SizedBox(height: 10.0),
              Card(
                shape: RoundedRectangleBorder(
                  side: BorderSide(color: Colors.white, width: 2.0),
                  borderRadius: BorderRadius.circular(25.0),
                ),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.stretch,
                  children: <Widget>[
                    Padding(
                      padding: const EdgeInsets.all(30.0),
                      child: FlatButton.icon(
                        icon: Icon(Icons.call),
                        label: Text(
                          "Make a Call",
                          style: TextStyle(fontSize: 25),
                        ),
                        onPressed: () => launch("tel://123"),
                      ),
                    ),
                  ],
                ),
              ),
              SizedBox(height: 10.0),
              Card(
                color: Colors.white70,
                shape: RoundedRectangleBorder(
                  side: BorderSide(color: Colors.white, width: 2.0),
                  borderRadius: BorderRadius.circular(25.0),
                ),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.stretch,
                  children: <Widget>[
                    Padding(
                      padding: const EdgeInsets.all(30.0),
                      child: FlatButton.icon(
                        icon: Icon(Icons.email),
                        label: Text(
                          "Send a Email",
                          style: TextStyle(fontSize: 25),
                        ),
                        onPressed: () => launch(
                            "mailto:abhi@androidcoding.in?subject=Hi&body=How are you%20plugin"),
                      ),
                    ),
                  ],
                ),
              ),
              SizedBox(height: 10.0),
              Card(
                shape: RoundedRectangleBorder(
                  side: BorderSide(color: Colors.white, width: 2.0),
                  borderRadius: BorderRadius.circular(25.0),
                ),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.stretch,
                  children: <Widget>[
                    Padding(
                      padding: const EdgeInsets.all(30.0),
                      child: FlatButton.icon(
                        icon: Icon(Icons.sms),
                        label: Text(
                          "Write a SMS",
                          style: TextStyle(fontSize: 25),
                        ),
                        onPressed: () => launch("sms:123456789"),
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

 

 

Flutter URL launcher output :

This screen depicts the usage of flutter URL launcher

If you have any query on this tutorial on url launcher do let us know in the comment section below.If you like this tutorial do like an share this tutorial for more interesting tutorials.

abhishek:
Related Post

This website uses cookies.