From 5847a927141f9602e56d8001da435150bda67e2d Mon Sep 17 00:00:00 2001 From: cialloo Date: Mon, 22 Jul 2024 20:36:36 +0800 Subject: [PATCH] feat: no pop when deleting the config --- lib/control_page.dart | 239 ++++++++++++++++++++++-------------------- 1 file changed, 124 insertions(+), 115 deletions(-) diff --git a/lib/control_page.dart b/lib/control_page.dart index a0970e4..631873f 100644 --- a/lib/control_page.dart +++ b/lib/control_page.dart @@ -302,130 +302,139 @@ class _ControlPageState extends State { ), TextButton( onPressed: () { - SavedConnection? selectedSavedConnection; showDialog( context: context, - builder: (BuildContext context) => Dialog( - child: Padding( - padding: const EdgeInsets.all(8.0), - child: Column( - mainAxisSize: MainAxisSize.min, - mainAxisAlignment: MainAxisAlignment.center, - children: [ - Builder(builder: (context) { - String jsonString = - SharedPrefSingleton().savedConnection; - List nodeList = jsonDecode(jsonString); - List nodes = nodeList - .cast>() - .map((nodeData) => - SavedConnection.fromJson(nodeData)) - .toList(); + builder: (BuildContext context) => StatefulBuilder( + builder: + (BuildContext context, StateSetter setState) { + SavedConnection? selectedSavedConnection; + return Dialog( + child: Padding( + padding: const EdgeInsets.all(8.0), + child: Column( + mainAxisSize: MainAxisSize.min, + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Builder(builder: (context) { + String jsonString = + SharedPrefSingleton().savedConnection; + List nodeList = + jsonDecode(jsonString); + List nodes = nodeList + .cast>() + .map((nodeData) => + SavedConnection.fromJson(nodeData)) + .toList(); - return StatefulBuilder(builder: - (BuildContext context, - StateSetter setState) { - return DropdownButton( - value: selectedSavedConnection, - items: nodes.map< - DropdownMenuItem< - SavedConnection>>( - (SavedConnection value) { - return DropdownMenuItem< - SavedConnection>( - value: value, - child: Text(value.name), - ); - }).toList(), - onChanged: (value) { - setState( - () { - selectedSavedConnection = value!; + return StatefulBuilder(builder: + (BuildContext context, + StateSetter setState) { + return DropdownButton( + value: selectedSavedConnection, + items: nodes.map< + DropdownMenuItem< + SavedConnection>>( + (SavedConnection value) { + return DropdownMenuItem< + SavedConnection>( + value: value, + child: Text(value.name), + ); + }).toList(), + onChanged: (value) { + setState( + () { + selectedSavedConnection = value!; + }, + ); + }); + }); + }), + const SizedBox(height: 15), + Row( + mainAxisSize: MainAxisSize.min, + mainAxisAlignment: MainAxisAlignment.center, + children: [ + TextButton( + onPressed: () { + if (selectedSavedConnection != null) { + _supernodeController.text = + selectedSavedConnection! + .supernode; + _communityController.text = + selectedSavedConnection! + .community; + _keyController.text = + selectedSavedConnection! + .communityKey; + _selfAddressController.text = + selectedSavedConnection! + .selfAddress; + } + + Navigator.pop(context); + return; + }, + child: Text( + AppLocalizations.of(context)!.use), + ), + TextButton( + onPressed: () { + if (selectedSavedConnection == null) { + Navigator.pop(context); + return; + } + + String jsonString = + SharedPrefSingleton() + .savedConnection; + List nodeList = + jsonDecode(jsonString); + List nodes = nodeList + .cast>() + .map((nodeData) => + SavedConnection.fromJson( + nodeData)) + .toList(); + + nodes.removeWhere( + (element) { + return element.name == + selectedSavedConnection!.name; }, ); - }); - }); - }), - const SizedBox(height: 15), - Row( - mainAxisSize: MainAxisSize.min, - mainAxisAlignment: MainAxisAlignment.center, - children: [ - TextButton( - onPressed: () { - if (selectedSavedConnection != null) { - _supernodeController.text = - selectedSavedConnection!.supernode; - _communityController.text = - selectedSavedConnection!.community; - _keyController.text = - selectedSavedConnection! - .communityKey; - _selfAddressController.text = - selectedSavedConnection! - .selfAddress; - } - Navigator.pop(context); - return; - }, - child: - Text(AppLocalizations.of(context)!.use), - ), - TextButton( - onPressed: () { - if (selectedSavedConnection == null) { + List> nodeMaps = + nodes + .map((node) => node.toJson()) + .toList(); + SharedPrefSingleton() + .setSavedConnection( + jsonEncode(nodeMaps)) + .then((onValue) { + setState(() {}); + // Navigator.pop(context); + return; + }); + }, + child: Text( + AppLocalizations.of(context)!.delete), + ), + TextButton( + onPressed: () { Navigator.pop(context); return; - } - - String jsonString = - SharedPrefSingleton().savedConnection; - List nodeList = - jsonDecode(jsonString); - List nodes = nodeList - .cast>() - .map((nodeData) => - SavedConnection.fromJson( - nodeData)) - .toList(); - - nodes.removeWhere( - (element) { - return element.name == - selectedSavedConnection!.name; - }, - ); - - List> nodeMaps = - nodes - .map((node) => node.toJson()) - .toList(); - SharedPrefSingleton() - .setSavedConnection( - jsonEncode(nodeMaps)) - .then((onValue) { - Navigator.pop(context); - return; - }); - }, - child: Text( - AppLocalizations.of(context)!.delete), - ), - TextButton( - onPressed: () { - Navigator.pop(context); - return; - }, - child: Text( - AppLocalizations.of(context)!.cancel), - ), - ], - ), - ], + }, + child: Text( + AppLocalizations.of(context)!.cancel), + ), + ], + ), + ], + ), ), - ), - ), + ); + }), ); }, child: Text(AppLocalizations.of(context)!.useConfig)),