From dd52ffb39811045e10e45aa93f6474b9073f89d8 Mon Sep 17 00:00:00 2001 From: Nathan Anderson Date: Tue, 21 Nov 2023 09:45:25 -0700 Subject: [PATCH] 0.0.3 released, see CHANGELOG.md --- .gitignore | 2 - CHANGELOG.md | 15 + LICENSE | 12 +- README.md | 56 ++- example/android/local.properties | 5 +- example/lib/main.dart | 46 +-- example/loading.gif | Bin 0 -> 18729 bytes lib/card_details.dart | 36 +- lib/card_provider_icon.dart | 28 +- lib/stripe_native_card_field.dart | 600 ++++++++++++++++++++---------- pubspec.yaml | 2 +- 11 files changed, 535 insertions(+), 267 deletions(-) create mode 100644 example/loading.gif diff --git a/.gitignore b/.gitignore index 3d13812..f91e482 100644 --- a/.gitignore +++ b/.gitignore @@ -29,5 +29,3 @@ migrate_working_dir/ .packages build/ -# Example flutter app -example/ diff --git a/CHANGELOG.md b/CHANGELOG.md index 7278e03..346ba7a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 Added dartdoc comments for more pub points! diff --git a/LICENSE b/LICENSE index d1876df..7811508 100644 --- a/LICENSE +++ b/LICENSE @@ -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. - 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. +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. diff --git a/README.md b/README.md index 9cc42b3..8fc98e3 100644 --- a/README.md +++ b/README.md @@ -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 elements they provide with flutter is less than ideal. + + ## Features -- Card number validation -- No more depending on Flutter Webview +Got to use emojis and taglines for attention grabbing and algorithm hacking: + +⚡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 @@ -16,10 +47,13 @@ elements they provide with flutter is less than ideal. Include the package in a file: + ```dart import 'package:stripe_native_card_field/stripe_native_card_field.dart'; ``` +### For just Card Data + ```dart CardTextField( 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 Repository located [here](https://git.fosscat.com/n8r/stripe_native_card_field) diff --git a/example/android/local.properties b/example/android/local.properties index 24d90b5..dafb1cf 100644 --- a/example/android/local.properties +++ b/example/android/local.properties @@ -1,2 +1,5 @@ sdk.dir=/home/nate/Android/Sdk -flutter.sdk=/home/nate/Tooling/flutter \ No newline at end of file +flutter.sdk=/home/nate/Tooling/flutter +flutter.buildMode=debug +flutter.versionName=1.0.0 +flutter.versionCode=1 \ No newline at end of file diff --git a/example/lib/main.dart b/example/lib/main.dart index c43b4f7..d3fb87c 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -14,23 +14,9 @@ class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( - title: 'Flutter Demo', + debugShowCheckedModeBanner: false, + title: 'Native Stripe Field Demo', 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), useMaterial3: true, ), @@ -42,15 +28,6 @@ class MyApp extends StatelessWidget { class MyHomePage extends StatefulWidget { 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; @override @@ -58,7 +35,7 @@ class MyHomePage extends StatefulWidget { } class _MyHomePageState extends State { - ValidState? state; + CardDetailsValidState? state; String? errorText; @override @@ -79,11 +56,18 @@ class _MyHomePageState extends State { ), ), CardTextField( - width: 500, - stripePublishableKey: 'pk_test_abc123importantIDhere', - onStripeResponse: (details) { - if (kDebugMode) print('Got card details: $details'); + width: 300, + onCardDetailsComplete: (details) { + if (kDebugMode) { + 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, errorText: errorText, ), @@ -91,7 +75,7 @@ class _MyHomePageState extends State { child: const Text('Set manual error'), onPressed: () => setState(() { errorText = 'There is a problem'; - state = ValidState.invalidCard; + state = CardDetailsValidState.invalidCard; }), ) ], diff --git a/example/loading.gif b/example/loading.gif new file mode 100644 index 0000000000000000000000000000000000000000..ebbafbef6e8bebef20f930992082852f4e2f90ad GIT binary patch literal 18729 zcmb5VcR1CL|Ns9y1LtsXNRm{?I_DU1jF8YVl2Am$s!m9Dl1&`P-m>?eS;yWSdxj)C z9ea;dQi-qEdwj3Y=l93=x_;;Uea?0MIk)G1J#O7rmAUrJ954sIvjDTRvz?utIXO8^ zO-(5&DKRlI9v&W{p`pda#Z^^ROG`^*V`IL)zK)KLTU%S-zkeSb90dOVytq#o?%uqw ztE{MfQ(OcG0iAx9`xg`r1y}&#f4=9Rp8?RE5ctAPEz-;_IsG?U?1YE0uQ~3Ra3Qu& zC@4_85GSlH$NrizTYA@}*Xn*3Ge(ph2d-Qz|4@4P!=*QnkZO(Oz%Vlh%(9F$j1E6i zq!7eljJA>tul<eCvV(5?PgFa=n~;%?#IK0f%sYsg3q2K=#foIiWY zeg-{^E}}Lp@V5+9@vrlDZP9+(;ejr%tbOLo@qV3ErgDRh{+RKb{jQec8T#M1c@(vv zVDD~QIblUL9=IeSDTw@Pw^#Mdp63a_b(UH%%zbM=mCd_kR-NV1Uem${?VMyNJxN4u zZvlmk)KgJyqzl#qfwiB$$8%J@BjuI%ds{}H8Hi<(ofM1IUr@`gq(k=ugz6G-1+Pw2 z`@2PWxa-fcQY5ja?8!OkYy+czR>}Y?_&6L#{1AP=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r8BsybrTMo!=gU$r8{=^Y}&P48Q5bXjtT++?Mhs&YxgeR zr%lxnFiTh|+!Ke_zWkM-F<~2#yMiNX`E1}CnOVx~j8z(s0t`ZPoD8Zm>64#%95A4o zbtTuNANLTKIdQE5xvQkcOVDy`C$!aqtwq>$BBoEtg3GG=&Fq!Z!TmK!^feq zPrkhQ^Hc^nSkJz_`}P5RmrsxYqy78%5zsI3zQ6zV=sl3%fCP?^z<&s~my-W^1U8u8 z6bVKMV0jK!XrBlYM#!Lr79Nmcf?bfuVTT_g7~*&%R`@`P|81CJfh_vx;*0o+aA1rI zo=4+@7~;rYfID7Df_gE=7vKZ*rMTabOg8D{lk%)lUKrwmqrjC{jyC{(^bK&uFIa|| zB|%)4Ilzf;BmjVzVvCsbOfJeek1!vLNdDi5Tg!bwA^bE2tTs;bVws+f9? z0Kl!@4Z(%04+wB4tV_l)>zF0ny1=mu0I)z25Cq`tvk<(5DX@#8Fzo+v0U)bwvbsL& zEutp`@N1;Cs!%ElYHq9T56yn-?J46X%B!`Fs-Ud8$O2%SmnpdW?Yts*si+NWnyLc6 z+e)|Vt|rLWgQhkZd@#buX0pJ-3ok2`zwXWeX8=85NyEJqA4>uP7-Jk5104T5Y5>bs zQL@P;q`YtmEq@!cJS5Ldg3TC{kh8Y{>oYUWKLgFM&_h2<-T^&RY=Y8DkFY?~&X#wy z#x+yT>i}36{YwE+BYc9_yb6Fd$lsNXF92tI9d6j)Z4E%R51@?#+e|{Rbqad_Q0_8r zs|&c??CMjvwnYbi0@y=~UCH9d!hJZ^Dl{#5B_+$?_})2l3d%v^gLjR&3JA2kIVj1h zYq<)agYNF?wHV8_=ZMQdG?X#KZtv^7U#@iURGdyc6DTX4>+wBYZ1e5R8{s?`(5n*y F06VwF?+O3_ literal 0 HcmV?d00001 diff --git a/lib/card_details.dart b/lib/card_details.dart index 0e870f2..b0bf20d 100644 --- a/lib/card_details.dart +++ b/lib/card_details.dart @@ -32,14 +32,14 @@ class CardDetails { String? expirationString; DateTime? expirationDate; bool _complete = false; - ValidState _validState = ValidState.blank; + CardDetailsValidState _validState = CardDetailsValidState.blank; int _lastCheckHash = 0; CardProvider? provider; - set overrideValidState(ValidState state) => _validState = state; + set overrideValidState(CardDetailsValidState state) => _validState = state; /// Checks the validity of the `CardDetails` and returns the result. - ValidState get validState { + CardDetailsValidState get validState { checkIsValid(); return _validState; } @@ -80,7 +80,7 @@ class CardDetails { _lastCheckHash = currentHash; if (_cardNumber == null && expirationString == null && securityCode == null && postalCode == null) { _complete = false; - _validState = ValidState.blank; + _validState = CardDetailsValidState.blank; return; } final nums = _cardNumber! @@ -92,71 +92,71 @@ class CardDetails { .toList(); if (!_luhnAlgorithmCheck(nums)) { _complete = false; - _validState = ValidState.invalidCard; + _validState = CardDetailsValidState.invalidCard; return; } if (_cardNumber == null || !cardNumberFilled) { _complete = false; - _validState = ValidState.missingCard; + _validState = CardDetailsValidState.missingCard; return; } if (expirationString == null) { _complete = false; - _validState = ValidState.missingDate; + _validState = CardDetailsValidState.missingDate; return; } final expSplits = expirationString!.split('/'); if (expSplits.length != 2 || expSplits.last == '') { _complete = false; - _validState = ValidState.missingDate; + _validState = CardDetailsValidState.missingDate; return; } final month = int.parse(expSplits.first[0] == '0' ? expSplits.first[1] : expSplits.first); if (month < 1 || month > 12) { _complete = false; - _validState = ValidState.invalidMonth; + _validState = CardDetailsValidState.invalidMonth; return; } final year = 2000 + int.parse(expSplits.last); final date = DateTime(year, month); if (date.isBefore(DateTime.now())) { _complete = false; - _validState = ValidState.dateTooEarly; + _validState = CardDetailsValidState.dateTooEarly; return; } else if (date.isAfter(DateTime.now().add(const Duration(days: 365 * 50)))) { _complete = false; - _validState = ValidState.dateTooLate; + _validState = CardDetailsValidState.dateTooLate; return; } expirationDate = date; if (securityCode == null) { _complete = false; - _validState = ValidState.missingCVC; + _validState = CardDetailsValidState.missingCVC; return; } if (provider != null && securityCode!.length != provider!.cvcLength) { _complete = false; - _validState = ValidState.invalidCVC; + _validState = CardDetailsValidState.invalidCVC; return; } if (postalCode == null) { _complete = false; - _validState = ValidState.missingZip; + _validState = CardDetailsValidState.missingZip; return; } if (!RegExp(r'^\d{5}(-\d{4})?$').hasMatch(postalCode!)) { _complete = false; - _validState = ValidState.invalidZip; + _validState = CardDetailsValidState.invalidZip; return; } _complete = true; - _validState = ValidState.ok; + _validState = CardDetailsValidState.ok; } catch (err, st) { if (kDebugMode) { print('Error while validating CardDetails: $err\n$st'); } _complete = false; - _validState = ValidState.error; + _validState = CardDetailsValidState.error; } } @@ -245,7 +245,7 @@ class CardDetails { } /// Enum of validation states a `CardDetails` object can have. -enum ValidState { +enum CardDetailsValidState { ok, error, blank, diff --git a/lib/card_provider_icon.dart b/lib/card_provider_icon.dart index ce10680..0271cbc 100644 --- a/lib/card_provider_icon.dart +++ b/lib/card_provider_icon.dart @@ -7,9 +7,10 @@ import 'package:flutter_svg/flutter_svg.dart'; /// /// To see a list of supported card providers, see `CardDetails.provider`. class CardProviderIcon extends StatefulWidget { - const CardProviderIcon({required this.cardDetails, super.key}); + const CardProviderIcon({required this.cardDetails, this.size, super.key}); final CardDetails? cardDetails; + final Size? size; @override State createState() => _CardProviderIconState(); @@ -34,22 +35,29 @@ class _CardProviderIconState extends State { CardProviderID.jcb.name: '', }; - 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 Widget build(BuildContext context) { late Widget child; if (widget.cardDetails?.cardNumber != null && widget.cardDetails!.cardNumberFilled && - widget.cardDetails!.validState == ValidState.invalidCard) { + widget.cardDetails!.validState == CardDetailsValidState.invalidCard) { child = Padding( padding: const EdgeInsets.symmetric(horizontal: 5.0), child: SvgPicture.string( key: const Key('invalid-card'), cardProviderSvg['error']!, - height: height, - width: width, + height: _size.height, + width: _size.width, ), ); } else { @@ -59,8 +67,8 @@ class _CardProviderIconState extends State { child: SvgPicture.string( key: const Key('credit_card'), cardProviderSvg['credit-card']!, - height: height, - width: width, + height: _size.height, + width: _size.width, ), ); } else { @@ -86,8 +94,8 @@ class _CardProviderIconState extends State { return SvgPicture.string( key: Key('${id.name}-card'), cardProviderSvg[id.name]!, - height: height, - width: width, + height: _size.height, + width: _size.width, ); } } diff --git a/lib/stripe_native_card_field.dart b/lib/stripe_native_card_field.dart index 2e721a6..323498d 100644 --- a/lib/stripe_native_card_field.dart +++ b/lib/stripe_native_card_field.dart @@ -2,6 +2,8 @@ library stripe_native_card_field; import 'dart:async'; import 'dart:convert'; +import 'dart:developer'; +import 'dart:io'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; @@ -28,9 +30,9 @@ enum CardEntryStep { number, exp, cvc, postal } class CardTextField extends StatefulWidget { CardTextField({ Key? key, + required this.width, this.onStripeResponse, this.onCardDetailsComplete, - required this.width, this.stripePublishableKey, this.height, this.textStyle, @@ -39,61 +41,89 @@ class CardTextField extends StatefulWidget { this.boxDecoration, this.errorBoxDecoration, this.loadingWidget, + this.showInternalLoadingWidget = true, this.delayToShowLoading = const Duration(milliseconds: 750), + this.onCallToStripe, this.overrideValidState, this.errorText, + this.cardFieldWidth, + this.expFieldWidth, + this.securityFieldWidth, + this.postalFieldWidth, + this.iconSize, // this.loadingWidgetLocation = LoadingLocation.rightInside, }) : super(key: key) { if (stripePublishableKey != null) { assert(stripePublishableKey!.startsWith('pk_')); 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_')) { - print( - 'StripeNativeCardField: *WARN* You are using a live stripe key in a debug environment, proceed with caution!'); - print('StripeNativeCardField: *WARN* Ideally you should be using your test keys whenever not in production.'); + log('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.'); } } else { if (onStripeResponse != null) { - print( - 'StripeNativeCardField: *ERROR* You provided the onTokenReceived callback, but did not provide a stripePublishableKey.'); + log('StripeNativeCardField: *ERROR* You provided the onTokenReceived callback, but did not provide a stripePublishableKey.'); 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 final BoxDecoration? boxDecoration; /// Overrides the default box decoration of the text field when there is a validation error 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` 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 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; /// TextStyle used when any TextFormField's have a validation error + /// If null, inherits from the `textStyle`. final TextStyle? errorTextStyle; /// Time to wait until showing the loading indicator when retrieving Stripe token final Duration delayToShowLoading; - /// Determines where the loading indicator appears when contacting stripe - // final LoadingLocation loadingWidgetLocation; + /// Whether to show the internal loading widget on calls to Stripe + 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 final void Function(Map)? onStripeResponse; @@ -102,7 +132,7 @@ class CardTextField extends StatefulWidget { final void Function(CardDetails)? onCardDetailsComplete; /// 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 final String? errorText; @@ -137,16 +167,32 @@ class CardTextFieldState extends State { late final TextStyle _normalTextStyle; late final TextStyle _hintTextSyle; - final double _cardFieldWidth = 180.0; - final double _expirationFieldWidth = 70.0; - final double _securityFieldWidth = 40.0; - final double _postalFieldWidth = 95.0; + /// Width of the card number text field + late final double _cardFieldWidth; + + /// 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; + + /// Width of the gap between card number and expiration text fields when expanded 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; bool _showBorderError = false; + final _isMobile = Platform.isAndroid || Platform.isIOS; + + /// If a request to Stripe is being made bool _loading = false; final CardDetails _cardDetails = CardDetails.blank(); int _prevErrorOverrideHash = 0; @@ -159,19 +205,32 @@ class CardTextFieldState extends State { @override 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(); - _expirationController = TextEditingController(); - _securityCodeController = TextEditingController(); - _postalCodeController = TextEditingController(); + _expirationController = TextEditingController(text: _isMobile ? '\u200b' : ''); + _securityCodeController = TextEditingController(text: _isMobile ? '\u200b' : ''); + _postalCodeController = TextEditingController(text: _isMobile ? '\u200b' : ''); + + // Otherwise, use `RawKeyboard` listener + if (!_isMobile) { + RawKeyboard.instance.addListener(_backspaceTransitionListener); + } cardNumberFocusNode = FocusNode(); expirationFocusNode = FocusNode(); securityCodeFocusNode = 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); - _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( color: const Color(0xfff6f9fc), @@ -212,12 +271,13 @@ class CardTextFieldState extends State { _currentCardEntryStepController.stream.listen( _onStepChange, ); - RawKeyboard.instance.addListener(_backspaceTransitionListener); - isWideFormat = widget.width >= 450; + + isWideFormat = + widget.width >= _cardFieldWidth + _expirationFieldWidth + _securityFieldWidth + _postalFieldWidth + 60.0; if (isWideFormat) { _internalFieldWidth = widget.width + _postalFieldWidth + 35; _expanderWidthExpanded = widget.width - _cardFieldWidth - _expirationFieldWidth - _securityFieldWidth - 35; - _expanderWidthContracted = + _expanderWidthCollapsed = widget.width - _cardFieldWidth - _expirationFieldWidth - _securityFieldWidth - _postalFieldWidth - 70; } else { _internalFieldWidth = _cardFieldWidth + _expirationFieldWidth + _securityFieldWidth + _postalFieldWidth + 80; @@ -236,7 +296,9 @@ class CardTextFieldState extends State { expirationFocusNode.dispose(); securityCodeFocusNode.dispose(); - RawKeyboard.instance.removeListener(_backspaceTransitionListener); + if (!_isMobile) { + RawKeyboard.instance.removeListener(_backspaceTransitionListener); + } super.dispose(); } @@ -259,6 +321,30 @@ class CardTextFieldState extends State { // Focuses to the current field _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( width: widget.width, height: widget.height ?? 60.0, @@ -279,6 +365,7 @@ class CardTextFieldState extends State { padding: const EdgeInsets.symmetric(horizontal: 6.0), child: CardProviderIcon( cardDetails: _cardDetails, + size: widget.iconSize, ), ), SizedBox( @@ -288,7 +375,11 @@ class CardTextFieldState extends State { focusNode: cardNumberFocusNode, controller: _cardNumberController, keyboardType: TextInputType.number, - style: _isRedText([ValidState.invalidCard, ValidState.missingCard, ValidState.blank]) + style: _isRedText([ + CardDetailsValidState.invalidCard, + CardDetailsValidState.missingCard, + CardDetailsValidState.blank + ]) ? _errorTextStyle : _normalTextStyle, validator: (content) { @@ -296,9 +387,9 @@ class CardTextFieldState extends State { return null; } _cardDetails.cardNumber = content; - if (_cardDetails.validState == ValidState.invalidCard) { + if (_cardDetails.validState == CardDetailsValidState.invalidCard) { _setValidationState('Your card number is invalid.'); - } else if (_cardDetails.validState == ValidState.missingCard) { + } else if (_cardDetails.validState == CardDetailsValidState.missingCard) { _setValidationState('Your card number is incomplete.'); } return null; @@ -340,7 +431,7 @@ class CardTextFieldState extends State { Size(_expanderWidthExpanded, 0.0), ) : BoxConstraints.tight( - Size(_expanderWidthContracted, 0.0), + Size(_expanderWidthCollapsed, 0.0), ), ), ), @@ -348,170 +439,210 @@ class CardTextFieldState extends State { // Spacer(flex: _currentStep == CardEntryStep.number ? 100 : 1), SizedBox( width: _expirationFieldWidth, - child: TextFormField( - key: const Key('expiration_field'), - focusNode: expirationFocusNode, - controller: _expirationController, - style: _isRedText([ - ValidState.dateTooLate, - ValidState.dateTooEarly, - ValidState.missingDate, - ValidState.invalidMonth - ]) - ? _errorTextStyle - : _normalTextStyle, - validator: (content) { - if (content == null || content.isEmpty) { - return null; - } - setState(() => _cardDetails.expirationString = content); - if (_cardDetails.validState == ValidState.dateTooEarly) { - _setValidationState('Your card\'s expiration date is in the past.'); - } else if (_cardDetails.validState == ValidState.dateTooLate) { - _setValidationState('Your card\'s expiration year is invalid.'); - } else if (_cardDetails.validState == ValidState.missingDate) { - _setValidationState('You must include your card\'s expiration date.'); - } else if (_cardDetails.validState == ValidState.invalidMonth) { - _setValidationState('Your card\'s expiration month is invalid.'); - } - return null; - }, - onChanged: (str) { - setState(() => _cardDetails.expirationString = str); - if (str.length == 5) { - _currentCardEntryStepController.add(CardEntryStep.cvc); - } - }, - onFieldSubmitted: (_) => _currentCardEntryStepController.add(CardEntryStep.cvc), - inputFormatters: [ - LengthLimitingTextInputFormatter(5), - FilteringTextInputFormatter.allow(RegExp('[0-9/]')), - CardExpirationFormatter(), + 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'), + focusNode: expirationFocusNode, + controller: _expirationController, + keyboardType: TextInputType.number, + style: _isRedText([ + CardDetailsValidState.dateTooLate, + CardDetailsValidState.dateTooEarly, + CardDetailsValidState.missingDate, + CardDetailsValidState.invalidMonth + ]) + ? _errorTextStyle + : _normalTextStyle, + validator: (content) { + if (content == null || content.isEmpty || _isMobile && content == '\u200b') { + return null; + } + + if (_isMobile) { + setState(() => _cardDetails.expirationString = content.replaceAll('\u200b', '')); + } else { + setState(() => _cardDetails.expirationString = content); + } + + if (_cardDetails.validState == CardDetailsValidState.dateTooEarly) { + _setValidationState('Your card\'s expiration date is in the past.'); + } else if (_cardDetails.validState == CardDetailsValidState.dateTooLate) { + _setValidationState('Your card\'s expiration year is invalid.'); + } else if (_cardDetails.validState == CardDetailsValidState.missingDate) { + _setValidationState('You must include your card\'s expiration date.'); + } else if (_cardDetails.validState == CardDetailsValidState.invalidMonth) { + _setValidationState('Your card\'s expiration month is invalid.'); + } + return null; + }, + onChanged: (str) { + if (_isMobile) { + if (str.isEmpty) { + _backspacePressed(); + } + setState(() => _cardDetails.expirationString = str.replaceAll('\u200b', '')); + } else { + setState(() => _cardDetails.expirationString = str); + } + if (str.length == 5) { + _currentCardEntryStepController.add(CardEntryStep.cvc); + } + }, + onFieldSubmitted: (_) => _currentCardEntryStepController.add(CardEntryStep.cvc), + inputFormatters: [ + LengthLimitingTextInputFormatter(5), + FilteringTextInputFormatter.allow(RegExp('[0-9/]')), + CardExpirationFormatter(), + ], + decoration: InputDecoration( + contentPadding: EdgeInsets.zero, + hintText: _isMobile ? '' : 'MM/YY', + hintStyle: _hintTextSyle, + fillColor: Colors.transparent, + border: InputBorder.none, + ), + ), ], - decoration: InputDecoration( - contentPadding: EdgeInsets.zero, - hintText: 'MM/YY', - hintStyle: _hintTextSyle, - fillColor: Colors.transparent, - border: InputBorder.none, - ), ), ), SizedBox( width: _securityFieldWidth, - child: TextFormField( - key: const Key('security_field'), - focusNode: securityCodeFocusNode, - controller: _securityCodeController, - style: _isRedText([ValidState.invalidCVC, ValidState.missingCVC]) - ? _errorTextStyle - : _normalTextStyle, - validator: (content) { - if (content == null || content.isEmpty) { - return null; - } - setState(() => _cardDetails.securityCode = content); - if (_cardDetails.validState == ValidState.invalidCVC) { - _setValidationState('Your card\'s security code is invalid.'); - } else if (_cardDetails.validState == ValidState.missingCVC) { - _setValidationState('Your card\'s security code is incomplete.'); - } - return null; - }, - onFieldSubmitted: (_) => _currentCardEntryStepController.add(CardEntryStep.postal), - onChanged: (str) { - setState(() => _cardDetails.expirationString = str); - if (str.length == _cardDetails.provider?.cvcLength) { - _currentCardEntryStepController.add(CardEntryStep.postal); - } - }, - inputFormatters: [ - LengthLimitingTextInputFormatter( - _cardDetails.provider == null ? 4 : _cardDetails.provider!.cvcLength), - FilteringTextInputFormatter.allow(RegExp('[0-9]')), + child: Stack( + alignment: Alignment.centerLeft, + children: [ + if (_isMobile && _securityCodeController.text == '\u200b') + Text( + 'CVC', + style: _hintTextSyle, + ), + TextFormField( + key: const Key('security_field'), + focusNode: securityCodeFocusNode, + controller: _securityCodeController, + keyboardType: TextInputType.number, + style: + _isRedText([CardDetailsValidState.invalidCVC, CardDetailsValidState.missingCVC]) + ? _errorTextStyle + : _normalTextStyle, + validator: (content) { + if (content == null || content.isEmpty || _isMobile && content == '\u200b') { + return null; + } + + if (_isMobile) { + setState(() => _cardDetails.securityCode = content.replaceAll('\u200b', '')); + } else { + setState(() => _cardDetails.securityCode = content); + } + + if (_cardDetails.validState == CardDetailsValidState.invalidCVC) { + _setValidationState('Your card\'s security code is invalid.'); + } else if (_cardDetails.validState == CardDetailsValidState.missingCVC) { + _setValidationState('Your card\'s security code is incomplete.'); + } + return null; + }, + onFieldSubmitted: (_) => _currentCardEntryStepController.add(CardEntryStep.postal), + onChanged: (str) { + if (_isMobile) { + if (str.isEmpty) { + _backspacePressed(); + } + setState(() => _cardDetails.expirationString = str.replaceAll('\u200b', '')); + } else { + setState(() => _cardDetails.expirationString = str); + } + + if (str.length == _cardDetails.provider?.cvcLength) { + _currentCardEntryStepController.add(CardEntryStep.postal); + } + }, + inputFormatters: [ + LengthLimitingTextInputFormatter( + _cardDetails.provider == null ? 4 : _cardDetails.provider!.cvcLength), + FilteringTextInputFormatter.allow(RegExp('[0-9]')), + ], + decoration: InputDecoration( + contentPadding: EdgeInsets.zero, + hintText: _isMobile ? '' : 'CVC', + hintStyle: _hintTextSyle, + fillColor: Colors.transparent, + border: InputBorder.none, + ), + ), ], - decoration: InputDecoration( - contentPadding: EdgeInsets.zero, - hintText: 'CVC', - hintStyle: _hintTextSyle, - fillColor: Colors.transparent, - border: InputBorder.none, - ), ), ), SizedBox( width: _postalFieldWidth, - child: TextFormField( - key: const Key('postal_field'), - focusNode: postalCodeFocusNode, - controller: _postalCodeController, - style: _isRedText([ValidState.invalidZip, ValidState.missingZip]) - ? _errorTextStyle - : _normalTextStyle, - validator: (content) { - if (content == null || content.isEmpty) { - return null; - } - setState(() => _cardDetails.postalCode = content); + child: Stack( + alignment: Alignment.centerLeft, + children: [ + if (_isMobile && _postalCodeController.text == '\u200b') + Text( + 'Postal Code', + style: _hintTextSyle, + ), + TextFormField( + key: const Key('postal_field'), + focusNode: postalCodeFocusNode, + controller: _postalCodeController, + keyboardType: TextInputType.number, + style: + _isRedText([CardDetailsValidState.invalidZip, CardDetailsValidState.missingZip]) + ? _errorTextStyle + : _normalTextStyle, + validator: (content) { + if (content == null || content.isEmpty || _isMobile && content == '\u200b') { + return null; + } - if (_cardDetails.validState == ValidState.invalidZip) { - _setValidationState('The postal code you entered is not correct.'); - } else if (_cardDetails.validState == ValidState.missingZip) { - _setValidationState('You must enter your card\'s postal code.'); - } - return null; - }, - onChanged: (str) { - setState(() => _cardDetails.postalCode = str); - }, - textInputAction: TextInputAction.done, - onFieldSubmitted: (_) async { - _validateFields(); - if (_cardDetails.isComplete) { - if (widget.onCardDetailsComplete != null) { - widget.onCardDetailsComplete!(_cardDetails); - } else if (widget.onStripeResponse != null) { - bool returned = false; + if (_isMobile) { + setState(() => _cardDetails.postalCode = content.replaceAll('\u200b', '')); + } else { + setState(() => _cardDetails.postalCode = content); + } - Future.delayed( - const Duration(milliseconds: 750), - () => returned ? null : setState(() => _loading = true), - ); - - const stripeCardUrl = 'https://api.stripe.com/v1/tokens'; - final response = await http.post( - Uri.parse(stripeCardUrl), - body: { - 'card[number]': _cardDetails.cardNumber, - 'card[cvc]': _cardDetails.securityCode, - 'card[exp_month]': _cardDetails.expMonth, - 'card[exp_year]': _cardDetails.expYear, - 'card[address_zip]': _cardDetails.postalCode, - 'key': widget.stripePublishableKey, - }, - headers: {"Content-Type": "application/x-www-form-urlencoded"}, - ); - - returned = true; - final jsonBody = jsonDecode(response.body); - - widget.onStripeResponse!(jsonBody); - if (_loading) setState(() => _loading = false); - } - } - }, - decoration: InputDecoration( - contentPadding: EdgeInsets.zero, - hintText: 'Postal Code', - hintStyle: _hintTextSyle, - fillColor: Colors.transparent, - border: InputBorder.none, - ), + if (_cardDetails.validState == CardDetailsValidState.invalidZip) { + _setValidationState('The postal code you entered is not correct.'); + } else if (_cardDetails.validState == CardDetailsValidState.missingZip) { + _setValidationState('You must enter your card\'s postal code.'); + } + return null; + }, + onChanged: (str) { + if (_isMobile) { + if (str.isEmpty) { + _backspacePressed(); + } + setState(() => _cardDetails.postalCode = str.replaceAll('\u200b', '')); + } else { + setState(() => _cardDetails.postalCode = str); + } + }, + textInputAction: TextInputAction.done, + 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 ? 1.0 : 0.0, + opacity: _loading && widget.showInternalLoadingWidget ? 1.0 : 0.0, child: widget.loadingWidget ?? const CircularProgressIndicator(), ), ], @@ -529,7 +660,8 @@ class CardTextFieldState extends State { child: Padding( padding: const EdgeInsets.only(top: 8.0, left: 14.0), child: Text( - _validationErrorText ?? '', + // 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), ), ), @@ -538,9 +670,47 @@ class CardTextFieldState extends State { ); } + Future _postalFieldSubmitted() async { + _validateFields(); + if (_cardDetails.isComplete) { + if (widget.onCardDetailsComplete != null) { + widget.onCardDetailsComplete!(_cardDetails); + } else if (widget.onStripeResponse != null) { + bool returned = false; + + Future.delayed( + const Duration(milliseconds: 750), + () => returned ? null : setState(() => _loading = true), + ); + + 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( + Uri.parse(stripeCardUrl), + body: { + 'card[number]': _cardDetails.cardNumber, + 'card[cvc]': _cardDetails.securityCode, + 'card[exp_month]': _cardDetails.expMonth, + 'card[exp_year]': _cardDetails.expYear, + 'card[address_zip]': _cardDetails.postalCode, + 'key': widget.stripePublishableKey, + }, + headers: {"Content-Type": "application/x-www-form-urlencoded"}, + ); + + returned = true; + final jsonBody = jsonDecode(response.body); + + widget.onStripeResponse!(jsonBody); + if (_loading) setState(() => _loading = false); + } + } + } + /// Provided a list of `ValidState`, returns whether /// make the text field red - bool _isRedText(List args) { + bool _isRedText(List args) { return _showBorderError && args.contains(_cardDetails.validState); } @@ -623,6 +793,23 @@ class CardTextFieldState extends State { if (!isWideFormat) { _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. @@ -638,24 +825,49 @@ class CardTextFieldState extends State { case CardEntryStep.number: break; case CardEntryStep.exp: - final expStr = _expirationController.text; - if (expStr.isNotEmpty) break; + if (_expirationController.text.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); String numStr = _cardNumberController.text; _cardNumberController.text = numStr.substring(0, numStr.length - 1); break; case CardEntryStep.cvc: - final cvcStr = _securityCodeController.text; - if (cvcStr.isNotEmpty) break; _currentCardEntryStepController.add(CardEntryStep.exp); final expStr = _expirationController.text; _expirationController.text = expStr.substring(0, expStr.length - 1); + break; case CardEntryStep.postal: - final String postalStr = _postalCodeController.text; - if (postalStr.isNotEmpty) break; _currentCardEntryStepController.add(CardEntryStep.cvc); final String cvcStr = _securityCodeController.text; _securityCodeController.text = cvcStr.substring(0, cvcStr.length - 1); + break; } } } diff --git a/pubspec.yaml b/pubspec.yaml index 8005687..8f063da 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: stripe_native_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 environment: