Updated 28 April 2023
When developing mobile apps in Flutter sometimes we require to launch some other app from our app. For example, map applications, caller applications, browsers, mail applications, and may more.
In Flutter, we have a very efficient package i.e. url_launcher
 package.
Partner with our Flutter app development company for your app ideas.
Let’s see how we can use url_launcher
 package in our app:
Firstly, we need to add the dependency to the pubspec.yml file. Then, run the flutter pub get
command.
1 2 |
dependencies: url_launcher: ^6.0.9 |
Then in the dart file we can use it as per our need.
1 |
import 'package:url_launcher/url_launcher.dart'; |
Now, let’s see an example implementation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
import 'dart:async'; 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: 'URL Launcher', theme: ThemeData( primarySwatch: Colors.blue, ), home: MyHomePage(title: 'URL Launcher'), ); } } class MyHomePage extends StatefulWidget { MyHomePage({Key? key, required this.title}) : super(key: key); final String title; @override _MyHomePageState createState() => _MyHomePageState(); } class _MyHomePageState extends State<MyHomePage> { Future<void>? _launched; String _phone = ''; Widget _launchStatus(BuildContext context, AsyncSnapshot<void> snapshot) { if (snapshot.hasError) { return Text('Error: ${snapshot.error}'); } else { return const Text(''); } } Future<void> _makePhoneCall(String url) async { if (await canLaunch(url)) { await launch(url); } else { throw 'Could not launch $url'; } } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text(widget.title), ), body: ListView( children: <Widget>[ Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Padding( padding: const EdgeInsets.all(16.0), child: TextField(onChanged: (String text) => _phone = text, decoration: const InputDecoration(hintText: 'Input the phone number to launch')), ), ElevatedButton( onPressed: () => setState(() { _launched = _makePhoneCall('tel:$_phone'); }), child: const Text('Make phone call'), ), const Padding(padding: EdgeInsets.all(16.0)), FutureBuilder<void>(future: _launched, builder: _launchStatus), ], ), ], ), ); } } |
In this way we can implement the URL launcher package in our app.
For a visual explanation we can also take reference from the official video by the Flutter team.
Thanks for reading this article ❤
If I missed something 🙈, then please let me know in the comments. I would love to improve.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.