diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d94457..252cbe7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,17 @@ +## [2.0.1] - 12 Mar 2021 + +* Add `favorite` option. + - Can be used to to show the favorite currencies at the top of the list. + - It takes a list of currency code. + ```Dart + showCurrencyPicker( + context: context, + onSelect: (Currency currency) { + print('Select currency: ${currency.name}'); + }, + favorite: ['EUR', 'GBP', 'USD'], + ); + ``` ## [2.0.0] - 05 Mar 2021 * Migrated to null safety diff --git a/README.md b/README.md index 81c3bd3..819e380 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ A flutter package to select a currency from a list of currencies. Add the package to your pubspec.yaml: ```yaml - currency_picker: ^2.0.0 + currency_picker: ^2.0.1 ``` In your dart file, import the library: @@ -38,7 +38,7 @@ showCurrencyPicker( * `searchHint`: Option to customize hint of the search TextField (optional). * `showCurrencyName`: Option to show/hide the currency name, default value `true` (optional). * `showCurrencyCode`: Option to show/hide the currency code, default value `true` (optional). -* `currencyFilter`: Can be used to uses filter the Currency list (optional). +* `currencyFilter`: Can be used to filter the Currency list (optional). ```Dart showCurrencyPicker( context: context, @@ -48,6 +48,7 @@ showCurrencyPicker( currencyFilter: ['EUR', 'GBP', 'USD', 'AUD', 'CAD', 'JPY', 'HKD', 'CHF', 'SEK', 'ILS'], ); ``` +* `favorite`: Can be used to show the favorite currencies at the top of the list (optional). ## Contributions Contributions of any kind are more than welcome! Feel free to fork and improve currency_picker in any way you want, make a pull request, or open an issue. diff --git a/example/README.md b/example/README.md index 81c3bd3..819e380 100644 --- a/example/README.md +++ b/example/README.md @@ -11,7 +11,7 @@ A flutter package to select a currency from a list of currencies. Add the package to your pubspec.yaml: ```yaml - currency_picker: ^2.0.0 + currency_picker: ^2.0.1 ``` In your dart file, import the library: @@ -38,7 +38,7 @@ showCurrencyPicker( * `searchHint`: Option to customize hint of the search TextField (optional). * `showCurrencyName`: Option to show/hide the currency name, default value `true` (optional). * `showCurrencyCode`: Option to show/hide the currency code, default value `true` (optional). -* `currencyFilter`: Can be used to uses filter the Currency list (optional). +* `currencyFilter`: Can be used to filter the Currency list (optional). ```Dart showCurrencyPicker( context: context, @@ -48,6 +48,7 @@ showCurrencyPicker( currencyFilter: ['EUR', 'GBP', 'USD', 'AUD', 'CAD', 'JPY', 'HKD', 'CHF', 'SEK', 'ILS'], ); ``` +* `favorite`: Can be used to show the favorite currencies at the top of the list (optional). ## Contributions Contributions of any kind are more than welcome! Feel free to fork and improve currency_picker in any way you want, make a pull request, or open an issue. diff --git a/example/lib/main.dart b/example/lib/main.dart index dd3e13e..56b2467 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -35,6 +35,7 @@ class HomePage extends StatelessWidget { onSelect: (Currency currency) { print('Select currency: ${currency.name}'); }, + favorite: ['SEK'], ); }, child: const Text('Show currency picker'), diff --git a/example/pubspec.lock b/example/pubspec.lock index a13a0b6..40bed79 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -49,7 +49,7 @@ packages: path: ".." relative: true source: path - version: "2.0.0" + version: "2.0.1" fake_async: dependency: transitive description: diff --git a/lib/currency_picker.dart b/lib/currency_picker.dart index d7c20b6..7079323 100644 --- a/lib/currency_picker.dart +++ b/lib/currency_picker.dart @@ -17,6 +17,7 @@ export 'package:currency_picker/src/currency_utils.dart'; /// `showCurrencyName`: Option to show/hide the currency name, default value true (optional). /// `showCurrencyCode`: Option to show/hide the currency code, default value true (optional). /// `currencyFilter`: Can be used to uses filter the Currency list (optional). +/// `favorite`: The Currencies that will appear at the top of the list (optional). /// /// This example demonstrates how to use `showCurrencyPicker` /// ```dart @@ -34,6 +35,7 @@ export 'package:currency_picker/src/currency_utils.dart'; void showCurrencyPicker({ required BuildContext context, required ValueChanged onSelect, + List? favorite, List? currencyFilter, String? searchHint, bool showFlag = true, @@ -49,6 +51,7 @@ void showCurrencyPicker({ showFlag: showFlag, showCurrencyName: showCurrencyName, showCurrencyCode: showCurrencyCode, + favorite: favorite, currencyFilter: currencyFilter, ); } diff --git a/lib/src/currency.dart b/lib/src/currency.dart index e6ae81d..8372405 100644 --- a/lib/src/currency.dart +++ b/lib/src/currency.dart @@ -10,13 +10,19 @@ class Currency { ///The currency flag code /// - /// To get flag unicode(Emoji ) use [CurrencyUtils.countryCodeToEmoji] + /// To get flag unicode(Emoji) use [CurrencyUtils.countryCodeToEmoji] final String flag; ///The currency number final String number; - Currency({required this.code, required this.name, required this.symbol, required this.flag, required this.number}); + Currency({ + required this.code, + required this.name, + required this.symbol, + required this.flag, + required this.number, + }); Currency.from({required Map json}) : code = json['code'], diff --git a/lib/src/currency_list_bottom_sheet.dart b/lib/src/currency_list_bottom_sheet.dart index 466e314..ba699a3 100644 --- a/lib/src/currency_list_bottom_sheet.dart +++ b/lib/src/currency_list_bottom_sheet.dart @@ -6,6 +6,7 @@ import 'currency_list_view.dart'; void showCurrencyListBottomSheet({ required BuildContext context, required ValueChanged onSelect, + List? favorite, List? currencyFilter, String? searchHint, bool showFlag = true, @@ -19,6 +20,7 @@ void showCurrencyListBottomSheet({ builder: (_) => _builder( context, onSelect, + favorite, currencyFilter, searchHint, showFlag, @@ -31,6 +33,7 @@ void showCurrencyListBottomSheet({ Widget _builder( BuildContext context, ValueChanged onSelect, + List? favorite, List? currencyFilter, String? searchHint, bool showFlag, @@ -65,6 +68,7 @@ Widget _builder( showFlag: showFlag, showCurrencyName: showCurrencyName, showCurrencyCode: showCurrencyCode, + favorite: favorite, currencyFilter: currencyFilter, ), ); diff --git a/lib/src/currency_list_view.dart b/lib/src/currency_list_view.dart index f6055bb..30c7e3c 100644 --- a/lib/src/currency_list_view.dart +++ b/lib/src/currency_list_view.dart @@ -10,6 +10,11 @@ class CurrencyListView extends StatefulWidget { /// The currency picker passes the new value to the callback. final ValueChanged onSelect; + /// The Currencies that will appear at the top of the list (optional). + /// + /// It takes a list of Currency code. + final List? favorite; + /// Can be used to uses filter the Currency list (optional). /// /// It takes a list of Currency code. @@ -40,6 +45,7 @@ class CurrencyListView extends StatefulWidget { const CurrencyListView({ Key? key, required this.onSelect, + this.favorite, this.currencyFilter, this.searchHint, this.showCurrencyCode = true, @@ -56,6 +62,7 @@ class _CurrencyListViewState extends State { late List _filteredList; late List _currencyList; + List? _favoriteList; TextEditingController? _searchController; @@ -75,6 +82,10 @@ class _CurrencyListViewState extends State { .removeWhere((element) => !currencyFilter.contains(element.code)); } + if (widget.favorite != null) { + _favoriteList = _currencyService.findCurrenciesByCode(widget.favorite!); + } + _filteredList.addAll(_currencyList); super.initState(); } @@ -109,9 +120,20 @@ class _CurrencyListViewState extends State { ), Expanded( child: ListView( - children: _filteredList - .map((currency) => _listRow(currency)) - .toList(), + children: [ + if (_favoriteList != null) ...[ + ..._favoriteList! + .map((currency) => _listRow(currency)) + .toList(), + const Padding( + padding: EdgeInsets.symmetric(horizontal: 20.0), + child: Divider(thickness: 1), + ), + ], + ..._filteredList + .map((currency) => _listRow(currency)) + .toList() + ], ), ), ], diff --git a/lib/src/currency_service.dart b/lib/src/currency_service.dart index bdf39e4..14ffc04 100644 --- a/lib/src/currency_service.dart +++ b/lib/src/currency_service.dart @@ -32,4 +32,18 @@ class CurrencyService { return _currencies .firstWhereOrNull((currency) => currency.number == number); } + + ///Returns a list with all the currencies that mach the given codes list. + List findCurrenciesByCode(List codes) { + final List _codes = + codes.map((code) => code.toUpperCase()).toList(); + final List currencies = []; + for (final code in _codes) { + final Currency? currency = findByCode(code); + if (currency != null) { + currencies.add(currency); + } + } + return currencies; + } } diff --git a/pubspec.lock b/pubspec.lock index 6c6d2a9..a81026f 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -66,7 +66,7 @@ packages: name: lint url: "https://pub.dartlang.org" source: hosted - version: "1.5.2" + version: "1.5.3" matcher: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index c3b0db1..6dbdca2 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -4,7 +4,7 @@ description: A flutter package to select a currency from a list of currencies. homepage: https://github.com/Daniel-Ioannou repository: https://github.com/Daniel-Ioannou/flutter_currency_picker -version: 2.0.0 +version: 2.0.1 environment: sdk: '>=2.12.0 <3.0.0' @@ -18,6 +18,6 @@ dependencies: dev_dependencies: flutter_test: sdk: flutter - lint: ^1.5.2 + lint: ^1.5.3 flutter: