| 1 | import 'package:cake_wallet/generated/i18n.dart'; |
| 2 | import 'package:cake_wallet/src/screens/base_page.dart'; |
| 3 | import 'package:cake_wallet/src/widgets/address_text_field.dart'; |
| 4 | import 'package:cake_wallet/src/widgets/primary_button.dart'; |
| 5 | import 'package:cake_wallet/view_model/dashboard/nft_view_model.dart'; |
| 6 | import 'package:cw_core/wallet_type.dart'; |
| 7 | import 'package:flutter/material.dart'; |
| 8 | import 'package:flutter/services.dart'; |
| 9 | import 'package:flutter_mobx/flutter_mobx.dart'; |
| 10 | |
| 11 | class ImportNFTPage extends StatefulWidget { |
| 12 | const ImportNFTPage({required this.nftViewModel, super.key}); |
| 13 | |
| 14 | final NFTViewModel nftViewModel; |
| 15 | |
| 16 | @override |
| 17 | State<ImportNFTPage> createState() => _ImportNFTPageState(); |
| 18 | } |
| 19 | |
| 20 | class _ImportNFTPageState extends State<ImportNFTPage> { |
| 21 | late TextEditingController tokenAddressController; |
| 22 | late TextEditingController tokenIDController; |
| 23 | |
| 24 | @override |
| 25 | void initState() { |
| 26 | super.initState(); |
| 27 | tokenAddressController = TextEditingController(); |
| 28 | tokenIDController = TextEditingController(); |
| 29 | } |
| 30 | |
| 31 | @override |
| 32 | void dispose() { |
| 33 | tokenAddressController.dispose(); |
| 34 | tokenIDController.dispose(); |
| 35 | super.dispose(); |
| 36 | } |
| 37 | |
| 38 | @override |
| 39 | Widget build(BuildContext context) { |
| 40 | return _ImportNFTPage( |
| 41 | nftViewModel: widget.nftViewModel, |
| 42 | tokenAddressController: tokenAddressController, |
| 43 | tokenIDController: tokenIDController, |
| 44 | ); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | class _ImportNFTPage extends BasePage { |
| 49 | _ImportNFTPage({ |
| 50 | required this.tokenIDController, |
| 51 | required this.tokenAddressController, |
| 52 | required this.nftViewModel, |
| 53 | }); |
| 54 | |
| 55 | final NFTViewModel nftViewModel; |
| 56 | final TextEditingController tokenIDController; |
| 57 | final TextEditingController tokenAddressController; |
| 58 | |
| 59 | @override |
| 60 | String? get title => S.current.import; |
| 61 | |
| 62 | @override |
| 63 | Widget body(BuildContext context) { |
| 64 | return Padding( |
| 65 | padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 16), |
| 66 | child: Column( |
| 67 | crossAxisAlignment: CrossAxisAlignment.start, |
| 68 | children: [ |
| 69 | Text( |
| 70 | S.current.address, |
| 71 | textAlign: TextAlign.center, |
| 72 | style: Theme.of(context).textTheme.titleMedium?.copyWith( |
| 73 | fontWeight: FontWeight.w800, |
| 74 | color: Theme.of(context).colorScheme.onSurfaceVariant, |
| 75 | height: 1, |
| 76 | ), |
| 77 | ), |
| 78 | SizedBox(height: 8), |
| 79 | AddressTextField( |
| 80 | controller: tokenAddressController, |
| 81 | options: [AddressTextFieldOption.paste], |
| 82 | onPushPasteButton: (context) async { |
| 83 | final clipboard = await Clipboard.getData('text/plain'); |
| 84 | final tokenAddress = clipboard?.text ?? ''; |
| 85 | |
| 86 | if (tokenAddress.isNotEmpty) { |
| 87 | tokenAddressController.text = tokenAddress; |
| 88 | } |
| 89 | }, |
| 90 | fillColor: Theme.of(context).colorScheme.surfaceContainer, |
| 91 | iconColor: Theme.of(context).colorScheme.primary, |
| 92 | placeholder: '0x...', |
| 93 | textStyle: Theme.of(context).textTheme.bodyLarge?.copyWith( |
| 94 | color: Theme.of(context).colorScheme.onSurface, |
| 95 | ), |
| 96 | hintStyle: Theme.of(context).textTheme.bodyLarge?.copyWith( |
| 97 | color: Theme.of(context).colorScheme.onSurfaceVariant, |
| 98 | ), |
| 99 | ), |
| 100 | if (nftViewModel.appStore.wallet!.type != WalletType.solana) ...[ |
| 101 | SizedBox(height: 48), |
| 102 | Text( |
| 103 | S.current.tokenID, |
| 104 | textAlign: TextAlign.center, |
| 105 | style: Theme.of(context).textTheme.titleMedium?.copyWith( |
| 106 | fontWeight: FontWeight.w800, |
| 107 | color: Theme.of(context).colorScheme.onSurfaceVariant, |
| 108 | height: 1, |
| 109 | ), |
| 110 | ), |
| 111 | SizedBox(height: 8), |
| 112 | AddressTextField( |
| 113 | controller: tokenIDController, |
| 114 | options: [AddressTextFieldOption.paste], |
| 115 | onPushPasteButton: (context) async { |
| 116 | final clipboard = await Clipboard.getData('text/plain'); |
| 117 | final tokenID = clipboard?.text ?? ''; |
| 118 | |
| 119 | if (tokenID.isNotEmpty) { |
| 120 | tokenIDController.text = tokenID; |
| 121 | } |
| 122 | }, |
| 123 | iconColor: Theme.of(context).colorScheme.primary, |
| 124 | placeholder: S.current.enterTokenID, |
| 125 | textStyle: Theme.of(context).textTheme.bodyLarge?.copyWith( |
| 126 | color: Theme.of(context).colorScheme.onSurface, |
| 127 | ), |
| 128 | hintStyle: Theme.of(context).textTheme.bodyLarge?.copyWith( |
| 129 | color: Theme.of(context).colorScheme.onSurfaceVariant, |
| 130 | ), |
| 131 | ), |
| 132 | ], |
| 133 | Spacer(), |
| 134 | Observer(builder: (context) { |
| 135 | return LoadingPrimaryButton( |
| 136 | isLoading: nftViewModel.isImportNFTLoading, |
| 137 | text: S.current.import, |
| 138 | color: Theme.of(context).colorScheme.primary, |
| 139 | textColor: Theme.of(context).colorScheme.onPrimary, |
| 140 | onPressed: () async { |
| 141 | await nftViewModel.importNFT(tokenAddressController.text, tokenIDController.text); |
| 142 | if (context.mounted && Navigator.of(context).canPop()) Navigator.pop(context); |
| 143 | }, |
| 144 | ); |
| 145 | }), |
| 146 | SizedBox(height: 16), |
| 147 | ], |
| 148 | ), |
| 149 | ); |
| 150 | } |
| 151 | } |