I’m bringing light to a Flutter Plugin – URL Launcher, because the core idea is a lot more than URL and a lot more than Launcher – bigger than Flutter.
The url_launcher
package in Flutter is a plugin that allows you to open URLs from your Flutter app, using the device’s default browser or other installed apps (e.g., email client). Let’s explore how it fits into the evolution of the launcher concept:
Evolution of the Launcher Concept
- Web 2.0 – In-app Handling: In this era, web applications were primarily accessed through a browser. When you clicked on a link within an app, the default behavior was to open the URL in a new tab or window within the same application.
- Web 3.0 – 3rd party integration and the Decentralized World: With the rise of decentralized technologies like blockchain, IoT, and WebAssembly, we’re entering a new era where applications can interact with each other in more sophisticated ways.
Using url_launcher
in Flutter
To use url_launcher
, add it to your pubspec.yaml
file:
dependencies:
url_launcher: ^6.1.4
Then, import the package and call the launch
function to open a URL:
import 'package:url_launcher/url_launcher.dart';
void main() {
_launchURL();
}
Future<void> _launchURL() async {
final Uri url = Uri.parse('https://www.example.com');
if (await canLaunchUrl(url)) {
await launchUrl(url);
} else {
throw 'Could not launch $url';
}
}
In this example, we use the canLaunchUrl
function to check if the device’s default browser or other installed apps can handle the URL. If it can be launched, we call the launchUrl
function to open the URL.
Intent URI Format
An Intent URI has the following format:
intent://[action]=[param1]#[param2]?[query]=value
Here’s a breakdown:
intent
: specifies that this is an intent URI[action]
: the action to be performed (e.g.,view
,edit
)[param1]
and[param2]
: optional parameters for the action?
: separates the query from the rest of the URI[query]=value
: a query parameter with its value
Launching Other Apps
To launch other apps using an Intent URI, you can use the launch
function from the url_launcher
package:
import 'package:url_launcher/url_launcher.dart';
void main() {
_launchIntentUri();
}
Future<void> _launchIntentUri() async {
final uri = Uri.parse('intent://view#param1=value1¶m2=value2?query=value');
if (await canLaunch(uri)) {
await launch(uri);
} else {
throw 'Could not launch $uri';
}
}
In this example, the launch
function will attempt to open an app that handles Intent URIs with the specified action (view
) and parameters. If an app is found that can handle the URI, it will be launched.
Supported Actions
The following actions are supported:
view
: opens a document or other content for viewingedit
: allows editing of a document or other contentshare
: shares content with otherssend
: sends content via email or messaging
You can use these actions by replacing [action]
in the Intent URI format above.
Example Use Cases
- Opening an email client:
final uri = Uri.parse('mailto:user@example.com?subject=Hello&body=Hi');
launch(uri);
- Opening a PDF viewer:
final uri = Uri.parse('intent://view#file:///path/to/document.pdf');
launch(uri);
- Sharing content on social media:
final uri = Uri.parse('intent://share#text=Hello World&url=https://example.com');
launch(uri);
Remember to replace the placeholder values with your actual data.
By using Intent URIs, you can leverage the capabilities of other apps installed on a user’s device, making your Flutter app more integrated and feature-rich.
Flutter Options for Opening Internet Addresses
Flutter allows you to open Internet URLs within your app without exiting the app using several approaches:
- WebView: You can use a
WebView
widget from theflutter_webview_pro
package or the built-influtter_html
package to display web content within your app. - Custom Tab – you can use the https://pub.dev/packages/flutter_custom_tabs package to open URLs in Chrome Custom Tabs without exiting your app.
- In-app browsers: You can also use third-party libraries like
in_app_browser
orflutter_inappwebview
to display web content within your app.