94 lines
2.8 KiB
Dart
94 lines
2.8 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
import 'package:pshared/controllers/balance_mask/wallets.dart';
|
|
import 'package:pshared/models/payment/wallet.dart';
|
|
import 'package:pshared/utils/currency.dart';
|
|
|
|
import 'package:pweb/generated/i18n/app_localizations.dart';
|
|
|
|
|
|
class SourceWalletSelector extends StatelessWidget {
|
|
const SourceWalletSelector({
|
|
super.key,
|
|
required this.walletsController,
|
|
this.isBusy = false,
|
|
this.onChanged,
|
|
});
|
|
|
|
final WalletsController walletsController;
|
|
final bool isBusy;
|
|
final ValueChanged<Wallet>? onChanged;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final wallets = walletsController.wallets;
|
|
final selectedWalletRef = walletsController.selectedWalletRef;
|
|
final theme = Theme.of(context);
|
|
final l10n = AppLocalizations.of(context)!;
|
|
|
|
if (wallets.isEmpty) {
|
|
return Text(l10n.noWalletsAvailable, style: theme.textTheme.bodySmall);
|
|
}
|
|
|
|
final effectiveSelectedWalletRef = selectedWalletRef != null &&
|
|
wallets.any((wallet) => wallet.id == selectedWalletRef)
|
|
? selectedWalletRef
|
|
: null;
|
|
|
|
return DropdownButtonFormField<String>(
|
|
initialValue: effectiveSelectedWalletRef,
|
|
isExpanded: true,
|
|
decoration: InputDecoration(
|
|
labelText: l10n.whereGetMoney,
|
|
border: const OutlineInputBorder(),
|
|
contentPadding: const EdgeInsets.symmetric(
|
|
horizontal: 12,
|
|
vertical: 10,
|
|
),
|
|
),
|
|
items: wallets
|
|
.map(
|
|
(wallet) => DropdownMenuItem<String>(
|
|
value: wallet.id,
|
|
child: Text(
|
|
'${_walletLabel(wallet)} - ${currencyCodeToSymbol(wallet.currency)} ${amountToString(wallet.balance)}',
|
|
overflow: TextOverflow.ellipsis,
|
|
),
|
|
),
|
|
)
|
|
.toList(growable: false),
|
|
onChanged: isBusy
|
|
? null
|
|
: (value) {
|
|
if (value == null) return;
|
|
walletsController.selectWalletByRef(value);
|
|
final selected = walletsController.selectedWallet;
|
|
if (selected != null) {
|
|
onChanged?.call(selected);
|
|
}
|
|
},
|
|
);
|
|
}
|
|
|
|
String _walletLabel(Wallet wallet) {
|
|
final description = wallet.description?.trim();
|
|
if (description != null && description.isNotEmpty) {
|
|
return description;
|
|
}
|
|
final name = wallet.name.trim();
|
|
if (name.isNotEmpty && !_looksLikeId(name)) {
|
|
return name;
|
|
}
|
|
final token = wallet.tokenSymbol?.trim();
|
|
if (token != null && token.isNotEmpty) {
|
|
return '$token wallet';
|
|
}
|
|
return '${currencyCodeToString(wallet.currency)} wallet';
|
|
}
|
|
|
|
bool _looksLikeId(String value) {
|
|
return RegExp(r'^[a-f0-9]{12,}$', caseSensitive: false)
|
|
.hasMatch(value);
|
|
}
|
|
}
|