flutter_credit_card 0.1.0 flutter_credit_card: ^0.1.0 copied to clipboard
A Credit Card widget package, support entering card details, card flip animation.
import 'package:flutter/material.dart';
import 'package:flutter_credit_card/flutter_credit_card.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Credit Card View Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: CreditCardExample(),
);
}
}
class CreditCardExample extends StatefulWidget {
CreditCardExample({
Key key,
}) : super(key: key);
@override
_CreditCardExampleState createState() => _CreditCardExampleState();
}
class _CreditCardExampleState extends State<CreditCardExample> {
String cardNumber = "";
String expiryDate = "";
String cardHolderName = "";
String cvvCode = "";
MaskedTextController _cardNumberController =
MaskedTextController(mask: "0000 0000 0000 0000");
TextEditingController _expiryDateController =
MaskedTextController(mask: "00/00");
TextEditingController _cardHolderNameController = TextEditingController();
TextEditingController _cvvCodeController = MaskedTextController(mask: "0000");
FocusNode cvvFocusNode = FocusNode();
bool isCvvFocused = false;
void textFieldFocusDidChange() {
setState(() {
isCvvFocused = cvvFocusNode.hasFocus;
});
}
@override
void initState() {
super.initState();
cvvFocusNode.addListener(textFieldFocusDidChange);
_cardNumberController.addListener(() {
setState(() {
cardNumber = _cardNumberController.text;
});
});
_expiryDateController.addListener(() {
setState(() {
expiryDate = _expiryDateController.text;
});
});
_cardHolderNameController.addListener(() {
setState(() {
cardHolderName = _cardHolderNameController.text;
});
});
_cvvCodeController.addListener(() {
setState(() {
cvvCode = _cvvCodeController.text;
});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomInset: true,
body: SafeArea(
child: Column(
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
CreditCardWidget(
cardNumber: cardNumber,
expiryDate: expiryDate,
cardHolderName: cardHolderName,
cvvCode: cvvCode,
showBackView: isCvvFocused,
),
Expanded(
child: SingleChildScrollView(
child: Form(
child: Column(
children: <Widget>[
Container(
padding: EdgeInsets.symmetric(vertical: 8.0),
margin: EdgeInsets.only(left: 16, top: 16, right: 16),
child: TextFormField(
controller: _cardNumberController,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: "Card number",
hintText: "xxxx xxxx xxxx xxxx",
),
keyboardType: TextInputType.number,
textInputAction: TextInputAction.next,
),
),
Container(
padding: EdgeInsets.symmetric(vertical: 8.0),
margin: EdgeInsets.only(left: 16, top: 8, right: 16),
child: TextFormField(
controller: _expiryDateController,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: "Expired Date",
hintText: "MM/YY"),
keyboardType: TextInputType.number,
textInputAction: TextInputAction.next,
),
),
Container(
padding: EdgeInsets.symmetric(vertical: 8.0),
margin: EdgeInsets.only(left: 16, top: 8, right: 16),
child: TextFormField(
controller: _cardHolderNameController,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: "Card Holder",
),
keyboardType: TextInputType.text,
textInputAction: TextInputAction.next,
),
),
Container(
padding: EdgeInsets.symmetric(vertical: 8.0),
margin: EdgeInsets.only(left: 16, top: 8, right: 16),
child: TextField(
focusNode: cvvFocusNode,
controller: _cvvCodeController,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: "CVV",
hintText: "XXXX",
),
keyboardType: TextInputType.number,
textInputAction: TextInputAction.done,
onChanged: (text) {
setState(() {
cvvCode = text;
});
},
),
),
],
),
),
),
),
],
),
),
);
}
}