From 1936cbb6ad8615f92095ef0ad9e732f7b22452ac Mon Sep 17 00:00:00 2001 From: Salvatore Giordano Date: Mon, 9 Mar 2020 00:07:28 +0100 Subject: [PATCH] add comparator --- CHANGELOG.md | 4 ++++ example/lib/main.dart | 1 + lib/country_code_picker.dart | 8 ++++++++ pubspec.yaml | 2 +- 4 files changed, 14 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 165da55..2640741 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 1.3.5 + +- Add `comparator` property + ## 1.3.4 - Add `showFlagDialog` and `showFlagMain` diff --git a/example/lib/main.dart b/example/lib/main.dart index 791615f..6e628a2 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -44,6 +44,7 @@ class _MyAppState extends State { favorite: ['+39', 'FR'], showFlag: false, showFlagDialog: true, + comparator: (a, b) => b.name.compareTo(a.name), //Get the country information relevant to the initial selection onInit: (code) => print("${code.name} ${code.dialCode}"), ), diff --git a/lib/country_code_picker.dart b/lib/country_code_picker.dart index 2eee8f5..a7b63f1 100644 --- a/lib/country_code_picker.dart +++ b/lib/country_code_picker.dart @@ -45,6 +45,9 @@ class CountryCodePicker extends StatefulWidget { /// Width of the flag images final double flagWidth; + /// Use this property to change the order of the options + final Comparator comparator; + CountryCodePicker({ this.onChanged, this.onInit, @@ -66,6 +69,7 @@ class CountryCodePicker extends StatefulWidget { this.flagWidth = 32.0, this.enabled = true, this.textOverflow = TextOverflow.ellipsis, + this.comparator, }); @override @@ -75,6 +79,10 @@ class CountryCodePicker extends StatefulWidget { List elements = jsonList.map((json) => CountryCode.fromJson(json)).toList(); + if (comparator != null) { + elements.sort(comparator); + } + if (countryFilter.length > 0) { elements = elements.where((c) => countryFilter.contains(c.code)).toList(); } diff --git a/pubspec.yaml b/pubspec.yaml index fa47aaf..a0c7b03 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: country_code_picker description: A flutter package for showing a country code selector. In addition it gives the possibility to select a list of favorites countries, as well as to search using a simple searchbox -version: 1.3.4 +version: 1.3.5 homepage: https://github.com/Salvatore-Giordano/CountryCodePicker environment: