| 1 | import 'package:cake_wallet/generated/i18n.dart'; |
| 2 | import 'package:cake_wallet/utils/address_formatter.dart'; |
| 3 | import 'package:cake_wallet/view_model/wallet_address_list/wallet_address_list_item.dart'; |
| 4 | import 'package:cw_core/wallet_type.dart'; |
| 5 | import 'package:flutter/cupertino.dart'; |
| 6 | import 'package:flutter/material.dart'; |
| 7 | import 'package:flutter_slidable/flutter_slidable.dart'; |
| 8 | |
| 9 | class AddressCell extends StatelessWidget { |
| 10 | AddressCell({ |
| 11 | required this.address, |
| 12 | required this.name, |
| 13 | required this.isCurrent, |
| 14 | required this.isPrimary, |
| 15 | required this.backgroundColor, |
| 16 | required this.textColor, |
| 17 | required this.walletType, |
| 18 | this.onTap, |
| 19 | this.onEdit, |
| 20 | this.onHide, |
| 21 | this.isHidden = false, |
| 22 | this.onDelete, |
| 23 | this.txCount, |
| 24 | this.balance, |
| 25 | this.isChange = false, |
| 26 | this.hasBalance = false, |
| 27 | this.hasReceived = false, |
| 28 | }); |
| 29 | |
| 30 | factory AddressCell.fromItem( |
| 31 | WalletAddressListItem item, { |
| 32 | required bool isCurrent, |
| 33 | required Color backgroundColor, |
| 34 | required Color textColor, |
| 35 | required WalletType walletType, |
| 36 | Function(String)? onTap, |
| 37 | bool hasBalance = false, |
| 38 | bool hasReceived = false, |
| 39 | Function()? onEdit, |
| 40 | Function()? onHide, |
| 41 | bool isHidden = false, |
| 42 | Function()? onDelete, |
| 43 | }) => |
| 44 | AddressCell( |
| 45 | address: item.address, |
| 46 | name: item.name ?? '', |
| 47 | isCurrent: isCurrent, |
| 48 | isPrimary: item.isPrimary, |
| 49 | backgroundColor: backgroundColor, |
| 50 | textColor: textColor, |
| 51 | walletType: walletType, |
| 52 | onTap: onTap, |
| 53 | onEdit: onEdit, |
| 54 | onHide: onHide, |
| 55 | isHidden: isHidden, |
| 56 | onDelete: onDelete, |
| 57 | txCount: item.txCount, |
| 58 | balance: item.balance, |
| 59 | isChange: item.isChange, |
| 60 | hasBalance: hasBalance, |
| 61 | hasReceived: hasReceived, |
| 62 | ); |
| 63 | |
| 64 | final String address; |
| 65 | final String name; |
| 66 | final bool isCurrent; |
| 67 | final bool isPrimary; |
| 68 | final Color backgroundColor; |
| 69 | final Color textColor; |
| 70 | final WalletType walletType; |
| 71 | final Function(String)? onTap; |
| 72 | final Function()? onEdit; |
| 73 | final Function()? onHide; |
| 74 | final bool isHidden; |
| 75 | final Function()? onDelete; |
| 76 | final int? txCount; |
| 77 | final String? balance; |
| 78 | final bool isChange; |
| 79 | final bool hasBalance; |
| 80 | final bool hasReceived; |
| 81 | |
| 82 | @override |
| 83 | Widget build(BuildContext context) { |
| 84 | final Widget cell = InkWell( |
| 85 | onTap: () => onTap?.call(address), |
| 86 | child: Container( |
| 87 | width: double.infinity, |
| 88 | color: backgroundColor, |
| 89 | padding: EdgeInsets.only(left: 24, right: 24, top: 20, bottom: 20), |
| 90 | child: Row( |
| 91 | children: [ |
| 92 | Expanded( |
| 93 | child: Column( |
| 94 | children: [ |
| 95 | Row( |
| 96 | mainAxisAlignment: name.isNotEmpty |
| 97 | ? MainAxisAlignment.spaceBetween |
| 98 | : MainAxisAlignment.center, |
| 99 | mainAxisSize: MainAxisSize.max, |
| 100 | children: [ |
| 101 | Row( |
| 102 | children: [ |
| 103 | if (isChange) |
| 104 | Padding( |
| 105 | padding: const EdgeInsets.only(right: 8.0), |
| 106 | child: Container( |
| 107 | height: 20, |
| 108 | padding: EdgeInsets.all(4), |
| 109 | decoration: BoxDecoration( |
| 110 | borderRadius: BorderRadius.all(Radius.circular(8.5)), |
| 111 | color: textColor, |
| 112 | ), |
| 113 | alignment: Alignment.center, |
| 114 | child: Text( |
| 115 | S.of(context).unspent_change, |
| 116 | style: Theme.of(context).textTheme.bodySmall?.copyWith( |
| 117 | color: backgroundColor, |
| 118 | fontSize: 10, |
| 119 | fontWeight: FontWeight.w600, |
| 120 | ), |
| 121 | ), |
| 122 | ), |
| 123 | ), |
| 124 | if (name.isNotEmpty) |
| 125 | Text( |
| 126 | '$name', |
| 127 | style: Theme.of(context).textTheme.bodyMedium?.copyWith( |
| 128 | fontWeight: FontWeight.w600, |
| 129 | color: textColor, |
| 130 | ), |
| 131 | ), |
| 132 | ], |
| 133 | ), |
| 134 | Flexible( |
| 135 | child: AddressFormatter.buildSegmentedAddress( |
| 136 | address: address, |
| 137 | walletType: walletType, |
| 138 | shouldTruncate: name.isNotEmpty || address.length > 43, |
| 139 | evenTextStyle: Theme.of(context).textTheme.bodyMedium!.copyWith( |
| 140 | fontSize: isChange ? 10 : 14, |
| 141 | color: textColor, |
| 142 | ), |
| 143 | ), |
| 144 | ), |
| 145 | ], |
| 146 | ), |
| 147 | if (hasBalance || hasReceived) |
| 148 | Padding( |
| 149 | padding: const EdgeInsets.only(top: 8.0), |
| 150 | child: Row( |
| 151 | mainAxisAlignment: MainAxisAlignment.spaceBetween, |
| 152 | mainAxisSize: MainAxisSize.max, |
| 153 | children: [ |
| 154 | Text( |
| 155 | '${hasReceived ? S.of(context).received : S.of(context).balance}: $balance', |
| 156 | style: Theme.of(context).textTheme.bodyMedium!.copyWith( |
| 157 | fontSize: 16, |
| 158 | fontWeight: FontWeight.w600, |
| 159 | color: textColor, |
| 160 | ), |
| 161 | ), |
| 162 | Text( |
| 163 | '${S.of(context).transactions.toLowerCase()}: $txCount', |
| 164 | style: Theme.of(context).textTheme.bodyMedium!.copyWith( |
| 165 | fontSize: 16, |
| 166 | fontWeight: FontWeight.w600, |
| 167 | color: textColor, |
| 168 | ), |
| 169 | ), |
| 170 | ], |
| 171 | ), |
| 172 | ), |
| 173 | ], |
| 174 | ), |
| 175 | ), |
| 176 | ], |
| 177 | ), |
| 178 | )); |
| 179 | return onEdit == null |
| 180 | ? cell |
| 181 | : Semantics( |
| 182 | label: S.of(context).slidable, |
| 183 | selected: isCurrent, |
| 184 | enabled: !isCurrent, |
| 185 | child: Slidable( |
| 186 | key: Key(address), |
| 187 | startActionPane: _actionPaneStart(context), |
| 188 | endActionPane: _actionPaneEnd(context), |
| 189 | child: cell, |
| 190 | ), |
| 191 | ); |
| 192 | } |
| 193 | |
| 194 | ActionPane _actionPaneEnd(BuildContext context) => ActionPane( |
| 195 | motion: const ScrollMotion(), |
| 196 | extentRatio: onDelete != null ? 0.4 : 0.3, |
| 197 | children: [ |
| 198 | SlidableAction( |
| 199 | onPressed: (_) => onHide?.call(), |
| 200 | backgroundColor: isHidden |
| 201 | ? Theme.of(context).colorScheme.primaryContainer |
| 202 | : Theme.of(context).colorScheme.errorContainer, |
| 203 | foregroundColor: Theme.of(context).colorScheme.onPrimaryContainer, |
| 204 | icon: isHidden ? CupertinoIcons.arrow_left : CupertinoIcons.arrow_right, |
| 205 | label: isHidden ? S.of(context).show : S.of(context).hide, |
| 206 | ), |
| 207 | ], |
| 208 | ); |
| 209 | |
| 210 | ActionPane _actionPaneStart(BuildContext context) => ActionPane( |
| 211 | motion: const ScrollMotion(), |
| 212 | extentRatio: onDelete != null ? 0.4 : 0.3, |
| 213 | children: [ |
| 214 | SlidableAction( |
| 215 | onPressed: (_) => onEdit?.call(), |
| 216 | backgroundColor: Theme.of(context).colorScheme.primaryContainer, |
| 217 | foregroundColor: Theme.of(context).colorScheme.onPrimaryContainer, |
| 218 | icon: Icons.edit, |
| 219 | label: S.of(context).edit, |
| 220 | ), |
| 221 | if (onDelete != null) |
| 222 | SlidableAction( |
| 223 | onPressed: (_) => onDelete!.call(), |
| 224 | backgroundColor: Theme.of(context).colorScheme.errorContainer, |
| 225 | foregroundColor: Theme.of(context).colorScheme.onErrorContainer, |
| 226 | icon: Icons.delete, |
| 227 | label: S.of(context).delete, |
| 228 | ), |
| 229 | ], |
| 230 | ); |
| 231 | } |