| 1 | import 'dart:io'; |
| 2 | |
| 3 | import 'package:cake_wallet/routes.dart'; |
| 4 | import 'package:cake_wallet/src/widgets/seedphrase_grid_widget.dart'; |
| 5 | import 'package:cake_wallet/src/widgets/warning_box_widget.dart'; |
| 6 | import 'package:cake_wallet/utils/clipboard_util.dart'; |
| 7 | import 'package:cake_wallet/utils/share_util.dart'; |
| 8 | import 'package:cake_wallet/utils/show_bar.dart'; |
| 9 | import 'package:cw_core/wallet_type.dart'; |
| 10 | import 'package:flutter/foundation.dart'; |
| 11 | import 'package:flutter/material.dart'; |
| 12 | import 'package:flutter/services.dart'; |
| 13 | import "package:file_picker/file_picker.dart"; |
| 14 | import 'package:flutter_mobx/flutter_mobx.dart'; |
| 15 | import 'package:cake_wallet/generated/i18n.dart'; |
| 16 | import 'package:cake_wallet/src/widgets/primary_button.dart'; |
| 17 | import 'package:cake_wallet/src/screens/base_page.dart'; |
| 18 | import 'package:cake_wallet/view_model/wallet_seed_view_model.dart'; |
| 19 | |
| 20 | class WalletSeedPage extends BasePage { |
| 21 | WalletSeedPage(this.walletSeedViewModel, {required this.isNewWalletCreated}); |
| 22 | |
| 23 | @override |
| 24 | String get title => |
| 25 | '${walletTypeToString(walletSeedViewModel.walletType)} ${S.current.seed_title}'; |
| 26 | |
| 27 | final bool isNewWalletCreated; |
| 28 | final WalletSeedViewModel walletSeedViewModel; |
| 29 | |
| 30 | @override |
| 31 | Widget trailing(BuildContext context) { |
| 32 | return isNewWalletCreated |
| 33 | ? GestureDetector( |
| 34 | key: ValueKey('wallet_seed_page_copy_seeds_button_key'), |
| 35 | onTap: () { |
| 36 | ClipboardUtil.setSensitiveDataToClipboard( |
| 37 | ClipboardData(text: walletSeedViewModel.seed), |
| 38 | ); |
| 39 | showBar<void>(context, S.of(context).copied_to_clipboard); |
| 40 | }, |
| 41 | child: Container( |
| 42 | padding: EdgeInsets.all(8), |
| 43 | width: 40, |
| 44 | alignment: Alignment.center, |
| 45 | margin: EdgeInsets.only(left: 10), |
| 46 | decoration: BoxDecoration( |
| 47 | borderRadius: BorderRadius.all(Radius.circular(8)), |
| 48 | color: Theme.of(context).colorScheme.surfaceContainerHighest, |
| 49 | ), |
| 50 | child: Image.asset( |
| 51 | 'assets/images/copy_address.png', |
| 52 | color: Theme.of(context).colorScheme.onSurfaceVariant, |
| 53 | ), |
| 54 | ), |
| 55 | ) |
| 56 | : Offstage(); |
| 57 | } |
| 58 | |
| 59 | @override |
| 60 | Widget body(BuildContext context) { |
| 61 | return PopScope( |
| 62 | //FIXME i can't figure out why this was here? it caused back gesture to break |
| 63 | canPop: true, |
| 64 | child: Container( |
| 65 | alignment: Alignment.center, |
| 66 | child: Column( |
| 67 | mainAxisAlignment: MainAxisAlignment.spaceBetween, |
| 68 | children: <Widget>[ |
| 69 | Observer( |
| 70 | builder: (_) { |
| 71 | return Expanded( |
| 72 | child: Padding( |
| 73 | padding: const EdgeInsets.symmetric(horizontal: 16), |
| 74 | child: Column( |
| 75 | crossAxisAlignment: CrossAxisAlignment.center, |
| 76 | children: <Widget>[ |
| 77 | SizedBox(height: 16), |
| 78 | WarningBox(content: S.current.cake_seeds_save_disclaimer), |
| 79 | SizedBox(height: 36), |
| 80 | Text( |
| 81 | key: ValueKey('wallet_seed_page_wallet_name_text_key'), |
| 82 | walletSeedViewModel.name, |
| 83 | style: Theme.of(context).textTheme.bodyMedium!.copyWith( |
| 84 | fontSize: 18, |
| 85 | fontWeight: FontWeight.w600, |
| 86 | color: Theme.of(context).colorScheme.onSurface, |
| 87 | ), |
| 88 | ), |
| 89 | SizedBox(height: 24), |
| 90 | Expanded( |
| 91 | child: SeedPhraseGridWidget( |
| 92 | list: walletSeedViewModel.seedSplit, |
| 93 | ), |
| 94 | ), |
| 95 | ], |
| 96 | ), |
| 97 | ), |
| 98 | ); |
| 99 | }, |
| 100 | ), |
| 101 | Column( |
| 102 | children: <Widget>[ |
| 103 | Padding( |
| 104 | padding: EdgeInsets.symmetric(horizontal: 14, vertical: 8), |
| 105 | child: Row( |
| 106 | mainAxisSize: MainAxisSize.max, |
| 107 | children: <Widget>[ |
| 108 | Flexible( |
| 109 | child: Container( |
| 110 | padding: EdgeInsets.only(right: 8.0, top: 8.0), |
| 111 | child: PrimaryButton( |
| 112 | key: ValueKey('wallet_seed_page_save_seeds_button_key'), |
| 113 | onPressed: () { |
| 114 | _shareSeed(context); |
| 115 | }, |
| 116 | text: S.of(context).save, |
| 117 | color: Theme.of(context).colorScheme.surfaceContainer, |
| 118 | textColor: Theme.of(context).colorScheme.onSecondaryContainer, |
| 119 | ), |
| 120 | ), |
| 121 | ), |
| 122 | Flexible( |
| 123 | child: Container( |
| 124 | padding: EdgeInsets.only(left: 8.0, top: 8.0), |
| 125 | child: PrimaryButton( |
| 126 | key: ValueKey('wallet_seed_page_verify_seed_button_key'), |
| 127 | onPressed: () => |
| 128 | Navigator.pushNamed(context, Routes.walletSeedVerificationPage), |
| 129 | text: S.current.verify_seed, |
| 130 | color: Theme.of(context).colorScheme.primary, |
| 131 | textColor: Theme.of(context).colorScheme.onPrimary, |
| 132 | ), |
| 133 | ), |
| 134 | ), |
| 135 | ], |
| 136 | ), |
| 137 | ), |
| 138 | SizedBox(height: 16), |
| 139 | ], |
| 140 | ) |
| 141 | ], |
| 142 | ), |
| 143 | ), |
| 144 | ); |
| 145 | } |
| 146 | |
| 147 | void _shareSeed(BuildContext context) async { |
| 148 | switch (defaultTargetPlatform) { |
| 149 | case TargetPlatform.android: |
| 150 | case TargetPlatform.iOS: |
| 151 | ShareUtil.share( |
| 152 | text: walletSeedViewModel.seed, |
| 153 | context: context, |
| 154 | ); |
| 155 | break; |
| 156 | case TargetPlatform.macOS: |
| 157 | case TargetPlatform.windows: |
| 158 | case TargetPlatform.linux: |
| 159 | final path = await FilePicker.platform.saveFile( |
| 160 | dialogTitle: "Save seed as", |
| 161 | fileName: "${walletSeedViewModel.name}-seed.txt", |
| 162 | lockParentWindow: true, |
| 163 | ); |
| 164 | if (path != null) { |
| 165 | final file = File(path); |
| 166 | await file.writeAsString(walletSeedViewModel.seed); |
| 167 | } |
| 168 | default: |
| 169 | break; |
| 170 | } |
| 171 | } |
| 172 | } |