forked from SimformSolutionsPvtLtd/flutter_credit_card
-
Notifications
You must be signed in to change notification settings - Fork 1
/
credit_card_form.dart
426 lines (373 loc) · 16.8 KB
/
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
import 'package:flutter/material.dart';
import 'package:flutter_credit_card/flutter_credit_card.dart';
class CreditCardForm extends StatefulWidget {
const CreditCardForm({
Key? key,
required this.cardNumber,
required this.expiryDate,
required this.cardHolderName,
required this.cvvCode,
this.obscureCvv = false,
this.obscureNumber = false,
required this.onCreditCardModelChange,
required this.themeColor,
this.textColor = Colors.black,
this.cursorColor,
required this.cardHolderDecoration,
required this.cardNumberDecoration,
required this.expiryDateDecoration,
required this.cvvCodeDecoration,
required this.formKey,
this.cardNumberKey,
this.cardHolderKey,
this.expiryDateKey,
this.cvvCodeKey,
this.cvvValidationMessage = 'Please input a valid CVV',
this.dateValidationMessage = 'Please input a valid date',
this.numberValidationMessage = 'Please input a valid number',
this.cardHolderNameValidationMessage = 'Please input a valid card holder name',
this.isHolderNameVisible = true,
this.isCardNumberVisible = true,
this.isExpiryDateVisible = true,
this.enableCvv = true,
this.autovalidateMode,
this.cardNumberValidator,
this.expiryDateValidator,
this.cvvValidator,
this.cardHolderValidator,
this.onFormComplete,
this.textStyle,
this.disableCardNumberAutoFillHints = false,
}) : super(key: key);
/// A string indicating card number in the text field.
final String cardNumber;
/// A string indicating expiry date in the text field.
final String expiryDate;
/// A string indicating card holder name in the text field.
final String cardHolderName;
/// A string indicating cvv code in the text field.
final String cvvCode;
/// Error message string when invalid cvv is entered.
final String cvvValidationMessage;
/// Error message string when invalid expiry date is entered.
final String dateValidationMessage;
/// Error message string when invalid credit card number is entered.
final String numberValidationMessage;
final String cardHolderNameValidationMessage;
/// Provides callback when there is any change in [CreditCardModel].
final void Function(CreditCardModel) onCreditCardModelChange;
/// Color of the theme of the credit card form.
final Color themeColor;
/// Color of text in the credit card form.
final Color textColor;
/// Cursor color in the credit card form.
final Color? cursorColor;
/// When enabled cvv gets hidden with obscuring characters. Defaults to
/// false.
final bool obscureCvv;
/// When enabled credit card number get hidden with obscuring characters.
/// Defaults to false.
final bool obscureNumber;
/// Allow editing the holder name by enabling this in the credit card form.
/// Defaults to true.
final bool isHolderNameVisible;
/// Allow editing the credit card number by enabling this in the credit
/// card form. Defaults to true.
final bool isCardNumberVisible;
/// Allow editing the cvv code by enabling this in the credit card form.
/// Defaults to true.
final bool enableCvv;
/// Allows editing the expiry date by enabling this in the credit
/// card form. Defaults to true.
final bool isExpiryDateVisible;
/// A form state key for this credit card form.
final GlobalKey<FormState> formKey;
/// Provides a callback when text field provides callback in
/// [onEditingComplete].
final Function? onFormComplete;
/// A FormFieldState key for card number text field.
final GlobalKey<FormFieldState<String>>? cardNumberKey;
/// A FormFieldState key for card holder text field.
final GlobalKey<FormFieldState<String>>? cardHolderKey;
/// A FormFieldState key for expiry date text field.
final GlobalKey<FormFieldState<String>>? expiryDateKey;
/// A FormFieldState key for cvv code text field.
final GlobalKey<FormFieldState<String>>? cvvCodeKey;
/// Provides decoration to card number text field.
final InputDecoration cardNumberDecoration;
/// Provides decoration to card holder text field.
final InputDecoration cardHolderDecoration;
/// Provides decoration to expiry date text field.
final InputDecoration expiryDateDecoration;
/// Provides decoration to cvv code text field.
final InputDecoration cvvCodeDecoration;
/// Used to configure the auto validation of [FormField] and [Form] widgets.
final AutovalidateMode? autovalidateMode;
/// A validator for card number text field.
final String? Function(String?)? cardNumberValidator;
/// A validator for expiry date text field.
final String? Function(String?)? expiryDateValidator;
/// A validator for cvv code text field.
final String? Function(String?)? cvvValidator;
/// A validator for card holder text field.
final String? Function(String?)? cardHolderValidator;
// Customized
final TextStyle? textStyle;
/// Setting this flag to true will disable autofill hints for Credit card
/// number text field. Flutter has a bug when auto fill hints are enabled for
/// credit card numbers it shows keyboard with characters. But, disabling
/// auto fill hints will show correct keyboard.
///
/// Defaults to false.
///
/// You can follow the issue here
/// [https://github.com/flutter/flutter/issues/104604](https://github.com/flutter/flutter/issues/104604).
final bool disableCardNumberAutoFillHints;
@override
_CreditCardFormState createState() => _CreditCardFormState();
}
class _CreditCardFormState extends State<CreditCardForm> {
late String cardNumber;
late String expiryDate;
late String cardHolderName;
late String cvvCode;
bool isCvvFocused = false;
late Color themeColor;
late void Function(CreditCardModel) onCreditCardModelChange;
late CreditCardModel creditCardModel;
final MaskedTextController _cardNumberController = MaskedTextController(mask: '0000 0000 0000 0000');
final TextEditingController _expiryDateController = MaskedTextController(mask: '00/00');
final TextEditingController _cardHolderNameController = TextEditingController();
final TextEditingController _cvvCodeController = MaskedTextController(mask: '0000');
bool showCardNumber = false;
FocusNode cvvFocusNode = FocusNode();
FocusNode expiryDateNode = FocusNode();
FocusNode cardHolderNode = FocusNode();
void textFieldFocusDidChange() {
creditCardModel.isCvvFocused = cvvFocusNode.hasFocus;
onCreditCardModelChange(creditCardModel);
}
void createCreditCardModel() {
cardNumber = widget.cardNumber;
expiryDate = widget.expiryDate;
cardHolderName = widget.cardHolderName;
cvvCode = widget.cvvCode;
creditCardModel = CreditCardModel(cardNumber, expiryDate, cardHolderName, cvvCode, isCvvFocused);
}
@override
void initState() {
super.initState();
createCreditCardModel();
_cardNumberController.text = widget.cardNumber;
_expiryDateController.text = widget.expiryDate;
_cardHolderNameController.text = widget.cardHolderName;
_cvvCodeController.text = widget.cvvCode;
onCreditCardModelChange = widget.onCreditCardModelChange;
cvvFocusNode.addListener(textFieldFocusDidChange);
_cardNumberController.addListener(() => widget.formKey.currentState!.validate());
_expiryDateController.addListener(() => widget.formKey.currentState!.validate());
_cardHolderNameController.addListener(() => widget.formKey.currentState!.validate());
_cvvCodeController.addListener(() => widget.formKey.currentState!.validate());
}
@override
void dispose() {
cardHolderNode.dispose();
cvvFocusNode.dispose();
expiryDateNode.dispose();
super.dispose();
}
@override
void didChangeDependencies() {
themeColor = widget.themeColor;
super.didChangeDependencies();
}
@override
Widget build(BuildContext context) {
return Form(
key: widget.formKey,
child: SingleChildScrollView(
child: Column(
children: <Widget>[
Visibility(
visible: widget.isHolderNameVisible,
child: Container(
padding: const EdgeInsets.symmetric(vertical: 4.0),
margin: const EdgeInsets.only(left: 16, top: 8, right: 16),
child: TextFormField(
key: widget.cardHolderKey,
controller: _cardHolderNameController,
onChanged: (String value) {
setState(() {
cardHolderName = _cardHolderNameController.text.toUpperCase();
creditCardModel.cardHolderName = cardHolderName;
onCreditCardModelChange(creditCardModel);
});
},
textCapitalization: TextCapitalization.characters,
cursorColor: widget.cursorColor ?? themeColor,
focusNode: cardHolderNode,
style: widget.textStyle ?? TextStyle(color: widget.textColor),
decoration: widget.cardHolderDecoration,
keyboardType: TextInputType.text,
textInputAction: TextInputAction.done,
autofillHints: const <String>[AutofillHints.creditCardName],
onEditingComplete: () {
FocusScope.of(context).unfocus();
onCreditCardModelChange(creditCardModel);
widget.onFormComplete?.call();
},
validator: widget.cardNumberValidator ??
(String? value) {
if (value!.isEmpty) {
return widget.cardHolderNameValidationMessage;
}
return null;
},
),
),
),
Visibility(
visible: widget.isCardNumberVisible,
child: Container(
padding: const EdgeInsets.symmetric(vertical: 4.0),
margin: const EdgeInsets.only(left: 16, top: 8, right: 16),
child: TextFormField(
key: widget.cardNumberKey,
obscureText: showCardNumber,
controller: _cardNumberController,
onChanged: (String value) {
setState(() {
cardNumber = _cardNumberController.text;
creditCardModel.cardNumber = cardNumber;
onCreditCardModelChange(creditCardModel);
});
},
cursorColor: widget.cursorColor ?? themeColor,
onEditingComplete: () => FocusScope.of(context).requestFocus(expiryDateNode),
style: widget.textStyle ?? TextStyle(color: widget.textColor),
decoration: widget.cardNumberDecoration.copyWith(
suffixIcon: IconButton(
onPressed: () => setState(() => showCardNumber = !showCardNumber),
icon: Icon(
showCardNumber ? Icons.visibility : Icons.visibility_off,
color: const Color(0xFFDDDDE8),
)),
),
keyboardType: TextInputType.number,
textInputAction: TextInputAction.next,
autofillHints: widget.disableCardNumberAutoFillHints ? null : const <String>[AutofillHints.creditCardNumber],
autovalidateMode: widget.autovalidateMode,
validator: widget.cardNumberValidator ??
(String? value) {
// Validate less that 13 digits +3 white spaces
if (value!.isEmpty || value.length < 16) {
return widget.numberValidationMessage;
}
return null;
},
),
),
),
Row(
children: <Widget>[
Visibility(
visible: widget.isExpiryDateVisible,
child: Expanded(
child: Container(
padding: const EdgeInsets.symmetric(vertical: 8.0),
margin: const EdgeInsets.only(left: 16, top: 0, right: 16),
child: TextFormField(
key: widget.expiryDateKey,
controller: _expiryDateController,
onChanged: (String value) {
if (_expiryDateController.text.startsWith(RegExp('[2-9]'))) {
_expiryDateController.text = '0' + _expiryDateController.text;
}
setState(() {
expiryDate = _expiryDateController.text;
creditCardModel.expiryDate = expiryDate;
onCreditCardModelChange(creditCardModel);
});
},
cursorColor: widget.cursorColor ?? themeColor,
focusNode: expiryDateNode,
onEditingComplete: () {
FocusScope.of(context).requestFocus(cvvFocusNode);
},
style: widget.textStyle ?? TextStyle(color: widget.textColor),
decoration: widget.expiryDateDecoration,
keyboardType: TextInputType.number,
textInputAction: TextInputAction.next,
autofillHints: const <String>[AutofillHints.creditCardExpirationDate],
validator: widget.expiryDateValidator ??
(String? value) {
if (value!.isEmpty) {
return widget.dateValidationMessage;
}
final DateTime now = DateTime.now();
final List<String> date = value.split(RegExp(r'/'));
final int month = int.parse(date.first);
final int year = int.parse('20${date.last}');
final int lastDayOfMonth = month < 12 ? DateTime(year, month + 1, 0).day : DateTime(year + 1, 1, 0).day;
final DateTime cardDate = DateTime(year, month, lastDayOfMonth, 23, 59, 59, 999);
if (cardDate.isBefore(now) || month > 12 || month == 0) {
return widget.dateValidationMessage;
}
return null;
},
),
),
),
),
Expanded(
child: Visibility(
visible: widget.enableCvv,
child: Container(
padding: const EdgeInsets.symmetric(vertical: 4.0),
margin: const EdgeInsets.only(left: 16, top: 0, right: 16),
child: TextFormField(
key: widget.cvvCodeKey,
obscureText: widget.obscureCvv,
focusNode: cvvFocusNode,
controller: _cvvCodeController,
cursorColor: widget.cursorColor ?? themeColor,
onEditingComplete: () {
if (widget.isHolderNameVisible)
FocusScope.of(context).requestFocus(cardHolderNode);
else {
FocusScope.of(context).unfocus();
onCreditCardModelChange(creditCardModel);
widget.onFormComplete?.call();
}
},
style: widget.textStyle ?? TextStyle(color: widget.textColor),
decoration: widget.cvvCodeDecoration,
keyboardType: TextInputType.number,
textInputAction: widget.isHolderNameVisible ? TextInputAction.next : TextInputAction.done,
autofillHints: const <String>[AutofillHints.creditCardSecurityCode],
onChanged: (String text) {
setState(() {
cvvCode = text;
creditCardModel.cvvCode = cvvCode;
onCreditCardModelChange(creditCardModel);
});
},
validator: widget.cvvValidator ??
(String? value) {
if (value!.isEmpty || value.length < 3) {
return widget.cvvValidationMessage;
}
return null;
},
),
),
),
),
],
),
],
),
),
);
}
}