add copy to clipboard button to mobile xpub dialog

This commit is contained in:
sneurlax 2023-04-06 13:56:35 -05:00
parent 767bcecd03
commit 6f3691625a

View file

@ -1,8 +1,11 @@
import 'dart:async';
import 'package:bip32/bip32.dart' as bip32;
import 'package:bip39/bip39.dart' as bip39;
import 'package:decimal/decimal.dart';
import 'package:event_bus/event_bus.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_svg/svg.dart';
import 'package:isar/isar.dart';
@ -38,6 +41,7 @@ import 'package:stackwallet/services/event_bus/global_event_bus.dart';
import 'package:stackwallet/services/exchange/exchange_data_loading_service.dart';
import 'package:stackwallet/services/mixins/paynym_wallet_interface.dart';
import 'package:stackwallet/utilities/assets.dart';
import 'package:stackwallet/utilities/clipboard_interface.dart';
import 'package:stackwallet/utilities/constants.dart';
import 'package:stackwallet/utilities/enums/backup_frequency_type.dart';
import 'package:stackwallet/utilities/enums/coin_enum.dart';
@ -72,6 +76,7 @@ class WalletView extends ConsumerStatefulWidget {
required this.walletId,
required this.managerProvider,
this.eventBus,
this.clipboardInterface = const ClipboardWrapper(),
}) : super(key: key);
static const String routeName = "/wallet";
@ -81,6 +86,8 @@ class WalletView extends ConsumerStatefulWidget {
final ChangeNotifierProvider<Manager> managerProvider;
final EventBus? eventBus;
final ClipboardInterface clipboardInterface;
@override
ConsumerState<WalletView> createState() => _WalletViewState();
}
@ -100,10 +107,13 @@ class _WalletViewState extends ConsumerState<WalletView> {
bool _rescanningOnOpen = false;
late ClipboardInterface _clipboardInterface;
@override
void initState() {
walletId = widget.walletId;
managerProvider = widget.managerProvider;
_clipboardInterface = widget.clipboardInterface;
ref.read(managerProvider).isActiveWallet = true;
if (!ref.read(managerProvider).shouldAutoSync) {
@ -189,6 +199,16 @@ class _WalletViewState extends ConsumerState<WalletView> {
super.dispose();
}
Future<void> _copy(String xpub) async {
await _clipboardInterface.setData(ClipboardData(text: xpub));
unawaited(showFloatingFlushBar(
type: FlushBarType.info,
message: "Copied to clipboard",
iconAsset: Assets.svg.copy,
context: context,
));
}
DateTime? _cachedTime;
Future<bool> _onWillPop() async {
@ -918,7 +938,50 @@ class _WalletViewState extends ConsumerState<WalletView> {
label: "Show xPub",
icon: const XPubNavIcon(),
onTap: () async {
print("TODO");
final List<String> mnemonic = await ref
.read(walletsChangeNotifierProvider)
.getManager(walletId)
.mnemonic;
final seed = bip39.mnemonicToSeed(mnemonic.join(' '));
final node = bip32.BIP32.fromSeed(seed);
final xpub = node.neutered().toBase58();
showDialog<dynamic>(
barrierDismissible: true,
context: context,
builder: (_) => StackDialog(
title: "Wallet xPub",
message: xpub,
leftButton: TextButton(
style: Theme.of(context)
.extension<StackColors>()!
.getSecondaryEnabledButtonStyle(context),
onPressed: () async {
await _copy(xpub);
},
child: Text(
"Copy to clipboard",
style: STextStyles.button(context).copyWith(
color: Theme.of(context)
.extension<StackColors>()!
.accentColorDark),
),
),
rightButton: TextButton(
style: Theme.of(context)
.extension<StackColors>()!
.getPrimaryEnabledButtonStyle(context),
onPressed: () {
Navigator.pop(context);
},
child: Text(
"Continue",
style: STextStyles.button(context),
),
),
),
);
},
),
],