forked from SimformSolutionsPvtLtd/flutter_credit_card
-
Notifications
You must be signed in to change notification settings - Fork 1
/
custom_credit_card_form.dart
158 lines (151 loc) · 6.37 KB
/
custom_credit_card_form.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import 'package:flutter/material.dart';
import 'package:flutter_credit_card/credit_card_brand.dart';
import 'package:flutter_credit_card/credit_card_form.dart';
import 'package:flutter_credit_card/credit_card_model.dart';
import 'package:flutter_credit_card/credit_card_widget.dart';
class CustomCreditCardForm extends StatefulWidget {
const CustomCreditCardForm({Key? key, required this.creditCardCallBack}) : super(key: key);
final void Function(CreditCardModel) creditCardCallBack;
@override
State<CustomCreditCardForm> createState() => _CustomCreditCardFormState();
}
class _CustomCreditCardFormState extends State<CustomCreditCardForm> {
String cardNumber = '';
String expiryDate = '';
String cardHolderName = '';
String cvvCode = '';
bool isCvvFocused = false;
bool useGlassMorphism = false;
bool useBackgroundImage = false;
final GlobalKey<FormState> formKey = GlobalKey<FormState>();
final TextStyle hintStyle = const TextStyle(color: Color(0xFFA9A6BE), fontSize: 16.0);
final TextStyle textStyle = const TextStyle(color: Color(0xFF2F2C35), fontSize: 16.0);
OutlineInputBorder border =
const OutlineInputBorder(borderSide: BorderSide(color: Color(0xFFDDDDE8), width: 1.0), borderRadius: BorderRadius.all(Radius.circular(10)));
final OutlineInputBorder focusBorder = const OutlineInputBorder(
borderSide: BorderSide(color: Color.fromARGB(255, 71, 44, 250), width: 1.0), borderRadius: BorderRadius.all(Radius.circular(10)));
final OutlineInputBorder errorBorder =
const OutlineInputBorder(borderSide: BorderSide(color: Color.fromARGB(255, 255, 0, 0), width: 1.0), borderRadius: BorderRadius.all(Radius.circular(10)));
bool isValidated = false;
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
Expanded(
child: SingleChildScrollView(
child: Column(
children: [
CreditCardWidget(
cardNumber: cardNumber,
expiryDate: expiryDate,
cardHolderName: cardHolderName,
cvvCode: cvvCode,
showBackView: isCvvFocused,
obscureCardNumber: true,
obscureCardCvv: true,
isHolderNameVisible: true,
cardBgColor: const Color(0xff363636),
backgroundImage: 'assets/card_image.png',
isSwipeGestureEnabled: true,
onCreditCardWidgetChange: (CreditCardBrand creditCardBrand) {},
),
CreditCardForm(
formKey: formKey,
cardNumber: cardNumber,
cvvCode: cvvCode,
isHolderNameVisible: true,
isCardNumberVisible: true,
isExpiryDateVisible: true,
cardHolderName: cardHolderName,
expiryDate: expiryDate,
themeColor: Colors.blue,
textColor: Colors.white,
textStyle: textStyle,
cardNumberDecoration: InputDecoration(
prefixIcon: Image.asset('assets/card_number.png'),
hintText: 'XXXX-XXXX-XXXX-XXXX',
hintStyle: hintStyle,
focusedBorder: focusBorder,
enabledBorder: border,
focusedErrorBorder: errorBorder,
errorBorder: errorBorder,
),
expiryDateDecoration: InputDecoration(
prefixIcon: Image.asset('assets/card_date.png'),
hintStyle: hintStyle,
focusedBorder: focusBorder,
enabledBorder: border,
errorBorder: errorBorder,
focusedErrorBorder: errorBorder,
hintText: 'MM/YY',
),
cvvCodeDecoration: InputDecoration(
prefixIcon: Image.asset('assets/card_cvv.png'),
hintStyle: hintStyle,
focusedBorder: focusBorder,
enabledBorder: border,
errorBorder: errorBorder,
focusedErrorBorder: errorBorder,
hintText: 'XXX',
),
cardHolderDecoration: InputDecoration(
prefixIcon: Image.asset('assets/card_name.png'),
hintStyle: hintStyle,
focusedBorder: focusBorder,
enabledBorder: border,
errorBorder: errorBorder,
focusedErrorBorder: errorBorder,
hintText: 'CARD HOLDER NAME',
),
onCreditCardModelChange: onCreditCardModelChange,
),
],
),
),
),
SafeArea(
child: Padding(
padding: const EdgeInsets.only(left: 16, right: 16, top: 16),
child: SizedBox(
width: double.infinity,
height: 48.0,
child: ElevatedButton(
onPressed: isValidated ? () => widget.creditCardCallBack(CreditCardModel(cardNumber, expiryDate, cardHolderName, cvvCode, isCvvFocused)) : null,
style: ElevatedButton.styleFrom(
elevation: 0,
foregroundColor: Colors.transparent,
backgroundColor: isValidated ? const Color(0xFF5F2EEA) : const Color(0xFFDDDDE8),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10.0))),
child: Text(
'Add',
style: TextStyle(
color: isValidated ? Colors.white : const Color(0xFFA9A6BE),
fontSize: 16.0,
fontWeight: FontWeight.bold,
),
),
),
),
),
)
],
);
}
void _onValidate() {
if (formKey.currentState!.validate()) {
isValidated = true;
return;
}
isValidated = false;
}
void onCreditCardModelChange(CreditCardModel? creditCardModel) {
setState(() {
cardNumber = creditCardModel!.cardNumber;
expiryDate = creditCardModel.expiryDate;
cardHolderName = creditCardModel.cardHolderName;
cvvCode = creditCardModel.cvvCode;
isCvvFocused = creditCardModel.isCvvFocused;
_onValidate();
});
}
}