diff --git a/lib/providers/wallet_options.dart b/lib/providers/wallet_options.dart index 6b862b6..fe6c33f 100644 --- a/lib/providers/wallet_options.dart +++ b/lib/providers/wallet_options.dart @@ -23,6 +23,7 @@ class WalletOptionsProvider with ChangeNotifier { FocusNode walletNameFocus = FocusNode(); TextEditingController nameController = TextEditingController(); late bool isDefaultWallet; + bool canValidateNameBool = false; Future? get badWallet => null; @@ -41,25 +42,6 @@ class WalletOptionsProvider with ChangeNotifier { _newWalletName.text = ''; } - bool editWalletName(List _wID, {bool? isCesium}) { - bool nameState; - if (isEditing) { - if (!nameController.text.contains(':') && - nameController.text.length <= 39) { - _renameWallet(_wID, nameController.text, isCesium: isCesium!); - nameState = true; - } else { - nameState = false; - } - } else { - nameState = true; - } - - isEditing ? isEditing = false : isEditing = true; - notifyListeners(); - return nameState; - } - Future deleteWallet(context, WalletData wallet) async { SubstrateSdk _sub = Provider.of(context, listen: false); final bool? _answer = await (confirmPopup(context, @@ -290,6 +272,111 @@ class WalletOptionsProvider with ChangeNotifier { ); } + Future editWalletName(BuildContext context, List _wID) async { + TextEditingController walletName = TextEditingController(); + canValidateNameBool = false; + + return showDialog( + context: context, + barrierDismissible: true, + builder: (BuildContext context) { + return AlertDialog( + title: const Text( + 'Choisissez un nouveau nom\n pour votre portefeuille :', + textAlign: TextAlign.center, + style: TextStyle(fontSize: 20, fontWeight: FontWeight.w500), + ), + content: SizedBox( + height: 100, + child: Column(children: [ + const SizedBox(height: 20), + TextField( + onChanged: (_) => canValidateName(context, walletName), + textAlign: TextAlign.center, + autofocus: true, + controller: walletName, + style: const TextStyle(fontSize: 19), + ) + ]), + ), + actions: [ + Row( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Consumer( + builder: (context, _wOptions, _) { + return TextButton( + key: const Key('infoPopup'), + child: Text( + "Valider", + style: TextStyle( + fontSize: 21, + color: canValidateNameBool + ? const Color(0xffD80000) + : Colors.grey, + fontWeight: FontWeight.w600, + ), + ), + onPressed: () async { + if (canValidateNameBool) { + nameController.text = walletName.text; + _renameWallet(_wID, walletName.text, isCesium: false); + // notifyListeners(); + Navigator.pop(context); + } + }, + ); + }) + ], + ), + Row( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + TextButton( + key: const Key('cancel'), + child: Text( + "Annuler", + style: TextStyle( + fontSize: 18, + color: Colors.grey[800], + fontWeight: FontWeight.w300), + ), + onPressed: () async { + Navigator.pop(context); + }, + ) + ], + ), + const SizedBox(height: 20) + ], + ); + }, + ); + } + + bool canValidateName(BuildContext context, TextEditingController walletName) { + MyWalletsProvider _myWalletProvider = + Provider.of(context, listen: false); + + bool isNameValid = walletName.text.length >= 2 && + !walletName.text.contains(':') && + walletName.text.length <= 39; + + if (isNameValid) { + for (var wallet in _myWalletProvider.listWallets) { + if (walletName.text == wallet.name!) { + canValidateNameBool = false; + break; + } + canValidateNameBool = true; + } + } else { + canValidateNameBool = false; + } + notifyListeners(); + return canValidateNameBool; + } + void reloadBuild() { notifyListeners(); } diff --git a/lib/screens/myWallets/wallet_options.dart b/lib/screens/myWallets/wallet_options.dart index fa17571..41f878a 100644 --- a/lib/screens/myWallets/wallet_options.dart +++ b/lib/screens/myWallets/wallet_options.dart @@ -100,7 +100,8 @@ class WalletOptions extends StatelessWidget { Column( crossAxisAlignment: CrossAxisAlignment.center, children: [ - walletName(walletProvider, _walletOptions), + walletName( + context, walletProvider, _walletOptions), SizedBox(height: isTall ? 5 : 0), // SizedBox(height: isTall ? 5 : 0), balance( @@ -246,7 +247,7 @@ class WalletOptions extends StatelessWidget { ); } - Widget walletName(WalletOptionsProvider walletProvider, + Widget walletName(BuildContext context, WalletOptionsProvider walletProvider, WalletOptionsProvider _walletOptions) { WidgetsBinding.instance.addPostFrameCallback((_) { _walletOptions.nameController.text = wallet.name!; @@ -284,7 +285,8 @@ class WalletOptions extends StatelessWidget { key: const Key('renameWallet'), onTap: () async { // _isNewNameValid = - walletProvider.editWalletName(wallet.id(), isCesium: false); + // walletProvider.editWalletName(wallet.id(), isCesium: false); + await walletProvider.editWalletName(context, wallet.id()); await Future.delayed(const Duration(milliseconds: 30)); walletProvider.walletNameFocus.requestFocus(); },