stack_wallet/lib/pages/receive_view/addresses/address_card.dart

163 lines
4.7 KiB
Dart
Raw Normal View History

2023-05-26 21:21:16 +00:00
/*
* This file is part of Stack Wallet.
*
* Copyright (c) 2023 Cypher Stack
* All Rights Reserved.
* The code is distributed under GPLv3 license, see LICENSE file for details.
* Generated by Cypher Stack on 2023-05-26
*
*/
2023-02-02 21:37:59 +00:00
import 'dart:async';
import 'dart:io';
2023-02-02 21:37:59 +00:00
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
2023-04-02 19:48:14 +00:00
import 'package:flutter_svg/flutter_svg.dart';
2023-02-03 19:22:21 +00:00
import 'package:isar/isar.dart';
2023-03-01 21:52:13 +00:00
import 'package:stackwallet/db/isar/main_db.dart';
2023-02-02 21:37:59 +00:00
import 'package:stackwallet/models/isar/models/isar_models.dart';
2023-03-21 23:18:07 +00:00
import 'package:stackwallet/pages/receive_view/addresses/address_tag.dart';
import 'package:stackwallet/themes/coin_icon_provider.dart';
2023-02-02 21:37:59 +00:00
import 'package:stackwallet/utilities/clipboard_interface.dart';
import 'package:stackwallet/utilities/enums/coin_enum.dart';
import 'package:stackwallet/utilities/text_styles.dart';
2023-04-02 19:48:14 +00:00
import 'package:stackwallet/utilities/util.dart';
import 'package:stackwallet/widgets/conditional_parent.dart';
2023-02-02 21:37:59 +00:00
import 'package:stackwallet/widgets/rounded_white_container.dart';
class AddressCard extends ConsumerStatefulWidget {
2023-02-02 21:37:59 +00:00
const AddressCard({
Key? key,
2023-03-07 13:48:25 +00:00
required this.addressId,
2023-02-02 21:37:59 +00:00
required this.walletId,
required this.coin,
2023-03-21 23:18:07 +00:00
this.onPressed,
2023-02-02 21:37:59 +00:00
this.clipboard = const ClipboardWrapper(),
}) : super(key: key);
2023-03-07 13:48:25 +00:00
final int addressId;
2023-02-02 21:37:59 +00:00
final String walletId;
final Coin coin;
final ClipboardInterface clipboard;
2023-03-21 23:18:07 +00:00
final VoidCallback? onPressed;
2023-02-02 21:37:59 +00:00
2023-02-03 19:22:21 +00:00
@override
ConsumerState<AddressCard> createState() => _AddressCardState();
2023-02-03 19:22:21 +00:00
}
class _AddressCardState extends ConsumerState<AddressCard> {
2023-04-02 19:48:14 +00:00
final isDesktop = Util.isDesktop;
2023-02-03 19:22:21 +00:00
late Stream<AddressLabel?> stream;
2023-03-07 13:48:25 +00:00
late final Address address;
2023-02-03 19:22:21 +00:00
AddressLabel? label;
@override
void initState() {
2023-03-07 13:48:25 +00:00
address = MainDB.instance.isar.addresses
.where()
.idEqualTo(widget.addressId)
.findFirstSync()!;
label = MainDB.instance.getAddressLabelSync(widget.walletId, address.value);
2023-02-03 19:22:21 +00:00
Id? id = label?.id;
if (id == null) {
label = AddressLabel(
walletId: widget.walletId,
2023-03-07 13:48:25 +00:00
addressString: address.value,
2023-02-03 19:22:21 +00:00
value: "",
tags: address.subType == AddressSubType.receiving
? ["receiving"]
: address.subType == AddressSubType.change
? ["change"]
: null,
2023-02-03 19:22:21 +00:00
);
id = MainDB.instance.putAddressLabelSync(label!);
}
stream = MainDB.instance.watchAddressLabel(id: id);
super.initState();
}
2023-02-02 21:37:59 +00:00
@override
Widget build(BuildContext context) {
return RoundedWhiteContainer(
2023-03-21 23:18:07 +00:00
onPressed: widget.onPressed,
2023-02-03 19:22:21 +00:00
child: StreamBuilder<AddressLabel?>(
2023-03-21 23:18:07 +00:00
stream: stream,
builder: (context, snapshot) {
if (snapshot.hasData) {
label = snapshot.data!;
}
2023-02-03 19:22:21 +00:00
2023-04-02 19:48:14 +00:00
return ConditionalParent(
condition: isDesktop,
builder: (child) => Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SvgPicture.file(
File(
ref.watch(
coinIconProvider(widget.coin),
),
),
2023-04-02 19:48:14 +00:00
width: 32,
height: 32,
2023-02-02 21:37:59 +00:00
),
2023-02-03 19:22:21 +00:00
const SizedBox(
2023-04-02 19:48:14 +00:00
width: 12,
2023-02-02 21:37:59 +00:00
),
2023-04-02 19:48:14 +00:00
Expanded(
child: child,
),
],
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
if (label!.value.isNotEmpty)
Text(
label!.value,
style: STextStyles.itemSubtitle(context),
textAlign: TextAlign.left,
2023-03-22 19:28:28 +00:00
),
2023-04-02 19:48:14 +00:00
if (label!.value.isNotEmpty)
SizedBox(
height: isDesktop ? 2 : 8,
),
Row(
children: [
Expanded(
child: Text(
address.value,
style: STextStyles.itemSubtitle12(context),
),
),
],
),
const SizedBox(
height: 10,
2023-03-21 23:18:07 +00:00
),
2023-04-02 19:48:14 +00:00
if (label!.tags != null && label!.tags!.isNotEmpty)
Wrap(
spacing: 10,
runSpacing: 10,
children: label!.tags!
.map(
(e) => AddressTag(
tag: e,
),
)
.toList(),
),
],
),
2023-03-21 23:18:07 +00:00
);
},
),
2023-02-02 21:37:59 +00:00
);
}
}