As a part of the flutter tutorial series, we’re going to explore the URL launcher example. Here we’re going to use a package called url_launcher
Usage:
- To open phone dialer / SMS app
- Open website in external browser
- Send email using a mailer app
- or open any app using a custom URL scheme.
Getting started
To work with our example, first, we need to create a new project or you can choose any existing flutter project.
flutter create learnflutter
cd learnflutter
Once the project is created, we need to add url_launcher
package in pubspec.yaml
file
dependencies:
url_launcher: ^5.7.10
Then execute flutter pub get
to install all required packages
Flutter URL Launcher example
_launch(url) async {
if (await canLaunch(url)) {
await launch(url);
} else {
print("Not supported");
}
}
When we pass the URL inside _launch(), we can open an app such as the dialer, SMS, WhatsApp, and more.
How to open Phone Dialer in flutter?
RaisedButton(
child: Text("Open Dialer"),
onPressed: () => _launch('tel:+919952313535'),
),
This will automatically open the phone dialer
How to open a Website in a browser in flutter?
RaisedButton(
child: Text("Open Website"),
onPressed: () => _launch('https://codesundar.com'),
),
This will launch the website in the system default browser.
How to send SMS app in flutter?
RaisedButton(
child: Text("Open SMS"),
onPressed: () => _launch('sms:+919952313535'),
),
This will launch the default SMS app on your device
How to send Email in flutter?
RaisedButton(
child: Text("Open Email"),
onPressed: () => _launch(
'mailto:me@codesundar.com?subject=amazing tutorial&body=thanks dude'),
),
This will open the default mailer app on your phone
How to open WhatsApp in flutter?
RaisedButton(
child: Text("Open WhatsApp"),
onPressed: () => _launch(
'whatsapp://send?text=sample text&phone=919952313535'),
)