| 1 | import "package:cake_wallet/generated/i18n.dart"; |
| 2 | import "package:cake_wallet/new-ui/widgets/modern_button.dart"; |
| 3 | import "package:cw_core/crypto_amount_format.dart"; |
| 4 | import "package:flutter/material.dart"; |
| 5 | |
| 6 | class FiatAmountBar extends StatelessWidget { |
| 7 | const FiatAmountBar({ |
| 8 | required this.fiatInputMode, |
| 9 | required this.onSwitchButtonPressed, |
| 10 | required this.cryptoAmount, |
| 11 | required this.fiatAmount, |
| 12 | required this.cryptoCurrencySymbol, |
| 13 | required this.fiatCurrencySymbol, |
| 14 | super.key, |
| 15 | this.onAllButtonPressed, |
| 16 | this.allAmount, |
| 17 | this.foregroundElementColor, |
| 18 | this.textColor, |
| 19 | this.allAmountColor, |
| 20 | this.allAmountTextColor, |
| 21 | }); |
| 22 | |
| 23 | final bool fiatInputMode; |
| 24 | final VoidCallback onSwitchButtonPressed; |
| 25 | final VoidCallback? onAllButtonPressed; |
| 26 | |
| 27 | final String cryptoAmount; |
| 28 | final String fiatAmount; |
| 29 | final String cryptoCurrencySymbol; |
| 30 | final String fiatCurrencySymbol; |
| 31 | final String? allAmount; |
| 32 | final Color? foregroundElementColor; |
| 33 | final Color? textColor; |
| 34 | final Color? allAmountColor; |
| 35 | final Color? allAmountTextColor; |
| 36 | |
| 37 | @override |
| 38 | Widget build(BuildContext context) { |
| 39 | final convertedAmount = fiatInputMode |
| 40 | ? "${cryptoAmount.isEmpty ? "0" : cryptoAmount.withMaxDecimals(8)} $cryptoCurrencySymbol" |
| 41 | : "${fiatAmount.isEmpty ? "0" : fiatAmount} $fiatCurrencySymbol"; |
| 42 | |
| 43 | return Row( |
| 44 | mainAxisAlignment: MainAxisAlignment.spaceBetween, |
| 45 | children: [ |
| 46 | Row( |
| 47 | spacing: 8, |
| 48 | children: [ |
| 49 | ModernButton.svg( |
| 50 | backgroundColor: foregroundElementColor, |
| 51 | size: 28, |
| 52 | svgPath: "assets/new-ui/switch.svg", |
| 53 | iconSize: 18, |
| 54 | onPressed: onSwitchButtonPressed, |
| 55 | semanticLabel: S.of(context).switch_input_currency, |
| 56 | ), |
| 57 | // Announced as the converted value only: the switch button next to it already |
| 58 | // exposes the same action, so this must not become a second control. |
| 59 | Semantics( |
| 60 | label: convertedAmount, |
| 61 | excludeSemantics: true, |
| 62 | child: GestureDetector( |
| 63 | onTap: onSwitchButtonPressed, |
| 64 | child: Text( |
| 65 | convertedAmount, |
| 66 | style: TextStyle(color: textColor ?? Theme.of(context).colorScheme.onSurface), |
| 67 | ), |
| 68 | ), |
| 69 | ), |
| 70 | ], |
| 71 | ), |
| 72 | if (allAmount != null && allAmount!.isNotEmpty) |
| 73 | Row( |
| 74 | spacing: 8, |
| 75 | children: [ |
| 76 | // The caption is part of the chip's label below. |
| 77 | ExcludeSemantics( |
| 78 | child: Text( |
| 79 | "${S.of(context).max}.", |
| 80 | style: TextStyle(color: textColor ?? Theme.of(context).colorScheme.onSurface), |
| 81 | ), |
| 82 | ), |
| 83 | Container( |
| 84 | decoration: BoxDecoration(borderRadius: BorderRadius.circular(999999)), |
| 85 | child: Material( |
| 86 | color: allAmountColor ?? |
| 87 | foregroundElementColor ?? |
| 88 | Theme.of(context).colorScheme.surfaceContainer, |
| 89 | borderRadius: BorderRadius.circular(99999), |
| 90 | child: Semantics( |
| 91 | button: true, |
| 92 | enabled: onAllButtonPressed != null, |
| 93 | label: S.of(context).max, |
| 94 | value: _formatAmount(allAmount!), |
| 95 | onTap: onAllButtonPressed, |
| 96 | excludeSemantics: true, |
| 97 | child: InkWell( |
| 98 | borderRadius: BorderRadius.circular(99999), |
| 99 | onTap: onAllButtonPressed, |
| 100 | child: Padding( |
| 101 | padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4), |
| 102 | child: Text( |
| 103 | _formatAmount(allAmount!), |
| 104 | style: TextStyle( |
| 105 | color: allAmountTextColor ?? Theme.of(context).colorScheme.primary, |
| 106 | ), |
| 107 | ), |
| 108 | ), |
| 109 | ), |
| 110 | ), |
| 111 | ), |
| 112 | ), |
| 113 | ], |
| 114 | ), |
| 115 | ], |
| 116 | ); |
| 117 | } |
| 118 | |
| 119 | String _formatAmount(String amount) { |
| 120 | try { |
| 121 | return double.parse(amount).toStringAsPrecision(8).replaceFirst(RegExp(r"\.?0+$"), ""); |
| 122 | } catch (e) { |
| 123 | return amount; |
| 124 | } |
| 125 | } |
| 126 | } |