Skip to content

Commit

Permalink
✨ Obscure initial character in credit card widget
Browse files Browse the repository at this point in the history
  • Loading branch information
ujas-m-simformsolutions committed Mar 1, 2023
1 parent 6b2111a commit d3d513f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -171,6 +171,7 @@ This package's animation is inspired from from this [Dribbble](https://dribbble.
<td align="center"><a href="https://github.com/meetjanani"><img src="https://avatars.githubusercontent.com/u/32095359?s=100" width="100px;" alt=""/><br /><sub><b>Meet Janani</b></sub></a></td>
<td align="center"><a href="https://github.com/shwetachauhan-simform"><img src="https://avatars.githubusercontent.com/u/63042002?s=100" width="100px;" alt=""/><br /><sub><b>Shweta Chauhan</b></sub></a></td>
<td align="center"><a href="https://github.com/kavantrivedi"><img src="https://avatars.githubusercontent.com/u/97207242?s=100" width="100px;" alt=""/><br /><sub><b>Kavan Trivedi</b></sub></a></td>
<td align="center"><a href="https://github.com/Ujas-Majithiya"><img src="https://avatars.githubusercontent.com/u/56400956?v=4" width="100px;" alt=""/><br /><sub><b>Ujas Majithiya</b></sub></a></td>
</tr>
</table>
<br/>
Expand Down
2 changes: 1 addition & 1 deletion lib/credit_card_form.dart
Expand Up @@ -66,7 +66,7 @@ class CreditCardForm extends StatefulWidget {
/// Error message string when invalid expiry date is entered.
final String dateValidationMessage;

/// Error message string when credit card number is entered.
/// Error message string when invalid credit card number is entered.
final String numberValidationMessage;

/// Provides callback when there is any change in [CreditCardModel].
Expand Down
35 changes: 25 additions & 10 deletions lib/credit_card_widget.dart
Expand Up @@ -51,6 +51,7 @@ class CreditCardWidget extends StatefulWidget {
this.chipColor,
this.frontCardBorder,
this.backCardBorder,
this.obscureInitialCardNumber = false,
}) : super(key: key);

/// A string indicating number on the card.
Expand Down Expand Up @@ -92,6 +93,10 @@ class CreditCardWidget extends StatefulWidget {
/// doesn't get obscured. Defaults to true.
final bool obscureCardNumber;

/// Also obscures initial 4 card numbers with obscuring characters. This
/// flag requires [obscureCardNumber] to be true. This flag defaults to false.
final bool obscureInitialCardNumber;

/// If this flag is enabled then cvv is replaced with obscuring characters
/// to hide the content. Defaults to true.
final bool obscureCardCvv;
Expand Down Expand Up @@ -326,16 +331,26 @@ class _CreditCardWidgetState extends State<CreditCardWidget>
String number = widget.cardNumber;
if (widget.obscureCardNumber) {
final String stripped = number.replaceAll(RegExp(r'[^\d]'), '');
if (stripped.length > 8) {
final String middle = number
.substring(4, number.length - 5)
.trim()
.replaceAll(RegExp(r'\d'), '*');
number = stripped.substring(0, 4) +
' ' +
middle +
' ' +
stripped.substring(stripped.length - 4);
if (widget.obscureInitialCardNumber) {
if (stripped.length > 4) {
final String start = number
.substring(0, number.length - 5)
.trim()
.replaceAll(RegExp(r'\d'), '*');
number = start + ' ' + stripped.substring(stripped.length - 4);
}
} else {
if (stripped.length > 8) {
final String middle = number
.substring(4, number.length - 5)
.trim()
.replaceAll(RegExp(r'\d'), '*');
number = stripped.substring(0, 4) +
' ' +
middle +
' ' +
stripped.substring(stripped.length - 4);
}
}
}
return CardBackground(
Expand Down

0 comments on commit d3d513f

Please sign in to comment.