How to Open SMS App with Number and Text in Flutter

In this post, we are going to show you how to open the default SMS app along with the receiver number and the SMS body text in Flutter. See the example below for more details:

First, add url_launcher package to your project by adding the following lines in pubspec.yaml file.

dependencies:
  flutter:
    sdk: flutter
  url_launcher: ^6.1.5

Import the package to your script:

import 'package:url_launcher/url_launcher.dart';

Uri sms = Uri.parse('sms:101022?body=your+text+here');
if (await launchUrl(sms)) {
    //app opened
}else{
    //app is not opened
}

Here, you have to replace the number 101022 with your own number and your SMS body text.

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(
      home: Home(),
    );
  }
}

class Home extends StatefulWidget{
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> { 

  @override
  Widget build(BuildContext context) { 
    return  Scaffold(
          appBar: AppBar(
            title: Text("Open SMS App"),
            backgroundColor: Colors.redAccent,
          ),
          body: Container(
            padding: EdgeInsets.only(top:20, left:20, right:20),
            alignment: Alignment.topCenter,
            child: Column(
              children: [
                 ElevatedButton(
                  onPressed: ()async{
                      Uri sms = Uri.parse('sms:101022?body=your+text+here');
                      if (await launchUrl(sms)) {
                          //app opened
                      }else{
                          //app is not opened
                      }
                  }, 
                  child: Text("Send Us SMS")
                )
            ],)
          )
       );
  }
}

Here, the first screenshot is the output of UI, the second one appears when you click on the button, and the third one is the SMS app with receiver number and SMS body text.

In this way, you can open the default SMS app with the receiver number and SMS body text.

No any Comments on this Article


Please Wait...