Flutter SMS

Flutter SMS Example:

Flutter Plugin used for sending SMS and MMS on Android and iOS devices. If you send the SMS to more than one person then  it will be send as MMS. On the iOS device if the message receiving  number is an iPhone and iMessage is enabled it will send as an iMessage for iOS device.

 

Using SMS Service in Flutter:

We can send SMS in multiple ways in Flutter:

  • Message and No People
  • People and No Message
  • Message and People

 

We have to  prefil the correct fields before sending SMS or MMS

Setup for Android:

To use SMS service in Android device we have to take paermission in  AndroidManifest.xml:

Use the below code to take SMS permission in Android.

<uses-permission android:name="android.permission.SEND_SMS"/>

sample example code:

Create a function for sending messages.

String _message = "";

void _sendSMS(String message, List<String> recipents) async {
String _result =
await FlutterSms.sendSMS(message: message, recipients: recipents);
setState(() => _message = _result);
}

 

To send Quick message use below code:

_sendSMS("Here is a test Message", ["5566543454", "26589477788"])

 

To use this SMS Service in Flutter we have to add its dependency package into the pubspec.yaml file:

use below code to add dependency package in pubspec.yaml file.

 

dependencies:

flutter_sms: ^0.0.3

Install SMS dependency package from terminal Editor:

You can install packages from the command line with Flutter by using below command:

$ flutter packages get

Bydefault, your Flutter terminal editor might support flutter packages get.

Importing SMS in flutter:

To use SMS in Flutter Dart code we have to import into the dart code for coding. without importing SMS if we use it in coding then it shows error or exception that is package not found error.

To import dependency package into dart code use below code.

import 'package:flutter_sms/flutter_sms.dart';

 

Below is the Example of  Flutter SMS :

Create a new  Flutter project and then copy and paste the below code into example/lib/main.dart file:

 

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter_sms/flutter_sms.dart';

void main() => runApp(new MyApp());

class MyApp extends StatefulWidget {
@override
_MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> {
TextEditingController _controllerPeople, _controllerMessage;
String _message, body;
List<String> people = [];

@override
void initState() {
super.initState();
initPlatformState();
}

Future<void> initPlatformState() async {
_controllerPeople = TextEditingController();
_controllerMessage = TextEditingController();
}

void _sendSMS(String message, List<String> recipents) async {
String _result =
await FlutterSms.sendSMS(message: message, recipients: recipents);
setState(() => _message = _result);
}

Widget _phoneTile(String name) {
return Padding(
padding: const EdgeInsets.all(3.0),
child: Container(
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(color: Colors.grey[300]),
top: BorderSide(color: Colors.grey[300]),
left: BorderSide(color: Colors.grey[300]),
right: BorderSide(color: Colors.grey[300]),
)),

child: Padding(
padding: EdgeInsets.all(4.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
IconButton(
icon: Icon(Icons.close),
onPressed: () => setState(() => people.remove(name)),
),

Padding(
padding: const EdgeInsets.all(0.0),
child: Text(
name,
textScaleFactor: 1.0,
style: TextStyle(fontSize: 12.0),
),
)
],
),
)),
);
}

@override
Widget build(BuildContext context) {
return new MaterialApp(
home: new Scaffold(
appBar: new AppBar(
title: const Text('SMS/MMS Example'),
),
body: ListView(
children: <Widget>[
people == null || people.isEmpty
? Container(
height: 0.0,
)
: Container(
height: 90.0,
child: Padding(
padding: const EdgeInsets.all(3.0),
child: ListView(
scrollDirection: Axis.horizontal,
children:
List<Widget>.generate(people.length, (int index) {
return _phoneTile(people[index]);
}),
),
),
),
ListTile(
leading: Icon(Icons.people),
title: TextField(
controller: _controllerPeople,
decoration: InputDecoration(labelText: "Add Phone Number"),
onChanged: (String value) => setState(() {}),
),
trailing: IconButton(
icon: Icon(Icons.add),
onPressed: _controllerPeople.text.isEmpty
? null
: () => setState(() {
people.add(_controllerPeople.text.toString());
_controllerPeople.clear();
}),
),
),
Divider(),
ListTile(
leading: Icon(Icons.message),
title: TextField(
decoration: InputDecoration(labelText: " Add Message"),
controller: _controllerMessage,
onChanged: (String value) => setState(() {}),
),
trailing: IconButton(
icon: Icon(Icons.save),
onPressed: _controllerMessage.text.isEmpty
? null
: () => setState(() {
body = _controllerMessage.text.toString();
_controllerMessage.clear();
}),
),
),
Divider(),
ListTile(
leading: Icon(Icons.text_fields),
title: Text(body ?? "No Message Set"),
subtitle: people == null || people.isEmpty
? null
: Text(people.toString()),
trailing: IconButton(
icon: Icon(Icons.send),
onPressed: () {
if ((people == null || people.isEmpty) ||
(body == null || body.isEmpty)) {
setState(() =>
_message = "At Least 1 Person or Message Required");
} else {
_sendSMS(body, people);
}
},
),
),
Divider(),
Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(12.0),
child: Text(
_message ?? "No Data",
maxLines: null,
),
),
],
),
],
),
),
);
}
}

Leave a Reply

Categories

Myles [CPS] IN
Shein WW