stack_wallet/lib/pages_desktop_specific/coin_control/utxo_row.dart

167 lines
5 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-13 22:41:53 +00:00
import 'package:stackwallet/widgets/desktop/secondary_button.dart';
import 'package:stackwallet/widgets/icon_widgets/utxo_status_icon.dart';
import 'package:stackwallet/widgets/rounded_white_container.dart';
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-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-13 22:41:53 +00:00
@override
ConsumerState<UtxoRow> createState() => _UtxoRowState();
}
class _UtxoRowState extends ConsumerState<UtxoRow> {
late Stream<UTXO?> stream;
late UTXO utxo;
@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!;
}
return RoundedWhiteContainer(
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,
),
Text(
"${Format.satoshisToAmount(
utxo.value,
coin: coin,
).toStringAsFixed(coin.decimals)} ${coin.ticker}",
textAlign: TextAlign.right,
style: STextStyles.w600_14(context),
),
const SizedBox(
width: 10,
),
Expanded(
flex: 13,
child: Text(
utxo.name.isNotEmpty ? utxo.name : utxo.address ?? utxo.txid,
textAlign: TextAlign.center,
style: STextStyles.w500_12(context).copyWith(
color: Theme.of(context)
.extension<StackColors>()!
.textSubtitle1,
),
),
),
const SizedBox(
width: 10,
),
SecondaryButton(
width: 120,
buttonHeight: ButtonHeight.xs,
label: "Details",
onPressed: () async {
await showDialog<String?>(
context: context,
builder: (context) => UtxoDetailsView(
utxoId: utxo.id,
walletId: widget.walletId,
),
);
},
2023-03-14 16:26:35 +00:00
),
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
);
}
}