100 lines
3.2 KiB
Dart
100 lines
3.2 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
import 'package:provider/provider.dart';
|
|
|
|
import 'package:pshared/controllers/balance_mask/wallets.dart';
|
|
import 'package:pshared/models/ledger/account.dart';
|
|
import 'package:pshared/models/payment/chain_network.dart';
|
|
import 'package:pshared/models/payment/source_type.dart';
|
|
import 'package:pshared/models/payment/wallet.dart';
|
|
import 'package:pshared/utils/l10n/chain.dart';
|
|
|
|
import 'package:pweb/pages/dashboard/buttons/balance/amount.dart';
|
|
import 'package:pweb/pages/dashboard/buttons/balance/ledger_amount.dart';
|
|
import 'package:pweb/pages/dashboard/buttons/balance/source/actions/ledger.dart';
|
|
import 'package:pweb/pages/dashboard/buttons/balance/source/actions/wallet.dart';
|
|
import 'package:pweb/pages/dashboard/buttons/balance/source/card_layout.dart';
|
|
|
|
import 'package:pweb/generated/i18n/app_localizations.dart';
|
|
|
|
|
|
class BalanceSourceCard extends StatelessWidget {
|
|
final PaymentSourceType _type;
|
|
final Wallet? _wallet;
|
|
final LedgerAccount? _ledgerAccount;
|
|
final VoidCallback onTap;
|
|
final VoidCallback onAddFunds;
|
|
|
|
const BalanceSourceCard.wallet({
|
|
super.key,
|
|
required Wallet wallet,
|
|
required this.onTap,
|
|
required this.onAddFunds,
|
|
}) : _type = PaymentSourceType.wallet,
|
|
_wallet = wallet,
|
|
_ledgerAccount = null;
|
|
|
|
const BalanceSourceCard.ledger({
|
|
super.key,
|
|
required LedgerAccount account,
|
|
required this.onTap,
|
|
required this.onAddFunds,
|
|
}) : _type = PaymentSourceType.ledger,
|
|
_wallet = null,
|
|
_ledgerAccount = account;
|
|
|
|
@override
|
|
Widget build(BuildContext context) => switch (_type) {
|
|
PaymentSourceType.wallet => _buildWalletCard(context, _wallet!),
|
|
PaymentSourceType.ledger => _buildLedgerCard(context, _ledgerAccount!),
|
|
};
|
|
|
|
Widget _buildWalletCard(BuildContext context, Wallet wallet) {
|
|
final networkLabel =
|
|
(wallet.network == null || wallet.network == ChainNetwork.unspecified)
|
|
? null
|
|
: wallet.network!.localizedName(context);
|
|
final symbol = wallet.tokenSymbol?.trim();
|
|
|
|
return BalanceSourceCardLayout(
|
|
title: wallet.name,
|
|
subtitle: networkLabel,
|
|
badge: (symbol == null || symbol.isEmpty) ? null : symbol,
|
|
onTap: onTap,
|
|
actions: WalletSourceActions(
|
|
walletRef: wallet.id,
|
|
onAddFunds: onAddFunds,
|
|
),
|
|
amount: BalanceAmount(
|
|
wallet: wallet,
|
|
onToggleMask: () {
|
|
context.read<WalletsController>().toggleBalanceMask(wallet.id);
|
|
},
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildLedgerCard(BuildContext context, LedgerAccount account) {
|
|
final loc = AppLocalizations.of(context)!;
|
|
final accountName = account.name.trim();
|
|
final accountCode = account.accountCode.trim();
|
|
final title = accountName.isNotEmpty ? accountName : loc.paymentTypeLedger;
|
|
final subtitle = accountCode.isNotEmpty ? accountCode : null;
|
|
final badge = account.currency.trim().isEmpty
|
|
? null
|
|
: account.currency.toUpperCase();
|
|
|
|
return BalanceSourceCardLayout(
|
|
title: title,
|
|
subtitle: subtitle,
|
|
badge: badge,
|
|
onTap: onTap,
|
|
actions: LedgerSourceActions(
|
|
ledgerAccountRef: account.ledgerAccountRef,
|
|
onAddFunds: onAddFunds,
|
|
),
|
|
amount: LedgerBalanceAmount(account: account),
|
|
);
|
|
}
|
|
}
|