Skip to content

Commit 25f3d4e

Browse files
authored
feat: Added creditCardType to control payment network logo (VISA, Amex, etc) (#11)
1 parent 0a60fb4 commit 25f3d4e

File tree

6 files changed

+110
-59
lines changed

6 files changed

+110
-59
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
All notable changes to this project will be documented in this file.
44

5+
## [1.0.7] 🛠️
6+
7+
### Feature
8+
9+
- 📝 Deprecated `disableShowingCardLogo:` property
10+
- Added `creditCardType` to override the logo, so setting `creditCardType: CreditCardTyoe.none` to disable showing card logo
11+
512
## [1.0.6] 🛠️
613

714
### Feature

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Add `u_credit_card` to your `pubspec.yaml`:
2929

3030
```yaml
3131
dependencies:
32-
u_credit_card: ^1.0.6
32+
u_credit_card: ^1.0.7
3333
```
3434
3535
Install it:
@@ -150,7 +150,6 @@ CreditCardUi(
150150
),
151151
```
152152

153-
If you want to disable the card logo you can pass `disableShowingCardLogo: true`, but this property was introduced after v1.0.6
154153

155154
#### Additional Customizations
156155
To further customize the card, you can add a background image by using the `backgroundDecorationImage` property. Additionally, you can include a logo for the card provider using the `cardProviderLogo` property. This logo can be positioned on either the left or the right side of the card using the `cardProviderLogoPosition` property.

lib/src/u_credit_card.dart

Lines changed: 75 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import 'package:u_credit_card/src/ui/credit_card_top_section_view.dart';
77
import 'package:u_credit_card/src/ui/credit_card_validity_view.dart';
88
import 'package:u_credit_card/src/utils/credit_card_helper.dart';
99

10-
/// Types of Cards
10+
/// Types of Cards.
1111
enum CardType {
1212
/// Credit Card
1313
credit,
@@ -25,22 +25,40 @@ enum CardType {
2525
other,
2626
}
2727

28-
/// Position of the Card Provider logo
29-
/// Left or Right in the top part of the card
28+
/// Types of payment network.
29+
enum CreditCardType {
30+
/// VISA
31+
visa,
32+
33+
/// Mastercard
34+
mastercard,
35+
36+
/// AMEX
37+
amex,
38+
39+
/// Discover
40+
discover,
41+
42+
/// None
43+
none,
44+
}
45+
46+
/// Position of the Card Provider logo.
47+
/// Left or Right in the top part of the card.
3048
enum CardProviderLogoPosition {
31-
/// Set the logo to the left side
49+
/// Set the logo to the left side.
3250
left,
3351

34-
/// Set the logo to the left side
52+
/// Set the logo to the left side.
3553
right;
3654

37-
/// Find if the logo is set to left or not
55+
/// Find if the logo is set to left or not.
3856
bool get isLeft => this == CardProviderLogoPosition.left;
3957
}
4058

41-
/// Creates Credit Card UI
59+
/// Creates Credit Card UI.
4260
class CreditCardUi extends StatelessWidget {
43-
/// Creates Credit Card UI
61+
/// Creates Credit Card UI.
4462
const CreditCardUi({
4563
super.key,
4664
required this.cardHolderFullName,
@@ -53,44 +71,46 @@ class CreditCardUi extends StatelessWidget {
5371
this.scale = 1.0,
5472
this.placeNfcIconAtTheEnd = false,
5573
this.cardType = CardType.credit,
74+
this.creditCardType,
5675
this.cardProviderLogo,
5776
this.cardProviderLogoPosition = CardProviderLogoPosition.right,
5877
this.backgroundDecorationImage,
78+
@Deprecated('Use `creditCardType: CreditCardType.none` instead')
5979
this.disableShowingCardLogo = false,
6080
});
6181

62-
/// Full Name of the Card Holder
82+
/// Full Name of the Card Holder.
6383
final String cardHolderFullName;
6484

65-
/// Full credit card number, can support asterisks
85+
/// Full credit card number, can support asterisks.
6686
final String cardNumber;
6787

6888
/// Enter valid from date of the card month and year like mm/yy,
6989
///
70-
/// Example 01/23, here 01 means month January & 23 means year 2023
71-
/// Optional field, can be skipped
90+
/// Example 01/23, here 01 means month January & 23 means year 2023.
91+
/// Optional field, can be skipped.
7292
final String? validFrom;
7393

74-
/// Enter validity of the card month and year like mm/yy,
94+
/// Enter validity of the card month and year like mm/yy.
7595
///
76-
/// Example 01/28, here 01 means month January & 28 means year 2028
96+
/// Example 01/28, here 01 means month January & 28 means year 2028.
7797
final String validThru;
7898

7999
/// Top Left Color for the Gradient,
80-
/// by default it's `Colors.purple`
100+
/// by default it's `Colors.purple`.
81101
///
82-
/// Tip: Avoid light colors, because texts are now white
102+
/// Tip: Avoid light colors, because texts are now white.
83103
final Color topLeftColor;
84104

85105
/// Bottom Left Color for the Gradient,
86-
/// by default it's deeper version of `topLeftColor`
106+
/// by default it's deeper version of `topLeftColor`.
87107
///
88-
/// Tip: Avoid light colors, because texts are now white
108+
/// Tip: Avoid light colors, because texts are now white.
89109
final Color? bottomRightColor;
90110

91111
/// Shows a NFC icon to tell user if the card supports NFC feature.
92112
///
93-
/// By default it is `true`
113+
/// By default it is `true`.
94114
final bool doesSupportNfc;
95115

96116
/// Places NFC icon at the opposite side of the chip,
@@ -101,39 +121,47 @@ class CreditCardUi extends StatelessWidget {
101121
/// so, icon will be beside the chip if nfc is enabled.
102122
final bool placeNfcIconAtTheEnd;
103123

104-
/// Can scale the credit card
124+
/// Can scale the credit card.
105125
///
106126
/// if you want reduce the size,
107-
/// set the value less than 1, else set greater than 1
127+
/// set the value less than 1, else set greater than 1.
108128
///
109-
/// By default the value is 1.0
129+
/// By default the value is 1.0.
110130
final double scale;
111131

112-
/// Provide the type of the card.
132+
/// Provide the type of the card - credit or debit.
113133
/// By default, it's `CardType.credit`
114134
///
115-
/// Set `CardType.other` if you don't want to set anything
135+
/// Set `CardType.other` if you don't want to set anything.
116136
final CardType cardType;
117137

138+
/// Set Credit card type to set network provider logo - VISA, Mastercard, etc.
139+
///
140+
/// Set `creditCardType: CreditCardType.none` to disable showing the logo.
141+
/// If this value is skipped, the card will show the logo automatically
142+
/// based on the `cardNumber`.
143+
final CreditCardType? creditCardType;
144+
118145
/// Provide the logo of the card provider (Optional).
119146
final Widget? cardProviderLogo;
120147

121148
/// Set the position of the card provider,
122-
/// by default, it is on the right
149+
/// by default, it is on the right.
123150
///
124-
/// Set `CardProviderLogoPosition.left` or `CardProviderLogoPosition.right`
151+
/// Set `CardProviderLogoPosition.left` or `CardProviderLogoPosition.right`.
125152
final CardProviderLogoPosition cardProviderLogoPosition;
126153

127-
/// Set Background image, can support both asset and network image
154+
/// Set Background image, can support both asset and network image.
128155
final DecorationImage? backgroundDecorationImage;
129156

130-
/// Disable card type logo, just set to `true`
157+
/// Disable credit card type logo, just set to `true`.
158+
@Deprecated('Use `creditCardType: CreditCardType.none` instead')
131159
final bool disableShowingCardLogo;
132160

133161
@override
134162
Widget build(BuildContext context) {
135163
final cardNumberMasked = CreditCardHelper.maskCreditCardNumber(
136-
cardNumber.replaceAll(' ', ''),
164+
cardNumber.replaceAll(' ', '').replaceAll('-', ''),
137165
);
138166

139167
final validFromMasked = validFrom == null
@@ -152,12 +180,24 @@ class CreditCardUi extends StatelessWidget {
152180
);
153181

154182
Widget cardLogoWidget;
155-
final cardLogoString = CreditCardHelper.getCardLogo(cardNumberMasked);
156-
if (disableShowingCardLogo || cardLogoString.isEmpty) {
183+
final cardLogoString = CreditCardHelper.getCardLogoFromCardNumber(
184+
cardNumber: cardNumberMasked,
185+
);
186+
187+
if (disableShowingCardLogo ||
188+
cardLogoString.isEmpty ||
189+
creditCardType == CreditCardType.none) {
157190
cardLogoWidget = const SizedBox.shrink();
191+
} else if (creditCardType != null) {
192+
cardLogoWidget = Image.asset(
193+
CreditCardHelper.getCardLogoFromType(creditCardType: creditCardType!),
194+
package: UiConstants.packageName,
195+
);
158196
} else {
159197
cardLogoWidget = Image.asset(
160-
CreditCardHelper.getCardLogo(cardNumberMasked),
198+
CreditCardHelper.getCardLogoFromCardNumber(
199+
cardNumber: cardNumberMasked,
200+
),
161201
package: UiConstants.packageName,
162202
);
163203
}
@@ -238,7 +278,9 @@ class CreditCardUi extends StatelessWidget {
238278
top: 108,
239279
left: 20,
240280
child: CreditCardText(
241-
cardNumberMasked,
281+
cardNumberMasked.length > 20
282+
? cardNumberMasked.substring(0, 20)
283+
: cardNumberMasked,
242284
),
243285
),
244286
],

lib/src/utils/credit_card_helper.dart

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'package:flutter/material.dart';
22
import 'package:u_credit_card/src/constants/assets.dart';
33
import 'package:u_credit_card/src/utils/utils.dart';
4+
import 'package:u_credit_card/u_credit_card.dart';
45

56
/// Helper class for Credit Card
67
class CreditCardHelper {
@@ -72,8 +73,8 @@ class CreditCardHelper {
7273
);
7374
}
7475

75-
/// Get Card Logo String based on Card Number
76-
static String getCardLogo(String cardNumber) {
76+
/// Get Card Logo String based on `cardNumber`
77+
static String getCardLogoFromCardNumber({required String cardNumber}) {
7778
final creditCard = CreditCard(cardNumber);
7879

7980
final cardType = creditCard.cardType;
@@ -87,7 +88,25 @@ class CreditCardHelper {
8788
return Assets.amexLogo;
8889
case CreditCardType.discover:
8990
return Assets.discoverLogo;
90-
case CreditCardType.other:
91+
case CreditCardType.none:
92+
return '';
93+
}
94+
}
95+
96+
/// Get Card Logo String based on [CreditCardType]
97+
static String getCardLogoFromType({required CreditCardType creditCardType}) {
98+
final cardType = creditCardType;
99+
100+
switch (cardType) {
101+
case CreditCardType.visa:
102+
return Assets.visaLogo;
103+
case CreditCardType.mastercard:
104+
return Assets.masterCardLogo;
105+
case CreditCardType.amex:
106+
return Assets.amexLogo;
107+
case CreditCardType.discover:
108+
return Assets.discoverLogo;
109+
case CreditCardType.none:
91110
return '';
92111
}
93112
}

lib/src/utils/utils.dart

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,4 @@
1-
///
2-
enum CreditCardType {
3-
///
4-
visa,
5-
6-
///
7-
mastercard,
8-
9-
///
10-
amex,
11-
12-
///
13-
discover,
14-
15-
///
16-
other,
17-
}
1+
import 'package:u_credit_card/u_credit_card.dart';
182

193
///
204
class CreditCard {
@@ -60,7 +44,7 @@ class CreditCard {
6044
final cardNumber = number.trim().replaceAll(RegExp('[^0-9]'), '');
6145

6246
if (cardNumber.isEmpty) {
63-
return CreditCardType.other;
47+
return CreditCardType.none;
6448
}
6549

6650
if (cardNumber.startsWith('4')) {
@@ -81,6 +65,6 @@ class CreditCard {
8165
return CreditCardType.discover;
8266
}
8367

84-
return CreditCardType.other;
68+
return CreditCardType.none;
8569
}
8670
}

pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
name: u_credit_card
22
description: uCreditCard - Easy to use beautiful Card UI Flutter Package.
3-
version: 1.0.6
3+
version: 1.0.7
44
homepage: 'https://github.com/utpal-barman/u-credit-card-flutter'
55

66
environment:
7-
sdk: ">=2.18.0 <3.0.0"
7+
sdk: ">=2.18.0 <4.0.0"
88

99

1010
dependencies:

0 commit comments

Comments
 (0)