flutter_sms 2.0.0+2

  • Readme
  • Changelog
  • Example
  • Installing
  • 97

Flutter Community: flutter_sms

Buy Me A Coffee Donate

flutter_sms #

alt-text-1

Online Demo: https://fluttercommunity.github.io/flutter_sms/

Description #

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

How To Use #

You can send multiple ways:

  1. Message and No People
  2. People and No Message
  3. Message and People

This will populate the correct fields.

Example #

Make sure to Install and Import the Package.

import 'package:flutter_sms/flutter_sms.dart';

Create a function for sending messages.

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

You can quickly send the message with this function.

String message = "This is a test message!";
List<String> recipents = ["1234567890", "5556787676"];

_sendSMS(message, recipents);

Screenshots #

iOS SMSAndroid MMS
alt-text-1alt-text-2

You can find other screenshots here.

2.0.0 #

  • Adding Online Demo
  • Adding Support for Web

1.1.0 #

  • Updating for latest ios/android flutter plugin template

1.0.1 #

  • Android result added

1.0.0 * 04.29.2019 #

  • Reduce minSdkVersion to 16
  • Fix crash on android when recipients is empty (substring on empty string)
  • Add canSendSMS API
    • Checks if device is SMS capable (iOS and Android)
    • Checks if smsto: Intent is resolved and resulting activity exported (Android)
  • sendSMS: Return error when device is not SMS capable (Android)
  • Remove unused AndroidX dependency
  • Use Intent.ACTION_SENDTO and Intent.EXTRA_TEXT to increase message app support coverage

0.2.0 * 04.06.2019 #

  • Updating example to be desktop aware

0.1.0 #

  • Fix for Android Multiple People
  • Fixes Issues #8 and #4

0.0.5 #

  • Removed SMS Permissions on Android

0.0.4 #

  • Bug Fix Send Result
  • Added Can Send Text

0.0.3 #

  • Bug Fix for iOS Simulator Error

0.0.2 #

  • Fixing Version

0.0.1 #

  • SMS and MMS for Android and iOS

example/lib/main.dart

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

import 'package:flutter_sms/flutter_sms.dart';

import 'dart:io' show Platform;
import 'package:flutter/foundation.dart';

// The existing imports
// !! Keep your existing impots here !!

/// main is entry point of Flutter application
void main() {
  WidgetsFlutterBinding.ensureInitialized();
  if (!kIsWeb) _setTargetPlatformForDesktop();
  return runApp(MyApp());
}

/// If the current platform is desktop, override the default platform to
/// a supported platform (iOS for macOS, Android for Linux and Windows).
/// Otherwise, do nothing.
void _setTargetPlatformForDesktop() {
  TargetPlatform targetPlatform;
  if (Platform.isMacOS) {
    targetPlatform = TargetPlatform.iOS;
  } else if (Platform.isLinux || Platform.isWindows) {
    targetPlatform = TargetPlatform.android;
  }
  if (targetPlatform != null) {
    debugDefaultTargetPlatformOverride = targetPlatform;
  }
}

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

class _MyAppState extends State<MyApp> {
  TextEditingController _controllerPeople, _controllerMessage;
  String _message, body;
  String _canSendSMSMessage = "Check is not run.";
  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 {
    try {
      String _result =
          await sendSMS(message: message, recipients: recipents);
      setState(() => _message = _result);
    } catch (error) {
      setState(() => _message = error.toString());
    }
  }

  void _canSendSMS() async {
    bool _result = await canSendSMS();
    setState(() => _canSendSMSMessage =
        _result ? 'This unit can send SMS' : 'This unit cannot send SMS');
  }

  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"),
                keyboardType: TextInputType.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>[
                Expanded(
                  child: Padding(
                    padding: const EdgeInsets.all(12.0),
                    child: Text(
                      _message ?? "No Data",
                      maxLines: null,
                    ),
                  ),
                ),
              ],
            ),
            Divider(),
            Padding(
              padding: const EdgeInsets.all(8),
              child: Text(
                "Can send SMS",
                style: Theme.of(context).textTheme.title,
              ),
            ),
            Padding(
                padding: const EdgeInsets.all(8),
                child: Text(_canSendSMSMessage,
                    style: Theme.of(context).textTheme.body1)),
            Padding(
              padding:
                  const EdgeInsets.symmetric(vertical: 8.0, horizontal: 24),
              child: RaisedButton(
                color: Theme.of(context).accentColor,
                padding: EdgeInsets.symmetric(vertical: 16),
                child: Text("RUN CHECK",
                    style: Theme.of(context).accentTextTheme.button),
                onPressed: () {
                  _canSendSMS();
                },
              ),
            )
          ],
        ),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  flutter_sms: ^2.0.0+2

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:flutter_sms/flutter_sms.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
93
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
97
Learn more about scoring.

We analyzed this package on Mar 27, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6
  • Flutter: 1.12.13+hotfix.8

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.28.0 <3.0.0
flutter 0.0.0
flutter_web_plugins 0.0.0
plugin_platform_interface ^1.0.1 1.0.2
url_launcher ^5.4.1 5.4.2
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.8
sky_engine 0.0.99
typed_data 1.1.6
url_launcher_macos 0.0.1+4
url_launcher_platform_interface 1.0.6
url_launcher_web 0.1.1+1
vector_math 2.0.8
Dev dependencies
flutter_test