flutter_otp 0.3.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 88

Awesome Flutter contributions welcome Known Vulnerabilities Build Status

flutter_otp #

A Flutter package for Android and iOS for sending and verifying OTP to a Phone number.

Important message for existing Users #

NEW FEATURE !! #

Now you can send the message to any country code passed as a parameter to the interface (sendOtp() function)

Installing #

Use this package as a library #

1. Depend on it #

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

dependencies:
  flutter_otp: ^0.3.1

2. Install it #

You can install packages from the command line: with Flutter:

$ flutter packages get

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

3. Import it #

Now in your Dart code, you can use:

import 'package:flutter_otp/flutter_otp.dart';

Functions #

The important functions used in the package are :

  1. sendOtp() to send a OTP (four digit by default, but customizable):
void sendOtp(String phoneNumber, [String messageText]) {
    ...        
}

NOTE:

  1. 10 digit phoneNumber should be passed.
  2. parameter "messageText" is optional. By default the message text is "Your OTP is : XXXX". If "messageText" parameter is passed then message is sent as "
  1. resultChecker() which takes the OTP entered by the user as a parameter. The function returns true if OTP is matched, else false is returned.
bool resultChecker(int enteredOtp) {
    ...
} 

NOTE: The OTP entered by user (say, through TextField widget or TextFormField widget etc) is to be passed as a parameter to this function.

Usage #

To use this package in your application you need to have a sim card in your mobile.

Example Usage:

...
sendOtp('958347XXXX');  //Pass phone number as String

...

int enteredOtp;
TextField(
  onChanged: (val) {
    enteredOtp = val;    
  }
)

...

bool isCorrectOTP = resultChecker();
if(isCorrectOTP) {
    print('Success');
} else {
    print('Failure');
}
...

OR custom "messageText" can be passed as parameter to sendOTP

...
sendOtp('958347XXXX', 'OTP is : ');  //Pass phone number and Custom messaseText as String

...

int enteredOtp;
TextField(
  onChanged: (val) {
    enteredOtp = val;    
  }
)

...

bool isCorrectOTP = resultChecker();
if(isCorrectOTP) {
    print('Success');
} else {
    print('Failure');
}
...

Contributors #

  1. Fawaz Hussain
  2. Jerin Francis

⭐ the repo to show support!

More Information #

Pub package Flutter documentation.

0.3.1 #

  • simplified boolean expression

0.3.0 #

  • private variable naming convention changed
  • bug fixes

0.2.9 #

  • Removed broken links from README.md

0.2.8 #

  • Code modifications
  • Ran flutter format on lib/flutter_otp.dart

0.2.6 #

  • NEW FEATURE !!! : country code parameter added (+1 by default)
  • parameter countryCode (optional) is included

0.2.4 #

  • Doc-strings updated
  • example/README.md updated

0.2.3 #

  • sendOtp() fixed: optional params list

0.2.2 #

  • Provide minimum and maximum OTP range

0.2.0 #

  • Security within classes increased
  • pubspec.yaml upgraded

0.1.4 #

  • API Reference document added

0.1.3 #

  • formatted lib/flutter_otp.dart

0.1.2 #

  • README.md changed
  • pubspec.yaml updated

0.1.1 #

  • parameter message changed to messageText in sendOtp()
  • README.md updated
  • pubspec.yaml description updated

0.1.0 #

  • example/ directory added
  • README.md updated
  • pubspec.yaml description updated

0.0.2 #

  • resultChecker() signature modified to resultChecker(int)

0.0.1 #

  • Initial Release

example/README.md

Example #

For more information read Pub Package

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

void main() => runApp(HomeScreen());

FlutterOtp otp = FlutterOtp();
String result;
int enteredOtp;

class HomeScreen extends StatefulWidget {
  @override
  _HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  String phoneNumber;

  @override
  Widget build(BuildContext context) {
    bool yesOrNo = otp.resultChecker(enteredOtp);
    return MaterialApp(
      home: SafeArea(
        child: Scaffold(
          body: Center(
            child: Column(
              children: <Widget>[
                Text('Enter Phone:'),
                TextField(
                  onChanged: (val) {
                    phoneNumber = val;
                  },
                ),
                RaisedButton(
                  child: Text('SEND OTP'),
                  onPressed: () {
                    otp.sendOtp(phoneNumber);
                  },
                ),
                Text('Enter OTP sent to your phone'),
                TextField(
                  onChanged: (val) {
                    enteredOtp = int.parse(val);
                  },
                ),
                RaisedButton(
                  child: Text('VERIFY'),
                  onPressed: () {
                    setState(() {
                      bool yesOrNo = otp.resultChecker(enteredOtp);
                      print(yesOrNo);
                    });
                  },
                ),
                Center(
                  child: Text(yesOrNo.toString()),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  flutter_otp: ^0.3.1

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_otp/flutter_otp.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
76
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]
88
Learn more about scoring.

We analyzed this package on Dec 13, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.0
  • pana: 0.13.1+4
  • Flutter: 1.12.13+hotfix.4

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
sms ^0.2.4 0.2.4
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.8
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test