Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,42 @@ import 'package:flutter_credit_card/flutter_credit_card.dart';
),
```

### Clearing Form Data

To clear all the form data programmatically, you can use the `clearCardData()` method:

```dart
// Create a GlobalKey for the CreditCardForm
final GlobalKey<State<CreditCardForm>> creditCardFormKey = GlobalKey<State<CreditCardForm>>();

// Use the key with your CreditCardForm
CreditCardForm(
key: creditCardFormKey,
// ... other parameters
)

// Clear the form data
void clearForm() {
if (creditCardFormKey.currentState != null) {
(creditCardFormKey.currentState as dynamic).clearCardData();
}
// Also clear your local state variables
setState(() {
cardNumber = '';
expiryDate = '';
cardHolderName = '';
cvvCode = '';
isCvvFocused = false;
});
}
```

The `clearCardData()` method will:
- Clear all text field controllers (card number, expiry date, card holder name, CVV)
- Reset the credit card model
- Reset the focus state
- Trigger the `onCreditCardModelChange` callback to notify listeners

## How to use
Check out the **example** app in the [example](example) directory or the 'Example' tab on pub.dartlang.org for a more complete example.

Expand Down
123 changes: 89 additions & 34 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class MySampleState extends State<MySample> {
),
);
final GlobalKey<FormState> formKey = GlobalKey<FormState>();
final GlobalKey<State<CreditCardForm>> creditCardFormKey = GlobalKey<State<CreditCardForm>>();

@override
Widget build(BuildContext context) {
Expand Down Expand Up @@ -146,6 +147,7 @@ class MySampleState extends State<MySample> {
child: Column(
children: <Widget>[
CreditCardForm(
key: creditCardFormKey,
formKey: formKey,
obscureCvv: true,
obscureNumber: true,
Expand Down Expand Up @@ -237,44 +239,82 @@ class MySampleState extends State<MySample> {
),
),
const SizedBox(height: 20),
GestureDetector(
onTap: _onValidate,
child: Container(
margin: const EdgeInsets.symmetric(
horizontal: 16,
vertical: 8,
),
decoration: const BoxDecoration(
gradient: LinearGradient(
colors: <Color>[
AppColors.colorB58D67,
AppColors.colorB58D67,
AppColors.colorE5D1B2,
AppColors.colorF9EED2,
AppColors.colorEFEFED,
AppColors.colorF9EED2,
AppColors.colorB58D67,
],
begin: Alignment(-1, -4),
end: Alignment(1, 4),
),
borderRadius: BorderRadius.all(
Radius.circular(8),
Row(
children: <Widget>[
Expanded(
child: GestureDetector(
onTap: _onValidate,
child: Container(
margin: const EdgeInsets.symmetric(
horizontal: 16,
vertical: 8,
),
decoration: const BoxDecoration(
gradient: LinearGradient(
colors: <Color>[
AppColors.colorB58D67,
AppColors.colorB58D67,
AppColors.colorE5D1B2,
AppColors.colorF9EED2,
AppColors.colorEFEFED,
AppColors.colorF9EED2,
AppColors.colorB58D67,
],
begin: Alignment(-1, -4),
end: Alignment(1, 4),
),
borderRadius: BorderRadius.all(
Radius.circular(8),
),
),
padding:
const EdgeInsets.symmetric(vertical: 15),
alignment: Alignment.center,
child: const Text(
'Validate',
style: TextStyle(
color: Colors.black,
fontFamily: 'halter',
fontSize: 14,
package: 'flutter_credit_card',
),
),
),
),
),
padding:
const EdgeInsets.symmetric(vertical: 15),
alignment: Alignment.center,
child: const Text(
'Validate',
style: TextStyle(
color: Colors.black,
fontFamily: 'halter',
fontSize: 14,
package: 'flutter_credit_card',
Expanded(
child: GestureDetector(
onTap: _onClear,
child: Container(
margin: const EdgeInsets.symmetric(
horizontal: 16,
vertical: 8,
),
decoration: BoxDecoration(
border: Border.all(
color: AppColors.colorB58D67,
width: 2,
),
borderRadius: const BorderRadius.all(
Radius.circular(8),
),
),
padding:
const EdgeInsets.symmetric(vertical: 15),
alignment: Alignment.center,
child: const Text(
'Clear',
style: TextStyle(
color: AppColors.colorB58D67,
fontFamily: 'halter',
fontSize: 14,
package: 'flutter_credit_card',
),
),
),
),
),
),
],
),
],
),
Expand All @@ -298,6 +338,21 @@ class MySampleState extends State<MySample> {
}
}

void _onClear() {
// Call the clearCardData method on the form
if (creditCardFormKey.currentState != null) {
(creditCardFormKey.currentState as dynamic).clearCardData();
}
// Also clear the local state
setState(() {
cardNumber = '';
expiryDate = '';
cardHolderName = '';
cvvCode = '';
isCvvFocused = false;
});
}

Glassmorphism? _getGlassmorphismConfig() {
if (!useGlassMorphism) {
return null;
Expand Down
24 changes: 24 additions & 0 deletions lib/src/credit_card_form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -394,4 +394,28 @@ class _CreditCardFormState extends State<CreditCardForm> {
onCreditCardModelChange(creditCardModel);
widget.onFormComplete?.call();
}

/// Clears all the card data from the form fields and resets the credit card model.
void clearCardData() {
_cardNumberController.clear();
_expiryDateController.clear();
_cardHolderNameController.clear();
_cvvCodeController.clear();

setState(() {
cardNumber = '';
expiryDate = '';
cardHolderName = '';
cvvCode = '';
isCvvFocused = false;

creditCardModel.cardNumber = '';
creditCardModel.expiryDate = '';
creditCardModel.cardHolderName = '';
creditCardModel.cvvCode = '';
creditCardModel.isCvvFocused = false;

onCreditCardModelChange(creditCardModel);
});
}
}