From c07cc9049b7975e75a5664f81198ad9d6d62d33b Mon Sep 17 00:00:00 2001 From: poka Date: Thu, 14 Jul 2022 18:18:19 +0200 Subject: [PATCH] workaround dropdown --- lib/providers/substrate_sdk.dart | 39 ++++++++++--- lib/screens/settings.dart | 97 ++++++++++++++++---------------- pubspec.lock | 7 +++ pubspec.yaml | 1 + 4 files changed, 87 insertions(+), 57 deletions(-) diff --git a/lib/providers/substrate_sdk.dart b/lib/providers/substrate_sdk.dart index 5de4a37..46abbf2 100644 --- a/lib/providers/substrate_sdk.dart +++ b/lib/providers/substrate_sdk.dart @@ -46,7 +46,6 @@ class SubstrateSdk with ChangeNotifier { } Future connectNode(BuildContext ctx) async { - List node = []; HomeProvider _homeProvider = Provider.of(ctx, listen: false); // var connectivityResult = await (Connectivity().checkConnectivity()); @@ -57,13 +56,14 @@ class SubstrateSdk with ChangeNotifier { // } _homeProvider.changeMessage("connectionPending".tr(), 0); - for (String _endpoint in configBox.get('endpoint')) { - final n = NetworkParams(); - n.name = currencyName; - n.endpoint = _endpoint; - n.ss58 = ss58; - node.add(n); - } + // configBox.delete('customEndpoint'); + final List listEndpoints = + configBox.containsKey('customEndpoint') + ? [getDuniterCustomEndpoint()] + : getDuniterBootstrap(); + + // final nodes = getDuniterBootstrap(); + int timeout = 10000; // if (n.endpoint!.startsWith('ws://')) { @@ -93,7 +93,7 @@ class SubstrateSdk with ChangeNotifier { isLoadingEndpoint = true; notifyListeners(); - final res = await sdk.api.connectNode(keyring, node).timeout( + final res = await sdk.api.connectNode(keyring, listEndpoints).timeout( Duration(milliseconds: timeout), onTimeout: () => null, ); @@ -133,6 +133,27 @@ class SubstrateSdk with ChangeNotifier { log.d(sdk.api.connectedNode?.endpoint); } + List getDuniterBootstrap() { + List node = []; + + for (String _endpoint in configBox.get('endpoint')) { + final n = NetworkParams(); + n.name = currencyName; + n.endpoint = _endpoint; + n.ss58 = ss58; + node.add(n); + } + return node; + } + + NetworkParams getDuniterCustomEndpoint() { + final nodeParams = NetworkParams(); + nodeParams.name = currencyName; + nodeParams.endpoint = configBox.get('customEndpoint'); + nodeParams.ss58 = ss58; + return nodeParams; + } + Future importAccount( {String mnemonic = '', bool fromMnemonic = false, diff --git a/lib/screens/settings.dart b/lib/screens/settings.dart index 5fd92cb..9c234ee 100644 --- a/lib/screens/settings.dart +++ b/lib/screens/settings.dart @@ -5,7 +5,9 @@ import 'package:gecko/providers/my_wallets.dart'; import 'package:gecko/providers/settings_provider.dart'; import 'package:gecko/providers/substrate_sdk.dart'; import 'package:gecko/globals.dart'; +import 'package:polkawallet_sdk/api/types/networkParams.dart'; import 'package:provider/provider.dart'; +import 'package:dropdown_button2/dropdown_button2.dart'; // ignore: must_be_immutable class SettingsScreen extends StatelessWidget { @@ -14,29 +16,21 @@ class SettingsScreen extends StatelessWidget { SettingsScreen({Key? key}) : super(key: key); // Initial Selected Value - String dropdownvalue = 'Item 1'; - - // List of items in our dropdown menu - var items = [ - 'Item 1', - 'Item 2', - 'Item 3', - 'Item 4', - 'Item 5', - ]; + String? selectedDuniterEndpoint; @override Widget build(BuildContext context) { SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]); SubstrateSdk _sub = Provider.of(context, listen: false); - TextEditingController _endpointController = TextEditingController( - text: _sub.sdk.api.connectedNode?.endpoint ?? - configBox.get('endpoint').first); const double buttonHigh = 50; const double buttonWidth = 240; const double fontSize = 16; + // List of items in our dropdown menu + var duniterBootstrapNodes = _sub.getDuniterBootstrap(); + selectedDuniterEndpoint = _sub.getConnectedEndpoint(); + return Scaffold( backgroundColor: backgroundColor, appBar: AppBar( @@ -47,23 +41,7 @@ class SettingsScreen extends StatelessWidget { )), body: Column(children: [ const SizedBox(height: 60), - Consumer(builder: (context, _set, _) { - return DropdownButton( - value: dropdownvalue, - icon: const Icon(Icons.keyboard_arrow_down), - items: items.map((String items) { - return DropdownMenuItem( - value: items, - child: Text(items), - ); - }).toList(), - onChanged: (String? newValue) { - log.d('coucoucoucouc'); - dropdownvalue = newValue!; - _set.reload(); - }, - ); - }), + Row(children: [ Consumer(builder: (context, _sub, _) { log.d(_sub.sdk.api.connectedNode?.endpoint); @@ -76,27 +54,50 @@ class SettingsScreen extends StatelessWidget { ? Icons.check : Icons.close), const Spacer(), - SizedBox( - width: 200, - height: 50, - child: TextField( - controller: _endpointController, - autocorrect: false, - ), - ), + Consumer(builder: (context, _set, _) { + return DropdownButtonHideUnderline( + child: DropdownButton( + //TODO + value: selectedDuniterEndpoint ?? + duniterBootstrapNodes.first.endpoint, + icon: const Icon(Icons.keyboard_arrow_down), + items: duniterBootstrapNodes + .map((NetworkParams _endpointParams) { + return DropdownMenuItem( + value: _endpointParams.endpoint, + child: Text(_endpointParams.endpoint!), + ); + }).toList(), + onChanged: (String? _newEndpoint) { + log.d(_newEndpoint!); + selectedDuniterEndpoint = _newEndpoint; + _set.reload(); + }, + ), + ); + }), const Spacer(flex: 5), _sub.isLoadingEndpoint ? CircularProgressIndicator(color: orangeC) - : IconButton( - icon: Icon( - Icons.send, - color: orangeC, - size: 40, - ), - onPressed: () async { - configBox.put('endpoint', [_endpointController.text]); - await _sub.connectNode(context); - }), + : Consumer(builder: (context, _set, _) { + return IconButton( + icon: Icon( + Icons.send, + color: selectedDuniterEndpoint != + _sub.getConnectedEndpoint() + ? orangeC + : Colors.grey[500], + size: 40, + ), + onPressed: selectedDuniterEndpoint != + _sub.getConnectedEndpoint() + ? () async { + configBox.put('customEndpoint', + selectedDuniterEndpoint); + await _sub.connectNode(context); + } + : null); + }), const Spacer(flex: 8), ]), ); diff --git a/pubspec.lock b/pubspec.lock index a8e8ef7..6eb5e7f 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -309,6 +309,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.1.0" + dropdown_button2: + dependency: "direct main" + description: + name: dropdown_button2 + url: "https://pub.dartlang.org" + source: hosted + version: "1.6.3" durt: dependency: "direct main" description: diff --git a/pubspec.yaml b/pubspec.yaml index c926352..99db576 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -74,6 +74,7 @@ dependencies: image_cropper: ^2.0.3 easy_localization: ^3.0.1 flutter_markdown: ^0.6.10+2 + dropdown_button2: ^1.6.3 dev_dependencies: # flutter_launcher_icons: ^0.9.2