0.0.3 released, see CHANGELOG.md

This commit is contained in:
Nathan Anderson 2023-11-21 09:45:25 -07:00
parent d4c697dde8
commit dd52ffb398
11 changed files with 535 additions and 267 deletions

2
.gitignore vendored
View File

@ -29,5 +29,3 @@ migrate_working_dir/
.packages .packages
build/ build/
# Example flutter app
example/

View File

@ -1,3 +1,18 @@
## 0.0.3
Lots of improvements!
- `CardTextField` now has customizable styles. Stripe integration is natively handled now, returning a card token, if stripe keys are provided.
- `README` revamped with emojis, screen recordings, the whole nine yards.
- `LICENSE` changed from BSD-3.0 to MIT license for pub points, I guess it wasn't being recognized correctly...
- Added `http` depency for handling Stripe token api call.
- Added Widget tests because that should be a thing that gets checked.
- Fix for backspacing on mobile not changing focus.
- Fix for text field spacing when in small form factor
- Much improved usability on mobile, added manually scrolling to element
- Added Icon Size param for card Provider Icon
## 0.0.2 ## 0.0.2
Added dartdoc comments for more pub points! Added dartdoc comments for more pub points!

12
LICENSE
View File

@ -1,11 +1,7 @@
Copyright 2023 Nathan Anderson Copyright 2023 - Nathan Anderson
BSD 3-Clause License Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
The names of its contributors may not be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

View File

@ -3,10 +3,41 @@ A simple and clean Stripe Element Card clone, rebuilt in native Flutter widgets.
This is not an officially maintained package by Stripe, but using the html stripe This is not an officially maintained package by Stripe, but using the html stripe
elements they provide with flutter is less than ideal. elements they provide with flutter is less than ideal.
## Features ## Features
- Card number validation Got to use emojis and taglines for attention grabbing and algorithm hacking:
- No more depending on Flutter Webview
⚡Blazingly fast ( its as fast as the rest of flutter )
🧹Cleaner ( fewer dependencies than the official stripe elements )
🛡Safe and Supports all Flutter Targets ( its native flutter with minimal dependencies )
Seemless UI/UX ( hard to match stripe quality, but I think I got close )
🔄Built-in Stripe Integration ( guess that one is obvious )
Chi Energy Boost ( alright I'm fishing... )
### Why StripeNativeCardField?
- Fewer dependencies: no more depending on Flutter Webview
- Customizable: the entire field can inherit native Flutter styling, i.e. `BoxDecoration()`
- Native Implementation: compiles and loads like the rest of your app, unlike embeded html
- Automatic validation: no `inputFormatters` or `RegExp` needed on your side
The card data can either be retrieved with the `onCardDetailsComplete` callback, or
you can have the element automatically create a Stripe card token when the fields
are filled out, and return the token with the `onTokenReceived` callback.
### Mobile
![mobile showcase](./example/loading.gif)
### Desktop
![desktop showcase](./example/loading.gif)
### Customizable
![cumstomization showcase](./example/loading.gif)
## Getting started ## Getting started
@ -16,10 +47,13 @@ elements they provide with flutter is less than ideal.
Include the package in a file: Include the package in a file:
```dart ```dart
import 'package:stripe_native_card_field/stripe_native_card_field.dart'; import 'package:stripe_native_card_field/stripe_native_card_field.dart';
``` ```
### For just Card Data
```dart ```dart
CardTextField( CardTextField(
width: 500, width: 500,
@ -30,6 +64,24 @@ CardTextField(
); );
``` ```
### For Stripe Token
```dart
CardTextField(
width: 500,
stripePublishableKey: 'pk_test_abc123', // Your stripe key here
onTokenReceived: (token) {
// Save the stripe token to send to your backend
setState(() => _token = token);
},
);
```
### Cumstomization
For documentation on all of the available customizable aspects of the `CardTextField`, go
to the [API docs here](https://pub.dev/documentation/stripe_native_card_field/latest/stripe_native_card_field/CardTextField-class.html).
## Additional information ## Additional information
Repository located [here](https://git.fosscat.com/n8r/stripe_native_card_field) Repository located [here](https://git.fosscat.com/n8r/stripe_native_card_field)

View File

@ -1,2 +1,5 @@
sdk.dir=/home/nate/Android/Sdk sdk.dir=/home/nate/Android/Sdk
flutter.sdk=/home/nate/Tooling/flutter flutter.sdk=/home/nate/Tooling/flutter
flutter.buildMode=debug
flutter.versionName=1.0.0
flutter.versionCode=1

View File

@ -14,23 +14,9 @@ class MyApp extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return MaterialApp( return MaterialApp(
title: 'Flutter Demo', debugShowCheckedModeBanner: false,
title: 'Native Stripe Field Demo',
theme: ThemeData( theme: ThemeData(
// This is the theme of your application.
//
// TRY THIS: Try running your application with "flutter run". You'll see
// the application has a blue toolbar. Then, without quitting the app,
// try changing the seedColor in the colorScheme below to Colors.green
// and then invoke "hot reload" (save your changes or press the "hot
// reload" button in a Flutter-supported IDE, or press "r" if you used
// the command line to start the app).
//
// Notice that the counter didn't reset back to zero; the application
// state is not lost during the reload. To reset the state, use hot
// restart instead.
//
// This works for code too, not just values: Most code changes can be
// tested with just a hot reload.
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true, useMaterial3: true,
), ),
@ -42,15 +28,6 @@ class MyApp extends StatelessWidget {
class MyHomePage extends StatefulWidget { class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title}); const MyHomePage({super.key, required this.title});
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title; final String title;
@override @override
@ -58,7 +35,7 @@ class MyHomePage extends StatefulWidget {
} }
class _MyHomePageState extends State<MyHomePage> { class _MyHomePageState extends State<MyHomePage> {
ValidState? state; CardDetailsValidState? state;
String? errorText; String? errorText;
@override @override
@ -79,11 +56,18 @@ class _MyHomePageState extends State<MyHomePage> {
), ),
), ),
CardTextField( CardTextField(
width: 500, width: 300,
stripePublishableKey: 'pk_test_abc123importantIDhere', onCardDetailsComplete: (details) {
onStripeResponse: (details) { if (kDebugMode) {
if (kDebugMode) print('Got card details: $details'); print(details);
}
}, },
// textStyle: TextStyle(fontSize: 24.0),
// cardFieldWidth: 260,
// expFieldWidth: 100.0,
// securityFieldWidth: 60.0,
// postalFieldWidth: 130.0,
// iconSize: Size(50.0, 35.0),
overrideValidState: state, overrideValidState: state,
errorText: errorText, errorText: errorText,
), ),
@ -91,7 +75,7 @@ class _MyHomePageState extends State<MyHomePage> {
child: const Text('Set manual error'), child: const Text('Set manual error'),
onPressed: () => setState(() { onPressed: () => setState(() {
errorText = 'There is a problem'; errorText = 'There is a problem';
state = ValidState.invalidCard; state = CardDetailsValidState.invalidCard;
}), }),
) )
], ],

BIN
example/loading.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

View File

@ -32,14 +32,14 @@ class CardDetails {
String? expirationString; String? expirationString;
DateTime? expirationDate; DateTime? expirationDate;
bool _complete = false; bool _complete = false;
ValidState _validState = ValidState.blank; CardDetailsValidState _validState = CardDetailsValidState.blank;
int _lastCheckHash = 0; int _lastCheckHash = 0;
CardProvider? provider; CardProvider? provider;
set overrideValidState(ValidState state) => _validState = state; set overrideValidState(CardDetailsValidState state) => _validState = state;
/// Checks the validity of the `CardDetails` and returns the result. /// Checks the validity of the `CardDetails` and returns the result.
ValidState get validState { CardDetailsValidState get validState {
checkIsValid(); checkIsValid();
return _validState; return _validState;
} }
@ -80,7 +80,7 @@ class CardDetails {
_lastCheckHash = currentHash; _lastCheckHash = currentHash;
if (_cardNumber == null && expirationString == null && securityCode == null && postalCode == null) { if (_cardNumber == null && expirationString == null && securityCode == null && postalCode == null) {
_complete = false; _complete = false;
_validState = ValidState.blank; _validState = CardDetailsValidState.blank;
return; return;
} }
final nums = _cardNumber! final nums = _cardNumber!
@ -92,71 +92,71 @@ class CardDetails {
.toList(); .toList();
if (!_luhnAlgorithmCheck(nums)) { if (!_luhnAlgorithmCheck(nums)) {
_complete = false; _complete = false;
_validState = ValidState.invalidCard; _validState = CardDetailsValidState.invalidCard;
return; return;
} }
if (_cardNumber == null || !cardNumberFilled) { if (_cardNumber == null || !cardNumberFilled) {
_complete = false; _complete = false;
_validState = ValidState.missingCard; _validState = CardDetailsValidState.missingCard;
return; return;
} }
if (expirationString == null) { if (expirationString == null) {
_complete = false; _complete = false;
_validState = ValidState.missingDate; _validState = CardDetailsValidState.missingDate;
return; return;
} }
final expSplits = expirationString!.split('/'); final expSplits = expirationString!.split('/');
if (expSplits.length != 2 || expSplits.last == '') { if (expSplits.length != 2 || expSplits.last == '') {
_complete = false; _complete = false;
_validState = ValidState.missingDate; _validState = CardDetailsValidState.missingDate;
return; return;
} }
final month = int.parse(expSplits.first[0] == '0' ? expSplits.first[1] : expSplits.first); final month = int.parse(expSplits.first[0] == '0' ? expSplits.first[1] : expSplits.first);
if (month < 1 || month > 12) { if (month < 1 || month > 12) {
_complete = false; _complete = false;
_validState = ValidState.invalidMonth; _validState = CardDetailsValidState.invalidMonth;
return; return;
} }
final year = 2000 + int.parse(expSplits.last); final year = 2000 + int.parse(expSplits.last);
final date = DateTime(year, month); final date = DateTime(year, month);
if (date.isBefore(DateTime.now())) { if (date.isBefore(DateTime.now())) {
_complete = false; _complete = false;
_validState = ValidState.dateTooEarly; _validState = CardDetailsValidState.dateTooEarly;
return; return;
} else if (date.isAfter(DateTime.now().add(const Duration(days: 365 * 50)))) { } else if (date.isAfter(DateTime.now().add(const Duration(days: 365 * 50)))) {
_complete = false; _complete = false;
_validState = ValidState.dateTooLate; _validState = CardDetailsValidState.dateTooLate;
return; return;
} }
expirationDate = date; expirationDate = date;
if (securityCode == null) { if (securityCode == null) {
_complete = false; _complete = false;
_validState = ValidState.missingCVC; _validState = CardDetailsValidState.missingCVC;
return; return;
} }
if (provider != null && securityCode!.length != provider!.cvcLength) { if (provider != null && securityCode!.length != provider!.cvcLength) {
_complete = false; _complete = false;
_validState = ValidState.invalidCVC; _validState = CardDetailsValidState.invalidCVC;
return; return;
} }
if (postalCode == null) { if (postalCode == null) {
_complete = false; _complete = false;
_validState = ValidState.missingZip; _validState = CardDetailsValidState.missingZip;
return; return;
} }
if (!RegExp(r'^\d{5}(-\d{4})?$').hasMatch(postalCode!)) { if (!RegExp(r'^\d{5}(-\d{4})?$').hasMatch(postalCode!)) {
_complete = false; _complete = false;
_validState = ValidState.invalidZip; _validState = CardDetailsValidState.invalidZip;
return; return;
} }
_complete = true; _complete = true;
_validState = ValidState.ok; _validState = CardDetailsValidState.ok;
} catch (err, st) { } catch (err, st) {
if (kDebugMode) { if (kDebugMode) {
print('Error while validating CardDetails: $err\n$st'); print('Error while validating CardDetails: $err\n$st');
} }
_complete = false; _complete = false;
_validState = ValidState.error; _validState = CardDetailsValidState.error;
} }
} }
@ -245,7 +245,7 @@ class CardDetails {
} }
/// Enum of validation states a `CardDetails` object can have. /// Enum of validation states a `CardDetails` object can have.
enum ValidState { enum CardDetailsValidState {
ok, ok,
error, error,
blank, blank,

View File

@ -7,9 +7,10 @@ import 'package:flutter_svg/flutter_svg.dart';
/// ///
/// To see a list of supported card providers, see `CardDetails.provider`. /// To see a list of supported card providers, see `CardDetails.provider`.
class CardProviderIcon extends StatefulWidget { class CardProviderIcon extends StatefulWidget {
const CardProviderIcon({required this.cardDetails, super.key}); const CardProviderIcon({required this.cardDetails, this.size, super.key});
final CardDetails? cardDetails; final CardDetails? cardDetails;
final Size? size;
@override @override
State<CardProviderIcon> createState() => _CardProviderIconState(); State<CardProviderIcon> createState() => _CardProviderIconState();
@ -34,22 +35,29 @@ class _CardProviderIconState extends State<CardProviderIcon> {
CardProviderID.jcb.name: CardProviderID.jcb.name:
'<svg enable-background="new 0 0 780 500" height="500" viewBox="0 0 780 500" width="780" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><linearGradient id="a" gradientTransform="matrix(132.87 0 0 323.02 -120270 -100930)" gradientUnits="userSpaceOnUse" x1="908.72" x2="909.72" y1="313.21" y2="313.21"><stop offset="0" stop-color="#007b40"/><stop offset="1" stop-color="#55b330"/></linearGradient><linearGradient id="b" gradientTransform="matrix(133.43 0 0 323.02 -121080 -100920)" gradientUnits="userSpaceOnUse" x1="908.73" x2="909.73" y1="313.21" y2="313.21"><stop offset="0" stop-color="#1d2970"/><stop offset="1" stop-color="#006dba"/></linearGradient><linearGradient id="c" gradientTransform="matrix(132.96 0 0 323.03 -120500 -100930)" gradientUnits="userSpaceOnUse" x1="908.72" x2="909.72" y1="313.21" y2="313.21"><stop offset="0" stop-color="#6e2b2f"/><stop offset="1" stop-color="#e30138"/></linearGradient><path d="m632.24 361.27c0 41.615-33.729 75.36-75.357 75.36h-409.13v-297.88c0-41.626 33.73-75.371 75.364-75.371h409.12l-.001 297.89z" fill="#fff"/><path d="m498.86 256.54c11.686.254 23.438-.516 35.077.4 11.787 2.199 14.628 20.043 4.156 25.887-7.145 3.85-15.633 1.434-23.379 2.113h-15.854zm41.834-32.145c2.596 9.164-6.238 17.392-15.064 16.13h-26.77c.188-8.642-.367-18.022.272-26.209 10.724.302 21.547-.616 32.209.48 4.581 1.151 8.415 4.917 9.353 9.599zm64.425-135.9c.498 17.501.072 35.927.215 53.783-.033 72.596.07 145.19-.057 217.79-.47 27.207-24.582 50.848-51.601 51.391-27.045.11-54.094.017-81.143.047v-109.75c29.471-.152 58.957.309 88.416-.23 13.666-.858 28.635-9.875 29.271-24.914 1.609-15.104-12.631-25.551-26.151-27.201-5.197-.135-5.045-1.515 0-2.117 12.895-2.787 23.021-16.133 19.227-29.499-3.233-14.058-18.771-19.499-31.695-19.472-26.352-.179-52.709-.025-79.062-.077.17-20.489-.355-41 .283-61.474 2.088-26.716 26.807-48.748 53.446-48.27 26.287-.004 52.57-.004 78.851-.005z" fill="url(#a)"/><path d="m174.74 139.54c.673-27.164 24.888-50.611 51.872-51.008 26.945-.083 53.894-.012 80.839-.036-.074 90.885.146 181.78-.111 272.66-1.038 26.834-24.989 49.834-51.679 50.309-26.996.098-53.995.014-80.992.041v-113.45c26.223 6.195 53.722 8.832 80.474 4.723 15.991-2.573 33.487-10.426 38.901-27.016 3.984-14.191 1.741-29.126 2.334-43.691v-33.825h-46.297c-.208 22.371.426 44.781-.335 67.125-1.248 13.734-14.849 22.46-27.802 21.994-16.064.17-47.897-11.642-47.897-11.642-.08-41.914.466-94.405.693-136.18z" fill="url(#b)"/><path d="m324.72 211.89c-2.437.517-.49-8.301-1.113-11.646.166-21.15-.347-42.323.283-63.458 2.082-26.829 26.991-48.916 53.738-48.288h78.768c-.074 90.885.145 181.78-.111 272.66-1.039 26.834-24.992 49.833-51.683 50.309-26.997.102-53.997.016-80.996.042v-124.3c18.439 15.129 43.5 17.484 66.472 17.525 17.318-.006 34.535-2.676 51.353-6.67v-22.772c-18.953 9.446-41.233 15.446-62.243 10.019-14.656-3.648-25.295-17.812-25.058-32.937-1.698-15.729 7.522-32.335 22.979-37.011 19.191-6.008 40.107-1.413 58.096 6.398 3.854 2.018 7.766 4.521 6.225-1.921v-17.899c-30.086-7.158-62.104-9.792-92.33-2.005-8.749 2.468-17.273 6.211-24.38 11.956z" fill="url(#c)"/></svg>', '<svg enable-background="new 0 0 780 500" height="500" viewBox="0 0 780 500" width="780" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><linearGradient id="a" gradientTransform="matrix(132.87 0 0 323.02 -120270 -100930)" gradientUnits="userSpaceOnUse" x1="908.72" x2="909.72" y1="313.21" y2="313.21"><stop offset="0" stop-color="#007b40"/><stop offset="1" stop-color="#55b330"/></linearGradient><linearGradient id="b" gradientTransform="matrix(133.43 0 0 323.02 -121080 -100920)" gradientUnits="userSpaceOnUse" x1="908.73" x2="909.73" y1="313.21" y2="313.21"><stop offset="0" stop-color="#1d2970"/><stop offset="1" stop-color="#006dba"/></linearGradient><linearGradient id="c" gradientTransform="matrix(132.96 0 0 323.03 -120500 -100930)" gradientUnits="userSpaceOnUse" x1="908.72" x2="909.72" y1="313.21" y2="313.21"><stop offset="0" stop-color="#6e2b2f"/><stop offset="1" stop-color="#e30138"/></linearGradient><path d="m632.24 361.27c0 41.615-33.729 75.36-75.357 75.36h-409.13v-297.88c0-41.626 33.73-75.371 75.364-75.371h409.12l-.001 297.89z" fill="#fff"/><path d="m498.86 256.54c11.686.254 23.438-.516 35.077.4 11.787 2.199 14.628 20.043 4.156 25.887-7.145 3.85-15.633 1.434-23.379 2.113h-15.854zm41.834-32.145c2.596 9.164-6.238 17.392-15.064 16.13h-26.77c.188-8.642-.367-18.022.272-26.209 10.724.302 21.547-.616 32.209.48 4.581 1.151 8.415 4.917 9.353 9.599zm64.425-135.9c.498 17.501.072 35.927.215 53.783-.033 72.596.07 145.19-.057 217.79-.47 27.207-24.582 50.848-51.601 51.391-27.045.11-54.094.017-81.143.047v-109.75c29.471-.152 58.957.309 88.416-.23 13.666-.858 28.635-9.875 29.271-24.914 1.609-15.104-12.631-25.551-26.151-27.201-5.197-.135-5.045-1.515 0-2.117 12.895-2.787 23.021-16.133 19.227-29.499-3.233-14.058-18.771-19.499-31.695-19.472-26.352-.179-52.709-.025-79.062-.077.17-20.489-.355-41 .283-61.474 2.088-26.716 26.807-48.748 53.446-48.27 26.287-.004 52.57-.004 78.851-.005z" fill="url(#a)"/><path d="m174.74 139.54c.673-27.164 24.888-50.611 51.872-51.008 26.945-.083 53.894-.012 80.839-.036-.074 90.885.146 181.78-.111 272.66-1.038 26.834-24.989 49.834-51.679 50.309-26.996.098-53.995.014-80.992.041v-113.45c26.223 6.195 53.722 8.832 80.474 4.723 15.991-2.573 33.487-10.426 38.901-27.016 3.984-14.191 1.741-29.126 2.334-43.691v-33.825h-46.297c-.208 22.371.426 44.781-.335 67.125-1.248 13.734-14.849 22.46-27.802 21.994-16.064.17-47.897-11.642-47.897-11.642-.08-41.914.466-94.405.693-136.18z" fill="url(#b)"/><path d="m324.72 211.89c-2.437.517-.49-8.301-1.113-11.646.166-21.15-.347-42.323.283-63.458 2.082-26.829 26.991-48.916 53.738-48.288h78.768c-.074 90.885.145 181.78-.111 272.66-1.039 26.834-24.992 49.833-51.683 50.309-26.997.102-53.997.016-80.996.042v-124.3c18.439 15.129 43.5 17.484 66.472 17.525 17.318-.006 34.535-2.676 51.353-6.67v-22.772c-18.953 9.446-41.233 15.446-62.243 10.019-14.656-3.648-25.295-17.812-25.058-32.937-1.698-15.729 7.522-32.335 22.979-37.011 19.191-6.008 40.107-1.413 58.096 6.398 3.854 2.018 7.766 4.521 6.225-1.921v-17.899c-30.086-7.158-62.104-9.792-92.33-2.005-8.749 2.468-17.273 6.211-24.38 11.956z" fill="url(#c)"/></svg>',
}; };
final double height = 20;
final double width = 30; late final Size _size;
@override
initState() {
super.initState();
_size = widget.size ?? const Size(30.0, 20.0);
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
late Widget child; late Widget child;
if (widget.cardDetails?.cardNumber != null && if (widget.cardDetails?.cardNumber != null &&
widget.cardDetails!.cardNumberFilled && widget.cardDetails!.cardNumberFilled &&
widget.cardDetails!.validState == ValidState.invalidCard) { widget.cardDetails!.validState == CardDetailsValidState.invalidCard) {
child = Padding( child = Padding(
padding: const EdgeInsets.symmetric(horizontal: 5.0), padding: const EdgeInsets.symmetric(horizontal: 5.0),
child: SvgPicture.string( child: SvgPicture.string(
key: const Key('invalid-card'), key: const Key('invalid-card'),
cardProviderSvg['error']!, cardProviderSvg['error']!,
height: height, height: _size.height,
width: width, width: _size.width,
), ),
); );
} else { } else {
@ -59,8 +67,8 @@ class _CardProviderIconState extends State<CardProviderIcon> {
child: SvgPicture.string( child: SvgPicture.string(
key: const Key('credit_card'), key: const Key('credit_card'),
cardProviderSvg['credit-card']!, cardProviderSvg['credit-card']!,
height: height, height: _size.height,
width: width, width: _size.width,
), ),
); );
} else { } else {
@ -86,8 +94,8 @@ class _CardProviderIconState extends State<CardProviderIcon> {
return SvgPicture.string( return SvgPicture.string(
key: Key('${id.name}-card'), key: Key('${id.name}-card'),
cardProviderSvg[id.name]!, cardProviderSvg[id.name]!,
height: height, height: _size.height,
width: width, width: _size.width,
); );
} }
} }

View File

@ -2,6 +2,8 @@ library stripe_native_card_field;
import 'dart:async'; import 'dart:async';
import 'dart:convert'; import 'dart:convert';
import 'dart:developer';
import 'dart:io';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
@ -28,9 +30,9 @@ enum CardEntryStep { number, exp, cvc, postal }
class CardTextField extends StatefulWidget { class CardTextField extends StatefulWidget {
CardTextField({ CardTextField({
Key? key, Key? key,
required this.width,
this.onStripeResponse, this.onStripeResponse,
this.onCardDetailsComplete, this.onCardDetailsComplete,
required this.width,
this.stripePublishableKey, this.stripePublishableKey,
this.height, this.height,
this.textStyle, this.textStyle,
@ -39,61 +41,89 @@ class CardTextField extends StatefulWidget {
this.boxDecoration, this.boxDecoration,
this.errorBoxDecoration, this.errorBoxDecoration,
this.loadingWidget, this.loadingWidget,
this.showInternalLoadingWidget = true,
this.delayToShowLoading = const Duration(milliseconds: 750), this.delayToShowLoading = const Duration(milliseconds: 750),
this.onCallToStripe,
this.overrideValidState, this.overrideValidState,
this.errorText, this.errorText,
this.cardFieldWidth,
this.expFieldWidth,
this.securityFieldWidth,
this.postalFieldWidth,
this.iconSize,
// this.loadingWidgetLocation = LoadingLocation.rightInside, // this.loadingWidgetLocation = LoadingLocation.rightInside,
}) : super(key: key) { }) : super(key: key) {
if (stripePublishableKey != null) { if (stripePublishableKey != null) {
assert(stripePublishableKey!.startsWith('pk_')); assert(stripePublishableKey!.startsWith('pk_'));
if (kReleaseMode && !stripePublishableKey!.startsWith('pk_live_')) { if (kReleaseMode && !stripePublishableKey!.startsWith('pk_live_')) {
print('StripeNativeCardField: *WARN* You are not using a live publishableKey in production.'); log('StripeNativeCardField: *WARN* You are not using a live publishableKey in production.');
} else if ((kDebugMode || kProfileMode) && stripePublishableKey!.startsWith('pk_live_')) { } else if ((kDebugMode || kProfileMode) && stripePublishableKey!.startsWith('pk_live_')) {
print( log('StripeNativeCardField: *WARN* You are using a live stripe key in a debug environment, proceed with caution!');
'StripeNativeCardField: *WARN* You are using a live stripe key in a debug environment, proceed with caution!'); log('StripeNativeCardField: *WARN* Ideally you should be using your test keys whenever not in production.');
print('StripeNativeCardField: *WARN* Ideally you should be using your test keys whenever not in production.');
} }
} else { } else {
if (onStripeResponse != null) { if (onStripeResponse != null) {
print( log('StripeNativeCardField: *ERROR* You provided the onTokenReceived callback, but did not provide a stripePublishableKey.');
'StripeNativeCardField: *ERROR* You provided the onTokenReceived callback, but did not provide a stripePublishableKey.');
assert(false); assert(false);
} }
} }
} }
/// Width of the entire CardTextField
final double width;
/// Height of the entire CardTextField, defaults to 60.0
final double? height;
/// Width of card number field, only override if changing the default `textStyle.fontSize`, defaults to 180.0
final double? cardFieldWidth;
/// Width of expiration date field, only override if changing the default `textStyle.fontSize`, defaults to 70.0
final double? expFieldWidth;
/// Width of security number field, only override if changing the default `textStyle.fontSize`, defaults to 40.0
final double? securityFieldWidth;
/// Width of postal code field, only override if changing the default `textStyle.fontSize`, defaults to 95.0
final double? postalFieldWidth;
/// Overrides the default box decoration of the text field /// Overrides the default box decoration of the text field
final BoxDecoration? boxDecoration; final BoxDecoration? boxDecoration;
/// Overrides the default box decoration of the text field when there is a validation error /// Overrides the default box decoration of the text field when there is a validation error
final BoxDecoration? errorBoxDecoration; final BoxDecoration? errorBoxDecoration;
/// Width of the entire CardTextField
final double width;
/// Height of the entire CardTextField
final double? height;
/// Stripe publishable key, starts with 'pk_'
final String? stripePublishableKey;
/// Shown and overrides CircularProgressIndicator() if the request to stripe takes longer than `delayToShowLoading` /// Shown and overrides CircularProgressIndicator() if the request to stripe takes longer than `delayToShowLoading`
final Widget? loadingWidget; final Widget? loadingWidget;
/// Overrides default icon size of the card provider, defaults to `Size(30.0, 20.0)`
final Size? iconSize;
/// Determines where the loading indicator appears when contacting stripe
// final LoadingLocation loadingWidgetLocation;
/// Default TextStyle /// Default TextStyle
final TextStyle? textStyle; final TextStyle? textStyle;
/// Default TextStyle for the hint text in each TextFormField /// Default TextStyle for the hint text in each TextFormField.
/// If null, inherits from the `textStyle`.
final TextStyle? hintTextStyle; final TextStyle? hintTextStyle;
/// TextStyle used when any TextFormField's have a validation error /// TextStyle used when any TextFormField's have a validation error
/// If null, inherits from the `textStyle`.
final TextStyle? errorTextStyle; final TextStyle? errorTextStyle;
/// Time to wait until showing the loading indicator when retrieving Stripe token /// Time to wait until showing the loading indicator when retrieving Stripe token
final Duration delayToShowLoading; final Duration delayToShowLoading;
/// Determines where the loading indicator appears when contacting stripe /// Whether to show the internal loading widget on calls to Stripe
// final LoadingLocation loadingWidgetLocation; final bool showInternalLoadingWidget;
/// Stripe publishable key, starts with 'pk_'
final String? stripePublishableKey;
/// Callback when the http request is made to Stripe
final void Function()? onCallToStripe;
/// Callback that returns the stripe token for the card /// Callback that returns the stripe token for the card
final void Function(Map<String, dynamic>)? onStripeResponse; final void Function(Map<String, dynamic>)? onStripeResponse;
@ -102,7 +132,7 @@ class CardTextField extends StatefulWidget {
final void Function(CardDetails)? onCardDetailsComplete; final void Function(CardDetails)? onCardDetailsComplete;
/// Can manually override the ValidState to surface errors returned from Stripe /// Can manually override the ValidState to surface errors returned from Stripe
final ValidState? overrideValidState; final CardDetailsValidState? overrideValidState;
/// Can manually override the errorText displayed to surface errors returned from Stripe /// Can manually override the errorText displayed to surface errors returned from Stripe
final String? errorText; final String? errorText;
@ -137,16 +167,32 @@ class CardTextFieldState extends State<CardTextField> {
late final TextStyle _normalTextStyle; late final TextStyle _normalTextStyle;
late final TextStyle _hintTextSyle; late final TextStyle _hintTextSyle;
final double _cardFieldWidth = 180.0; /// Width of the card number text field
final double _expirationFieldWidth = 70.0; late final double _cardFieldWidth;
final double _securityFieldWidth = 40.0;
final double _postalFieldWidth = 95.0; /// Width of the expiration text field
late final double _expirationFieldWidth;
/// Width of the security code text field
late final double _securityFieldWidth;
/// Width of the postal code text field
late final double _postalFieldWidth;
/// Width of the internal scrollable field, is potentially larger than the provided `widget.width`
late final double _internalFieldWidth; late final double _internalFieldWidth;
/// Width of the gap between card number and expiration text fields when expanded
late final double _expanderWidthExpanded; late final double _expanderWidthExpanded;
late final double _expanderWidthContracted;
/// Width of the gap between card number and expiration text fields when collapsed
late final double _expanderWidthCollapsed;
String? _validationErrorText; String? _validationErrorText;
bool _showBorderError = false; bool _showBorderError = false;
final _isMobile = Platform.isAndroid || Platform.isIOS;
/// If a request to Stripe is being made
bool _loading = false; bool _loading = false;
final CardDetails _cardDetails = CardDetails.blank(); final CardDetails _cardDetails = CardDetails.blank();
int _prevErrorOverrideHash = 0; int _prevErrorOverrideHash = 0;
@ -159,19 +205,32 @@ class CardTextFieldState extends State<CardTextField> {
@override @override
void initState() { void initState() {
_cardFieldWidth = widget.cardFieldWidth ?? 180.0;
_expirationFieldWidth = widget.expFieldWidth ?? 70.0;
_securityFieldWidth = widget.securityFieldWidth ?? 40.0;
_postalFieldWidth = widget.postalFieldWidth ?? 95.0;
// No way to get backspace events on soft keyboards, so add invisible character to detect delete
_cardNumberController = TextEditingController(); _cardNumberController = TextEditingController();
_expirationController = TextEditingController(); _expirationController = TextEditingController(text: _isMobile ? '\u200b' : '');
_securityCodeController = TextEditingController(); _securityCodeController = TextEditingController(text: _isMobile ? '\u200b' : '');
_postalCodeController = TextEditingController(); _postalCodeController = TextEditingController(text: _isMobile ? '\u200b' : '');
// Otherwise, use `RawKeyboard` listener
if (!_isMobile) {
RawKeyboard.instance.addListener(_backspaceTransitionListener);
}
cardNumberFocusNode = FocusNode(); cardNumberFocusNode = FocusNode();
expirationFocusNode = FocusNode(); expirationFocusNode = FocusNode();
securityCodeFocusNode = FocusNode(); securityCodeFocusNode = FocusNode();
postalCodeFocusNode = FocusNode(); postalCodeFocusNode = FocusNode();
_errorTextStyle = const TextStyle(color: Colors.red, fontSize: 14, inherit: true).merge(widget.errorTextStyle); _errorTextStyle = const TextStyle(color: Colors.red, fontSize: 14, inherit: true)
.merge(widget.errorTextStyle ?? widget.textStyle);
_normalTextStyle = const TextStyle(color: Colors.black87, fontSize: 14, inherit: true).merge(widget.textStyle); _normalTextStyle = const TextStyle(color: Colors.black87, fontSize: 14, inherit: true).merge(widget.textStyle);
_hintTextSyle = const TextStyle(color: Colors.black54, fontSize: 14, inherit: true).merge(widget.hintTextStyle); _hintTextSyle = const TextStyle(color: Colors.black54, fontSize: 14, inherit: true)
.merge(widget.hintTextStyle ?? widget.textStyle);
_normalBoxDecoration = BoxDecoration( _normalBoxDecoration = BoxDecoration(
color: const Color(0xfff6f9fc), color: const Color(0xfff6f9fc),
@ -212,12 +271,13 @@ class CardTextFieldState extends State<CardTextField> {
_currentCardEntryStepController.stream.listen( _currentCardEntryStepController.stream.listen(
_onStepChange, _onStepChange,
); );
RawKeyboard.instance.addListener(_backspaceTransitionListener);
isWideFormat = widget.width >= 450; isWideFormat =
widget.width >= _cardFieldWidth + _expirationFieldWidth + _securityFieldWidth + _postalFieldWidth + 60.0;
if (isWideFormat) { if (isWideFormat) {
_internalFieldWidth = widget.width + _postalFieldWidth + 35; _internalFieldWidth = widget.width + _postalFieldWidth + 35;
_expanderWidthExpanded = widget.width - _cardFieldWidth - _expirationFieldWidth - _securityFieldWidth - 35; _expanderWidthExpanded = widget.width - _cardFieldWidth - _expirationFieldWidth - _securityFieldWidth - 35;
_expanderWidthContracted = _expanderWidthCollapsed =
widget.width - _cardFieldWidth - _expirationFieldWidth - _securityFieldWidth - _postalFieldWidth - 70; widget.width - _cardFieldWidth - _expirationFieldWidth - _securityFieldWidth - _postalFieldWidth - 70;
} else { } else {
_internalFieldWidth = _cardFieldWidth + _expirationFieldWidth + _securityFieldWidth + _postalFieldWidth + 80; _internalFieldWidth = _cardFieldWidth + _expirationFieldWidth + _securityFieldWidth + _postalFieldWidth + 80;
@ -236,7 +296,9 @@ class CardTextFieldState extends State<CardTextField> {
expirationFocusNode.dispose(); expirationFocusNode.dispose();
securityCodeFocusNode.dispose(); securityCodeFocusNode.dispose();
if (!_isMobile) {
RawKeyboard.instance.removeListener(_backspaceTransitionListener); RawKeyboard.instance.removeListener(_backspaceTransitionListener);
}
super.dispose(); super.dispose();
} }
@ -259,6 +321,30 @@ class CardTextFieldState extends State<CardTextField> {
// Focuses to the current field // Focuses to the current field
_currentCardEntryStepController.add(_currentStep); _currentCardEntryStepController.add(_currentStep);
}, },
// Enable scrolling on mobile and if its narrow (not all fields visible)
onHorizontalDragUpdate: (details) {
if (!_isMobile || isWideFormat) return;
final newOffset = _horizontalScrollController.offset - details.delta.dx;
final max = _horizontalScrollController.position.maxScrollExtent;
if (newOffset < -30.0) {
_horizontalScrollController.jumpTo(-30.0);
} else if (newOffset > max + 30.0) {
_horizontalScrollController.jumpTo(max + 30.0);
} else {
_horizontalScrollController.jumpTo(newOffset);
}
},
onHorizontalDragEnd: (details) {
if (!_isMobile || isWideFormat || details.primaryVelocity == null) return;
const dur = Duration(milliseconds: 300);
const cur = Curves.ease;
// final max = _horizontalScrollController.position.maxScrollExtent;
final newOffset = _horizontalScrollController.offset - details.primaryVelocity! * 0.15;
_horizontalScrollController.animateTo(newOffset, curve: cur, duration: dur);
},
child: Container( child: Container(
width: widget.width, width: widget.width,
height: widget.height ?? 60.0, height: widget.height ?? 60.0,
@ -279,6 +365,7 @@ class CardTextFieldState extends State<CardTextField> {
padding: const EdgeInsets.symmetric(horizontal: 6.0), padding: const EdgeInsets.symmetric(horizontal: 6.0),
child: CardProviderIcon( child: CardProviderIcon(
cardDetails: _cardDetails, cardDetails: _cardDetails,
size: widget.iconSize,
), ),
), ),
SizedBox( SizedBox(
@ -288,7 +375,11 @@ class CardTextFieldState extends State<CardTextField> {
focusNode: cardNumberFocusNode, focusNode: cardNumberFocusNode,
controller: _cardNumberController, controller: _cardNumberController,
keyboardType: TextInputType.number, keyboardType: TextInputType.number,
style: _isRedText([ValidState.invalidCard, ValidState.missingCard, ValidState.blank]) style: _isRedText([
CardDetailsValidState.invalidCard,
CardDetailsValidState.missingCard,
CardDetailsValidState.blank
])
? _errorTextStyle ? _errorTextStyle
: _normalTextStyle, : _normalTextStyle,
validator: (content) { validator: (content) {
@ -296,9 +387,9 @@ class CardTextFieldState extends State<CardTextField> {
return null; return null;
} }
_cardDetails.cardNumber = content; _cardDetails.cardNumber = content;
if (_cardDetails.validState == ValidState.invalidCard) { if (_cardDetails.validState == CardDetailsValidState.invalidCard) {
_setValidationState('Your card number is invalid.'); _setValidationState('Your card number is invalid.');
} else if (_cardDetails.validState == ValidState.missingCard) { } else if (_cardDetails.validState == CardDetailsValidState.missingCard) {
_setValidationState('Your card number is incomplete.'); _setValidationState('Your card number is incomplete.');
} }
return null; return null;
@ -340,7 +431,7 @@ class CardTextFieldState extends State<CardTextField> {
Size(_expanderWidthExpanded, 0.0), Size(_expanderWidthExpanded, 0.0),
) )
: BoxConstraints.tight( : BoxConstraints.tight(
Size(_expanderWidthContracted, 0.0), Size(_expanderWidthCollapsed, 0.0),
), ),
), ),
), ),
@ -348,36 +439,56 @@ class CardTextFieldState extends State<CardTextField> {
// Spacer(flex: _currentStep == CardEntryStep.number ? 100 : 1), // Spacer(flex: _currentStep == CardEntryStep.number ? 100 : 1),
SizedBox( SizedBox(
width: _expirationFieldWidth, width: _expirationFieldWidth,
child: TextFormField( child: Stack(
alignment: Alignment.centerLeft,
children: [
// Must manually add hint label because they wont show on mobile with backspace hack
if (_isMobile && _expirationController.text == '\u200b')
Text('MM/YY', style: _hintTextSyle),
TextFormField(
key: const Key('expiration_field'), key: const Key('expiration_field'),
focusNode: expirationFocusNode, focusNode: expirationFocusNode,
controller: _expirationController, controller: _expirationController,
keyboardType: TextInputType.number,
style: _isRedText([ style: _isRedText([
ValidState.dateTooLate, CardDetailsValidState.dateTooLate,
ValidState.dateTooEarly, CardDetailsValidState.dateTooEarly,
ValidState.missingDate, CardDetailsValidState.missingDate,
ValidState.invalidMonth CardDetailsValidState.invalidMonth
]) ])
? _errorTextStyle ? _errorTextStyle
: _normalTextStyle, : _normalTextStyle,
validator: (content) { validator: (content) {
if (content == null || content.isEmpty) { if (content == null || content.isEmpty || _isMobile && content == '\u200b') {
return null; return null;
} }
if (_isMobile) {
setState(() => _cardDetails.expirationString = content.replaceAll('\u200b', ''));
} else {
setState(() => _cardDetails.expirationString = content); setState(() => _cardDetails.expirationString = content);
if (_cardDetails.validState == ValidState.dateTooEarly) { }
if (_cardDetails.validState == CardDetailsValidState.dateTooEarly) {
_setValidationState('Your card\'s expiration date is in the past.'); _setValidationState('Your card\'s expiration date is in the past.');
} else if (_cardDetails.validState == ValidState.dateTooLate) { } else if (_cardDetails.validState == CardDetailsValidState.dateTooLate) {
_setValidationState('Your card\'s expiration year is invalid.'); _setValidationState('Your card\'s expiration year is invalid.');
} else if (_cardDetails.validState == ValidState.missingDate) { } else if (_cardDetails.validState == CardDetailsValidState.missingDate) {
_setValidationState('You must include your card\'s expiration date.'); _setValidationState('You must include your card\'s expiration date.');
} else if (_cardDetails.validState == ValidState.invalidMonth) { } else if (_cardDetails.validState == CardDetailsValidState.invalidMonth) {
_setValidationState('Your card\'s expiration month is invalid.'); _setValidationState('Your card\'s expiration month is invalid.');
} }
return null; return null;
}, },
onChanged: (str) { onChanged: (str) {
if (_isMobile) {
if (str.isEmpty) {
_backspacePressed();
}
setState(() => _cardDetails.expirationString = str.replaceAll('\u200b', ''));
} else {
setState(() => _cardDetails.expirationString = str); setState(() => _cardDetails.expirationString = str);
}
if (str.length == 5) { if (str.length == 5) {
_currentCardEntryStepController.add(CardEntryStep.cvc); _currentCardEntryStepController.add(CardEntryStep.cvc);
} }
@ -390,37 +501,63 @@ class CardTextFieldState extends State<CardTextField> {
], ],
decoration: InputDecoration( decoration: InputDecoration(
contentPadding: EdgeInsets.zero, contentPadding: EdgeInsets.zero,
hintText: 'MM/YY', hintText: _isMobile ? '' : 'MM/YY',
hintStyle: _hintTextSyle, hintStyle: _hintTextSyle,
fillColor: Colors.transparent, fillColor: Colors.transparent,
border: InputBorder.none, border: InputBorder.none,
), ),
), ),
],
),
), ),
SizedBox( SizedBox(
width: _securityFieldWidth, width: _securityFieldWidth,
child: TextFormField( child: Stack(
alignment: Alignment.centerLeft,
children: [
if (_isMobile && _securityCodeController.text == '\u200b')
Text(
'CVC',
style: _hintTextSyle,
),
TextFormField(
key: const Key('security_field'), key: const Key('security_field'),
focusNode: securityCodeFocusNode, focusNode: securityCodeFocusNode,
controller: _securityCodeController, controller: _securityCodeController,
style: _isRedText([ValidState.invalidCVC, ValidState.missingCVC]) keyboardType: TextInputType.number,
style:
_isRedText([CardDetailsValidState.invalidCVC, CardDetailsValidState.missingCVC])
? _errorTextStyle ? _errorTextStyle
: _normalTextStyle, : _normalTextStyle,
validator: (content) { validator: (content) {
if (content == null || content.isEmpty) { if (content == null || content.isEmpty || _isMobile && content == '\u200b') {
return null; return null;
} }
if (_isMobile) {
setState(() => _cardDetails.securityCode = content.replaceAll('\u200b', ''));
} else {
setState(() => _cardDetails.securityCode = content); setState(() => _cardDetails.securityCode = content);
if (_cardDetails.validState == ValidState.invalidCVC) { }
if (_cardDetails.validState == CardDetailsValidState.invalidCVC) {
_setValidationState('Your card\'s security code is invalid.'); _setValidationState('Your card\'s security code is invalid.');
} else if (_cardDetails.validState == ValidState.missingCVC) { } else if (_cardDetails.validState == CardDetailsValidState.missingCVC) {
_setValidationState('Your card\'s security code is incomplete.'); _setValidationState('Your card\'s security code is incomplete.');
} }
return null; return null;
}, },
onFieldSubmitted: (_) => _currentCardEntryStepController.add(CardEntryStep.postal), onFieldSubmitted: (_) => _currentCardEntryStepController.add(CardEntryStep.postal),
onChanged: (str) { onChanged: (str) {
if (_isMobile) {
if (str.isEmpty) {
_backspacePressed();
}
setState(() => _cardDetails.expirationString = str.replaceAll('\u200b', ''));
} else {
setState(() => _cardDetails.expirationString = str); setState(() => _cardDetails.expirationString = str);
}
if (str.length == _cardDetails.provider?.cvcLength) { if (str.length == _cardDetails.provider?.cvcLength) {
_currentCardEntryStepController.add(CardEntryStep.postal); _currentCardEntryStepController.add(CardEntryStep.postal);
} }
@ -432,40 +569,108 @@ class CardTextFieldState extends State<CardTextField> {
], ],
decoration: InputDecoration( decoration: InputDecoration(
contentPadding: EdgeInsets.zero, contentPadding: EdgeInsets.zero,
hintText: 'CVC', hintText: _isMobile ? '' : 'CVC',
hintStyle: _hintTextSyle, hintStyle: _hintTextSyle,
fillColor: Colors.transparent, fillColor: Colors.transparent,
border: InputBorder.none, border: InputBorder.none,
), ),
), ),
],
),
), ),
SizedBox( SizedBox(
width: _postalFieldWidth, width: _postalFieldWidth,
child: TextFormField( child: Stack(
alignment: Alignment.centerLeft,
children: [
if (_isMobile && _postalCodeController.text == '\u200b')
Text(
'Postal Code',
style: _hintTextSyle,
),
TextFormField(
key: const Key('postal_field'), key: const Key('postal_field'),
focusNode: postalCodeFocusNode, focusNode: postalCodeFocusNode,
controller: _postalCodeController, controller: _postalCodeController,
style: _isRedText([ValidState.invalidZip, ValidState.missingZip]) keyboardType: TextInputType.number,
style:
_isRedText([CardDetailsValidState.invalidZip, CardDetailsValidState.missingZip])
? _errorTextStyle ? _errorTextStyle
: _normalTextStyle, : _normalTextStyle,
validator: (content) { validator: (content) {
if (content == null || content.isEmpty) { if (content == null || content.isEmpty || _isMobile && content == '\u200b') {
return null; return null;
} }
setState(() => _cardDetails.postalCode = content);
if (_cardDetails.validState == ValidState.invalidZip) { if (_isMobile) {
setState(() => _cardDetails.postalCode = content.replaceAll('\u200b', ''));
} else {
setState(() => _cardDetails.postalCode = content);
}
if (_cardDetails.validState == CardDetailsValidState.invalidZip) {
_setValidationState('The postal code you entered is not correct.'); _setValidationState('The postal code you entered is not correct.');
} else if (_cardDetails.validState == ValidState.missingZip) { } else if (_cardDetails.validState == CardDetailsValidState.missingZip) {
_setValidationState('You must enter your card\'s postal code.'); _setValidationState('You must enter your card\'s postal code.');
} }
return null; return null;
}, },
onChanged: (str) { onChanged: (str) {
if (_isMobile) {
if (str.isEmpty) {
_backspacePressed();
}
setState(() => _cardDetails.postalCode = str.replaceAll('\u200b', ''));
} else {
setState(() => _cardDetails.postalCode = str); setState(() => _cardDetails.postalCode = str);
}
}, },
textInputAction: TextInputAction.done, textInputAction: TextInputAction.done,
onFieldSubmitted: (_) async { onFieldSubmitted: (_) {
_postalFieldSubmitted();
},
decoration: InputDecoration(
contentPadding: EdgeInsets.zero,
hintText: _isMobile ? '' : 'Postal Code',
hintStyle: _hintTextSyle,
fillColor: Colors.transparent,
border: InputBorder.none,
),
),
],
),
),
AnimatedOpacity(
duration: const Duration(milliseconds: 300),
opacity: _loading && widget.showInternalLoadingWidget ? 1.0 : 0.0,
child: widget.loadingWidget ?? const CircularProgressIndicator(),
),
],
),
),
),
),
),
),
),
),
AnimatedOpacity(
duration: const Duration(milliseconds: 525),
opacity: _validationErrorText == null ? 0.0 : 1.0,
child: Padding(
padding: const EdgeInsets.only(top: 8.0, left: 14.0),
child: Text(
// Spacing changes by like a pixel if its an empty string, slight jitter when error appears and disappears
_validationErrorText ?? ' ',
style: const TextStyle(color: Colors.red),
),
),
),
],
);
}
Future<void> _postalFieldSubmitted() async {
_validateFields(); _validateFields();
if (_cardDetails.isComplete) { if (_cardDetails.isComplete) {
if (widget.onCardDetailsComplete != null) { if (widget.onCardDetailsComplete != null) {
@ -479,6 +684,8 @@ class CardTextFieldState extends State<CardTextField> {
); );
const stripeCardUrl = 'https://api.stripe.com/v1/tokens'; const stripeCardUrl = 'https://api.stripe.com/v1/tokens';
// Callback that stripe call is being made
if (widget.onCallToStripe != null) widget.onCallToStripe!();
final response = await http.post( final response = await http.post(
Uri.parse(stripeCardUrl), Uri.parse(stripeCardUrl),
body: { body: {
@ -499,48 +706,11 @@ class CardTextFieldState extends State<CardTextField> {
if (_loading) setState(() => _loading = false); if (_loading) setState(() => _loading = false);
} }
} }
},
decoration: InputDecoration(
contentPadding: EdgeInsets.zero,
hintText: 'Postal Code',
hintStyle: _hintTextSyle,
fillColor: Colors.transparent,
border: InputBorder.none,
),
),
),
AnimatedOpacity(
duration: const Duration(milliseconds: 300),
opacity: _loading ? 1.0 : 0.0,
child: widget.loadingWidget ?? const CircularProgressIndicator(),
),
],
),
),
),
),
),
),
),
),
AnimatedOpacity(
duration: const Duration(milliseconds: 525),
opacity: _validationErrorText == null ? 0.0 : 1.0,
child: Padding(
padding: const EdgeInsets.only(top: 8.0, left: 14.0),
child: Text(
_validationErrorText ?? '',
style: const TextStyle(color: Colors.red),
),
),
),
],
);
} }
/// Provided a list of `ValidState`, returns whether /// Provided a list of `ValidState`, returns whether
/// make the text field red /// make the text field red
bool _isRedText(List<ValidState> args) { bool _isRedText(List<CardDetailsValidState> args) {
return _showBorderError && args.contains(_cardDetails.validState); return _showBorderError && args.contains(_cardDetails.validState);
} }
@ -623,6 +793,23 @@ class CardTextFieldState extends State<CardTextField> {
if (!isWideFormat) { if (!isWideFormat) {
_scrollRow(step); _scrollRow(step);
} }
// If mobile, and keyboard is closed, unfocus, to allow refocus
// print(MediaQuery.of(context).viewInsets.bottom);
// if (_isMobile && _hasFocus() && MediaQuery.of(context).viewInsets.bottom == 0.0) {
// cardNumberFocusNode.unfocus();
// expirationFocusNode.unfocus();
// securityCodeFocusNode.unfocus();
// postalCodeFocusNode.unfocus();
// }
}
/// Returns true if any field in the `CardTextField` has focus.
// ignore: unused_element
bool _hasFocus() {
return cardNumberFocusNode.hasFocus ||
expirationFocusNode.hasFocus ||
securityCodeFocusNode.hasFocus ||
postalCodeFocusNode.hasFocus;
} }
/// Function that is listening to the keyboard events. /// Function that is listening to the keyboard events.
@ -638,24 +825,49 @@ class CardTextFieldState extends State<CardTextField> {
case CardEntryStep.number: case CardEntryStep.number:
break; break;
case CardEntryStep.exp: case CardEntryStep.exp:
final expStr = _expirationController.text; if (_expirationController.text.isNotEmpty) break;
if (expStr.isNotEmpty) break; case CardEntryStep.cvc:
if (_securityCodeController.text.isNotEmpty) break;
case CardEntryStep.postal:
if (_postalCodeController.text.isNotEmpty) break;
}
_transitionStepFocus();
}
void _backspacePressed() {
// Put the empty char back into the controller
switch (_currentStep) {
case CardEntryStep.number:
break;
case CardEntryStep.exp:
_expirationController.text = '\u200b';
case CardEntryStep.cvc:
_securityCodeController.text = '\u200b';
case CardEntryStep.postal:
_postalCodeController.text = '\u200b';
}
_transitionStepFocus();
}
void _transitionStepFocus() {
switch (_currentStep) {
case CardEntryStep.number:
break;
case CardEntryStep.exp:
_currentCardEntryStepController.add(CardEntryStep.number); _currentCardEntryStepController.add(CardEntryStep.number);
String numStr = _cardNumberController.text; String numStr = _cardNumberController.text;
_cardNumberController.text = numStr.substring(0, numStr.length - 1); _cardNumberController.text = numStr.substring(0, numStr.length - 1);
break; break;
case CardEntryStep.cvc: case CardEntryStep.cvc:
final cvcStr = _securityCodeController.text;
if (cvcStr.isNotEmpty) break;
_currentCardEntryStepController.add(CardEntryStep.exp); _currentCardEntryStepController.add(CardEntryStep.exp);
final expStr = _expirationController.text; final expStr = _expirationController.text;
_expirationController.text = expStr.substring(0, expStr.length - 1); _expirationController.text = expStr.substring(0, expStr.length - 1);
break;
case CardEntryStep.postal: case CardEntryStep.postal:
final String postalStr = _postalCodeController.text;
if (postalStr.isNotEmpty) break;
_currentCardEntryStepController.add(CardEntryStep.cvc); _currentCardEntryStepController.add(CardEntryStep.cvc);
final String cvcStr = _securityCodeController.text; final String cvcStr = _securityCodeController.text;
_securityCodeController.text = cvcStr.substring(0, cvcStr.length - 1); _securityCodeController.text = cvcStr.substring(0, cvcStr.length - 1);
break;
} }
} }
} }

View File

@ -1,6 +1,6 @@
name: stripe_native_card_field name: stripe_native_card_field
description: A native flutter implementation of the elegant Stripe Card Field. description: A native flutter implementation of the elegant Stripe Card Field.
version: 0.0.2 version: 0.0.3
repository: https://git.fosscat.com/n8r/stripe_native_card_field repository: https://git.fosscat.com/n8r/stripe_native_card_field
environment: environment: