Flutter — Making Phone calls, sending SMS and emails with url_launcher
Depending on the type of app you are developing, you might want the user to quickly make a call or send a message or email by pressing a button in your app. This might be useful for example if you want him to contact a support line, or if you want to let him call a store or a restaurant.
Adding the url_launcher package
In order to do all the above mentioned we’re going to need to import a single library which is called url_launcher, you might have used it already to launch websites on safari or chrome from your flutter app. If you haven’t already, let’s add it to our pubspec.yaml file.
Creating the calls and messages service
In our services folder, let’s create a calls_and_messages_service.dart file.
This should be pretty self-explanatory, the thing to place attention to is the URL format.
- tel: followed by the number will open the default phone app
- sms: followed by the number will open the default messages app
- mailto: followed by the email will open the default email app
As always, our service_locator
In order to instantiate the CallsAndMessagesService and have it available quickly anywhere in our app, we want to create a service locator using get_it.
And don’t forget to call setupLocator in the main function.
Getting the service
In order to have access to out CallsAndMessagesService we call it using the locator.
Creating a quick UI and calling the respective methods
Wrapping it up, let’s create a column of three buttons each for the feature we previously implemented.
GitHub link for the code shown in this article
All the code in this article is available at this GitHub link.
Conclusion
This was a quick snippet in order to allow you to make phone calls, send SMS and emails from your flutter app. I hope you enjoyed this snippet!
About the author
You can find me on
- Instagram: Daniele Cambi (https://www.instagram.com/dancamdev/)
- Twitter: @dancamdev (https://twitter.com/dancamdev)
- Email: dancam.dev@gmail.com
Daniele Cambi (@dancamdev)