credit_card_validate 0.0.3

  • Readme
  • Changelog
  • Example
  • Installing
  • new58

credit_card_validate #

Demo : https://creditcardvalidator.netlify.com/

Using this package you can easily validate a credit card number, and identify the brand of the credit card.

Note: This package can be used in Flutter for Mobile, Web and Desktop.

Currently Supported Brands #

  1. Visa
  2. Mastercard
  3. American Express
  4. Discover

Installing #

Add the following line to your dependencies:

credit_card_validate: 0.0.3

and run flutter packages get

Import #

Import the package wherever you want to use it.

import 'package:credit_card_validate/credit_card_validate.dart';

Example #

For a fully functional demo, please visit https://creditcardvalidator.netlify.com/

Validating a Credit Card Number

bool isValid = CreditCardValidator.isCreditCardValid(cardNumber: '4716073082297205')

Identifying Credit Card Brand

String brand = CreditCardValidator.identifyCardBrand('4716073082297205');

For a fully functional example, please see the example directory

Demo #

Credit Card Validator Demo

Issues and Improvements #

Feel free to contribute at: https://github.com/ayush221b/credit_card_validate/issues

[0.0.3] - 26.03.2020 #

  1. Added sanity checks for input data

[0.0.2] - 26.03.2020 #

  1. Added the credit card number validation logic.
  2. Added function to identify brand of credit card. Currently supported brands include: Visa, Master Card, American Express, and Discover.
  3. Added an example app with a demo textfield to illustrate the use of the package.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:credit_card_validate/credit_card_validate.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Credit Card Validator Demo',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'CC Validator'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String creditCardNumber = '';
  IconData brandIcon;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
        centerTitle: true,
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Container(
              width: 250,
              child: TextField(
                onChanged: (String str) {
                  setState(() {
                    creditCardNumber = str;
                  });
                  String brand = CreditCardValidator.identifyCardBrand(str);
                  IconData ccBrandIcon;
                  if (brand != null) {
                    if (brand == 'visa') {
                      ccBrandIcon = FontAwesomeIcons.ccVisa;
                    } else if (brand == 'master_card') {
                      ccBrandIcon = FontAwesomeIcons.ccMastercard;
                    } else if (brand == 'american_express') {
                      ccBrandIcon = FontAwesomeIcons.ccAmex;
                    } else if (brand == 'discover') {
                      ccBrandIcon = FontAwesomeIcons.ccDiscover;
                    }
                  }
                  setState(() {
                    brandIcon = ccBrandIcon;
                  });
                },
                decoration: InputDecoration(
                  labelText: 'Start typing to validate...',
                    suffixIcon: brandIcon != null ? FaIcon(brandIcon, size: 32,) : null),
              ),
            ),
            SizedBox(
              height: 20,
            ),
            creditCardNumber.length < 13
                ? Text('Please enter atleast 13 characters')
                : CreditCardValidator.isCreditCardValid(
                        cardNumber: creditCardNumber)
                    ? Text(
                        'The credit card number is valid.',
                        style: TextStyle(color: Colors.green),
                      )
                    : Text(
                        'The credit card number is invalid.',
                        style: TextStyle(color: Colors.red),
                      )
          ],
        ),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  credit_card_validate: ^0.0.3

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:credit_card_validate/credit_card_validate.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
21
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
90
Overall:
Weighted score of the above. [more]
58
Learn more about scoring.

We analyzed this package on Mar 31, 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

Health suggestions

Format lib/credit_card_validate.dart.

Run flutter format to format lib/credit_card_validate.dart.

Maintenance suggestions

Package is pre-v0.1 release. (-10 points)

While nothing is inherently wrong with versions of 0.0.*, it might mean that the author is still experimenting with the general direction of the API.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
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