url_launcher in Flutter.

Many times, an app wants to launch another app present on the user’s device to accomplish a task. For example, if your app has a “contact us” option, then you might give your app users options like email, text message, or call to contact. Then in this scenario, your app can simply launch an app already present on the user’s device to accomplish this. And it is achieved by just calling a single method from the url_launcher flutter package.

How to use url_launcher in Flutter?

I’m assuming that you have already created a new flutter project.

Now, you need to add the url_launcher dependency to your flutter project. To do this, add the below lines in your pubspec.yaml file.

dependencies:
  flutter:
    sdk: flutter
  url_launcher: ^6.1.5

Now, to run it on android devices you add any URL schemes you pass to canLaunchUrl as <queries> entries in your AndroidManifest.xml, otherwise, it will return false in most cases starting on Android 11 (API 30) or higher. To do this, open  android/app/src/main/AndroidManifest.xml and add the <queries> element as a child of the root element (See the modification below).

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.url_launcher_example">

      <queries>
        <!-- To open phone's dialer app -->
        <intent>
            <action android:name="android.intent.action.DIAL" />
            <data android:scheme="tel" />
        </intent>
        <!-- To send SMS messages -->
        <intent>
            <action android:name="android.intent.action.SENDTO" />
            <data android:scheme="smsto" />
        </intent>
        <!-- To open email app -->
        <intent>
            <action android:name="android.intent.action.SEND" />
            <data android:mimeType="*/*" />
        </intent>
          <!-- To opens https URLs -->
        <intent>
            <action android:name="android.intent.action.VIEW" />
            <data android:scheme="https" />
        </intent>
    </queries>
  <application>
        ...
    </application>
</manifest>

Now it’s time to add the main code. To make a phone call using the default phone app use the code below.

To send an SMS message using the default messaging app.

To create email in the default email app.

To open URL.

Now let’s see the full code.

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

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'url_launcher example',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color.fromARGB(255, 244, 204, 234),
      appBar: AppBar(title: Text('url_launcher example')),
      body: Padding(
          padding: EdgeInsets.all(25.0),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              const Text('Contact us',
                  style: TextStyle(
                      color: Colors.white,
                      fontSize: 35.0,
                      fontWeight: FontWeight.bold)),
              const SizedBox(height: 22.0),
              TextButton(
                onPressed: () async {
                  final url = Uri.parse('tel:+123 456 789');
                  if (await canLaunchUrl(url)) {
                    await launchUrl(url);
                  } else {
                    throw 'Could not launch $url';
                  }
                },
                child: Row(
                  mainAxisSize: MainAxisSize.min,
                  children: const [
                    Icon(
                      Icons.phone,
                      color: Color.fromARGB(255, 106, 130, 238),
                      size: 30,
                    ),
                    SizedBox(width: 18.0),
                    Text('+123 456 789',
                        style: TextStyle(
                            color: Color.fromARGB(255, 42, 225, 137),
                            fontSize: 18.0,
                            fontWeight: FontWeight.w500)),
                  ],
                ),
              ),
              TextButton(
                onPressed: () async{
                  final url = Uri.parse('sms:+123 456 789');
                  if (await canLaunchUrl(url)) {
                    await launchUrl(url);
                  } else {
                    throw 'Could not launch $url';
                  }
                },
                child: Row(
                  mainAxisSize: MainAxisSize.min,
                  children: const [
                    Icon(
                      Icons.sms,
                      color: Color.fromARGB(255, 106, 130, 238),
                      size: 30,
                    ),
                    SizedBox(width: 18.0),
                    Text('+123 456 789',
                        style: TextStyle(
                            color: Color.fromARGB(255, 42, 225, 137),
                            fontSize: 18.0,
                            fontWeight: FontWeight.w500)),
                  ],
                ),
              ),
              TextButton(
                onPressed: () async {
                  final url = Uri.parse(
                      'mailto:example@developerxon.com?subject=Test&body=Test email');
                  if (await canLaunchUrl(url)) {
                    await launchUrl(url);
                  } else {
                    throw 'Could not launch $url';
                  }
                },
                child: Row(
                  mainAxisSize: MainAxisSize.min,
                  children: const [
                    Icon(
                      Icons.mail,
                      color: Color.fromARGB(255, 106, 130, 238),
                      size: 30,
                    ),
                    SizedBox(width: 18.0),
                    Text('example@developerxon.com',
                        style: TextStyle(
                            color: Color.fromARGB(255, 42, 225, 137),
                            fontSize: 18.0,
                            fontWeight: FontWeight.w500)),
                  ],
                ),
              ),
              TextButton(
                onPressed: () async {
                  final url = Uri.parse('https://www.developerxon.com/');
                  if (await canLaunchUrl(url)) {
                    await launchUrl(url);
                  } else {
                    throw 'Could not launch $url';
                  }
                },
                child: Row(
                  mainAxisSize: MainAxisSize.min,
                  children: const [
                    Icon(
                      Icons.language,
                      color: Color.fromARGB(255, 106, 130, 238),
                      size: 30,
                    ),
                    SizedBox(width: 18.0),
                    Text('www.developerxon.com',
                        style: TextStyle(
                            color: Color.fromARGB(255, 42, 225, 137),
                            fontSize: 20.0,
                            fontWeight: FontWeight.w500)),
                  ],
                ),
              ),
            ],
          )),
    );
  }
}

Output:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: