Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove unnecessary country reload (since default country is selected) #345

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion lib/src/providers/country_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,30 @@ class CountryProvider {
/// * If [countries] is `null` or empty it returns a list of all [Countries.countryList].
/// * If [countries] is not empty it returns a filtered list containing
/// counties as specified.
static List<Country> getCountriesData({required List<String>? countries}) {
static List<Country> getCountriesData(
{required List<String>? countries, List<String>? prioritizedCountries}) {
List jsonList = Countries.countryList;

if (prioritizedCountries != null) {
if (countries != null) {
List filteredList = jsonList.where((country) {
return countries.contains(country[PropertyName]);
}).toList();
jsonList = filteredList;
}

List customSortList = [];
for (var item in prioritizedCountries) {
customSortList.addAll(jsonList.where((country) {
return item == country[PropertyName];
}).toList());
}
customSortList.addAll(jsonList);
return customSortList
.map((country) => Country.fromJson(country))
.toList();
}

if (countries == null || countries.isEmpty) {
return jsonList.map((country) => Country.fromJson(country)).toList();
}
Expand Down
15 changes: 6 additions & 9 deletions lib/src/widgets/input_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class InternationalPhoneNumberInput extends StatefulWidget {
final Iterable<String>? autofillHints;

final List<String>? countries;
final List<String>? prioritizedCountries;

InternationalPhoneNumberInput(
{Key? key,
Expand Down Expand Up @@ -122,7 +123,8 @@ class InternationalPhoneNumberInput extends StatefulWidget {
this.focusNode,
this.cursorColor,
this.autofillHints,
this.countries})
this.countries,
this.prioritizedCountries})
: super(key: key);

@override
Expand Down Expand Up @@ -162,12 +164,6 @@ class _InputWidgetState extends State<InternationalPhoneNumberInput> {
@override
void didUpdateWidget(InternationalPhoneNumberInput oldWidget) {
loadCountries(previouslySelectedCountry: country);
if (oldWidget.initialValue?.hash != widget.initialValue?.hash) {
if (country!.alpha2Code != widget.initialValue?.isoCode) {
loadCountries();
}
initialiseWidget();
}
super.didUpdateWidget(oldWidget);
}

Expand All @@ -194,8 +190,9 @@ class _InputWidgetState extends State<InternationalPhoneNumberInput> {
/// loads countries from [Countries.countryList] and selected Country
void loadCountries({Country? previouslySelectedCountry}) {
if (this.mounted) {
List<Country> countries =
CountryProvider.getCountriesData(countries: widget.countries);
List<Country> countries = CountryProvider.getCountriesData(
countries: widget.countries,
prioritizedCountries: widget.prioritizedCountries);

Country country = previouslySelectedCountry ??
Utils.getInitialSelectedCountry(
Expand Down