import 'card_details.dart'; import 'package:flutter/material.dart'; import 'package:flutter_svg/flutter_svg.dart'; /// Widget that provides the various supported card provider's /// icons, as well as a default and error card icon. /// /// To see a list of supported card providers, see `CardDetails.provider`. class CardProviderIcon extends StatefulWidget { const CardProviderIcon({required this.cardDetails, this.size, this.defaultCardColor, this.errorCardColor, super.key}); final CardDetails? cardDetails; final Size? size; final String? defaultCardColor; final String? errorCardColor; @override State createState() => _CardProviderIconState(); } class _CardProviderIconState extends State { late final Map cardProviderSvg; late final Size _size; @override initState() { final errorCardColor = widget.errorCardColor ?? 'red'; final defaultCardColor = widget.defaultCardColor ?? 'black'; cardProviderSvg = { 'credit-card': '', 'error': '', CardProviderID.discoverCard.name: '', CardProviderID.americanExpress.name: '', CardProviderID.mastercard.name: '', CardProviderID.visa.name: '', CardProviderID.dinersClub.name: '', CardProviderID.jcb.name: '', }; _size = widget.size ?? const Size(30.0, 20.0); super.initState(); } @override Widget build(BuildContext context) { late Widget child; if (widget.cardDetails?.cardNumber != null && widget.cardDetails!.cardNumberFilled && 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: _size.height, width: _size.width, ), ); } else { if (widget.cardDetails?.provider?.id == null) { child = Padding( padding: const EdgeInsets.symmetric(horizontal: 5.0), child: SvgPicture.string( key: const Key('credit_card'), cardProviderSvg['credit-card']!, height: _size.height, width: _size.width, ), ); } else { child = createCardSvg(widget.cardDetails!.provider!.id); } } return AnimatedSwitcher( duration: const Duration(milliseconds: 150), transitionBuilder: (child, animation) { return ScaleTransition( scale: animation, child: FadeTransition( opacity: animation, child: child, )); }, child: child, ); } /// Helper function to create the SVG icons provided a `CardProviderID`. Widget createCardSvg(CardProviderID id) { return SvgPicture.string( key: Key('${id.name}-card'), cardProviderSvg[id.name]!, height: _size.height, width: _size.width, ); } }