stack_wallet/lib/pages_desktop_specific/coin_control/utxo_row.dart

209 lines
6.6 KiB
Dart
Raw Normal View History

2023-03-13 22:41:53 +00:00
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
2023-03-15 15:48:39 +00:00
import 'package:isar/isar.dart';
2023-03-14 16:26:35 +00:00
import 'package:stackwallet/db/main_db.dart';
2023-03-13 22:41:53 +00:00
import 'package:stackwallet/models/isar/models/isar_models.dart';
2023-03-13 23:09:14 +00:00
import 'package:stackwallet/pages/coin_control/utxo_details_view.dart';
2023-03-14 16:26:35 +00:00
import 'package:stackwallet/providers/global/wallets_provider.dart';
2023-03-13 22:41:53 +00:00
import 'package:stackwallet/utilities/enums/coin_enum.dart';
2023-03-14 16:26:35 +00:00
import 'package:stackwallet/utilities/format.dart';
import 'package:stackwallet/utilities/text_styles.dart';
import 'package:stackwallet/utilities/theme/stack_colors.dart';
2023-03-15 18:01:10 +00:00
import 'package:stackwallet/widgets/conditional_parent.dart';
import 'package:stackwallet/widgets/custom_buttons/blue_text_button.dart';
2023-03-13 22:41:53 +00:00
import 'package:stackwallet/widgets/desktop/secondary_button.dart';
import 'package:stackwallet/widgets/icon_widgets/utxo_status_icon.dart';
2023-03-15 18:01:10 +00:00
import 'package:stackwallet/widgets/rounded_container.dart';
2023-03-13 22:41:53 +00:00
2023-03-14 16:26:35 +00:00
class UtxoRowData {
2023-03-15 15:48:39 +00:00
UtxoRowData(this.utxoId, this.selected);
2023-03-14 16:26:35 +00:00
2023-03-15 15:48:39 +00:00
Id utxoId;
2023-03-14 16:26:35 +00:00
bool selected;
@override
String toString() {
2023-03-15 15:48:39 +00:00
return "selected=$selected: $utxoId";
2023-03-14 16:26:35 +00:00
}
@override
bool operator ==(Object other) {
2023-03-15 15:48:39 +00:00
return other is UtxoRowData && other.utxoId == utxoId;
2023-03-14 16:26:35 +00:00
}
@override
2023-03-15 15:48:39 +00:00
int get hashCode => Object.hashAll([utxoId.hashCode]);
2023-03-14 16:26:35 +00:00
}
2023-03-13 22:41:53 +00:00
class UtxoRow extends ConsumerStatefulWidget {
const UtxoRow({
Key? key,
2023-03-14 16:26:35 +00:00
required this.data,
2023-03-13 22:41:53 +00:00
required this.walletId,
2023-03-14 16:26:35 +00:00
this.onSelectionChanged,
2023-03-15 18:01:10 +00:00
this.compact = false,
2023-03-13 22:41:53 +00:00
}) : super(key: key);
final String walletId;
2023-03-14 16:26:35 +00:00
final UtxoRowData data;
final void Function(UtxoRowData)? onSelectionChanged;
2023-03-15 18:01:10 +00:00
final bool compact;
2023-03-13 22:41:53 +00:00
@override
ConsumerState<UtxoRow> createState() => _UtxoRowState();
}
class _UtxoRowState extends ConsumerState<UtxoRow> {
late Stream<UTXO?> stream;
late UTXO utxo;
2023-03-15 18:01:10 +00:00
void _details() async {
await showDialog<String?>(
context: context,
builder: (context) => UtxoDetailsView(
utxoId: utxo.id,
walletId: widget.walletId,
),
);
}
2023-03-13 22:41:53 +00:00
@override
void initState() {
2023-03-15 15:48:39 +00:00
utxo = MainDB.instance.isar.utxos
.where()
.idEqualTo(widget.data.utxoId)
.findFirstSync()!;
2023-03-13 22:41:53 +00:00
stream = MainDB.instance.watchUTXO(id: utxo.id);
super.initState();
}
@override
Widget build(BuildContext context) {
debugPrint("BUILD: $runtimeType");
final coin = ref.watch(walletsChangeNotifierProvider
.select((value) => value.getManager(widget.walletId).coin));
final currentChainHeight = ref.watch(walletsChangeNotifierProvider
.select((value) => value.getManager(widget.walletId).currentHeight));
2023-03-13 23:09:14 +00:00
return StreamBuilder<UTXO?>(
stream: stream,
builder: (context, snapshot) {
if (snapshot.hasData) {
utxo = snapshot.data!;
}
2023-03-15 18:01:10 +00:00
return RoundedContainer(
borderColor: widget.compact
? Theme.of(context).extension<StackColors>()!.textFieldDefaultBG
: null,
color: Theme.of(context).extension<StackColors>()!.popupBG,
2023-03-14 16:26:35 +00:00
boxShadow: widget.data.selected
? [
Theme.of(context).extension<StackColors>()!.standardBoxShadow,
]
: null,
2023-03-13 23:09:14 +00:00
child: Row(
children: [
Checkbox(
2023-03-14 16:26:35 +00:00
value: widget.data.selected,
onChanged: (value) {
setState(() {
widget.data.selected = value!;
});
widget.onSelectionChanged?.call(widget.data);
},
2023-03-13 22:41:53 +00:00
),
2023-03-13 23:09:14 +00:00
const SizedBox(
width: 10,
),
UTXOStatusIcon(
blocked: utxo.isBlocked,
status: utxo.isConfirmed(
currentChainHeight,
coin.requiredConfirmations,
)
? UTXOStatusIconStatus.confirmed
: UTXOStatusIconStatus.unconfirmed,
background: Theme.of(context).extension<StackColors>()!.popupBG,
selected: false,
width: 32,
height: 32,
),
const SizedBox(
width: 10,
),
2023-03-15 18:01:10 +00:00
if (!widget.compact)
Text(
"${Format.satoshisToAmount(
utxo.value,
coin: coin,
).toStringAsFixed(coin.decimals)} ${coin.ticker}",
textAlign: TextAlign.right,
style: STextStyles.w600_14(context),
),
if (!widget.compact)
const SizedBox(
width: 10,
),
2023-03-13 23:09:14 +00:00
Expanded(
2023-03-15 18:01:10 +00:00
child: ConditionalParent(
condition: widget.compact,
builder: (child) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Text(
"${Format.satoshisToAmount(
utxo.value,
coin: coin,
).toStringAsFixed(coin.decimals)} ${coin.ticker}",
textAlign: TextAlign.right,
style: STextStyles.w600_14(context),
),
const SizedBox(
height: 2,
),
child,
],
);
},
child: Text(
utxo.name.isNotEmpty
? utxo.name
: utxo.address ?? utxo.txid,
textAlign:
widget.compact ? TextAlign.left : TextAlign.center,
style: STextStyles.w500_12(context).copyWith(
color: Theme.of(context)
.extension<StackColors>()!
.textSubtitle1,
),
2023-03-13 23:09:14 +00:00
),
),
),
const SizedBox(
width: 10,
),
2023-03-15 18:01:10 +00:00
widget.compact
? CustomTextButton(
text: "Details",
onTap: _details,
)
: SecondaryButton(
width: 120,
buttonHeight: ButtonHeight.xs,
label: "Details",
onPressed: _details,
2023-03-13 23:09:14 +00:00
),
],
2023-03-13 22:41:53 +00:00
),
2023-03-13 23:09:14 +00:00
);
},
2023-03-13 22:41:53 +00:00
);
}
}