From f037ff5560b891d5a027914d842e01bc31203470 Mon Sep 17 00:00:00 2001 From: Joachim Schmidt Date: Fri, 18 Jun 2021 09:34:09 +0200 Subject: [PATCH 1/4] added additional parameter to use default mobile behavior on non mobile platforms --- lib/country_code_picker.dart | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/country_code_picker.dart b/lib/country_code_picker.dart index 1ba6ded8..5589db14 100644 --- a/lib/country_code_picker.dart +++ b/lib/country_code_picker.dart @@ -80,6 +80,9 @@ class CountryCodePicker extends StatefulWidget { /// [BoxDecoration] for the flag image final Decoration? flagDecoration; + /// Set to false if you want to use showMaterialModalBottomSheet instead of showDialog in platforms other than mobile + final bool useDifferentBehaviorForNonMobilePlatforms; + CountryCodePicker({ this.onChanged, this.onInit, @@ -113,6 +116,7 @@ class CountryCodePicker extends StatefulWidget { this.dialogSize, this.dialogBackgroundColor, this.closeIcon = const Icon(Icons.close), + this.useDifferentBehaviorForNonMobilePlatforms = true, Key? key, }) : super(key: key); @@ -278,7 +282,8 @@ class CountryCodePickerState extends State { } void showCountryCodePickerDialog() { - if (!UniversalPlatform.isAndroid && !UniversalPlatform.isIOS) { + if (widget.useDifferentBehaviorForNonMobilePlatforms && + !(UniversalPlatform.isAndroid || UniversalPlatform.isIOS)) { showDialog( barrierColor: widget.barrierColor ?? Colors.grey.withOpacity(0.5), // backgroundColor: widget.backgroundColor ?? Colors.transparent, From 9724b601e9e26fc9cf60e40df7b6ce9ee7ab5c3b Mon Sep 17 00:00:00 2001 From: Joachim Schmidt Date: Mon, 21 Jun 2021 08:27:47 +0200 Subject: [PATCH 2/4] renamed parameter --- lib/country_code_picker.dart | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/country_code_picker.dart b/lib/country_code_picker.dart index 5589db14..8302a7f3 100644 --- a/lib/country_code_picker.dart +++ b/lib/country_code_picker.dart @@ -80,8 +80,8 @@ class CountryCodePicker extends StatefulWidget { /// [BoxDecoration] for the flag image final Decoration? flagDecoration; - /// Set to false if you want to use showMaterialModalBottomSheet instead of showDialog in platforms other than mobile - final bool useDifferentBehaviorForNonMobilePlatforms; + /// Set to true if you want to use showMaterialModalBottomSheet instead of showDialog in platforms other than mobile + final bool alwaysShowAsSheet; CountryCodePicker({ this.onChanged, @@ -116,7 +116,7 @@ class CountryCodePicker extends StatefulWidget { this.dialogSize, this.dialogBackgroundColor, this.closeIcon = const Icon(Icons.close), - this.useDifferentBehaviorForNonMobilePlatforms = true, + this.alwaysShowAsSheet = false, Key? key, }) : super(key: key); @@ -282,8 +282,9 @@ class CountryCodePickerState extends State { } void showCountryCodePickerDialog() { - if (widget.useDifferentBehaviorForNonMobilePlatforms && - !(UniversalPlatform.isAndroid || UniversalPlatform.isIOS)) { + if (!widget.alwaysShowAsSheet && + !UniversalPlatform.isAndroid && + !UniversalPlatform.isIOS) { showDialog( barrierColor: widget.barrierColor ?? Colors.grey.withOpacity(0.5), // backgroundColor: widget.backgroundColor ?? Colors.transparent, From 20fadc600b9b6504a63803a34c65a287792dd552 Mon Sep 17 00:00:00 2001 From: Joachim Schmidt Date: Mon, 21 Jun 2021 08:41:50 +0200 Subject: [PATCH 3/4] fix after merge --- .github/no-response.yml | 15 +++++++++++++++ CHANGELOG.md | 6 ++++++ example/pubspec.yaml | 1 + lib/country_code_picker.dart | 5 +++++ lib/country_codes.dart | 4 ++-- lib/country_localizations.dart | 1 - lib/i18n/en.json | 3 ++- lib/i18n/it.json | 3 ++- lib/selection_dialog.dart | 4 +++- pubspec.yaml | 4 ++-- 10 files changed, 38 insertions(+), 8 deletions(-) create mode 100644 .github/no-response.yml diff --git a/.github/no-response.yml b/.github/no-response.yml new file mode 100644 index 00000000..47f66161 --- /dev/null +++ b/.github/no-response.yml @@ -0,0 +1,15 @@ +# Configuration for probot-no-response - https://github.com/probot/no-response + +# Number of days of inactivity before an issue is closed for lack of response. +daysUntilClose: 7 + +# Label requiring a response. +responseRequiredLabel: "waiting for response" + +# Comment to post when closing an Issue for lack of response. Set to `false` to disable +closeComment: > + Without additional information, I'm unfortunately not sure how to + resolve this issue. I'm therefore reluctantly going to close this + bug for now. Please don't hesitate to comment on the bug if you have + any more information for us; I will reopen it right away! + Thanks for your contribution. \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 48743f6d..6a0299a5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## 2.0.2 + +- added localization for no_country text in italian and english (please open a pr with other languages if you know them ๐Ÿ™) +- added possibility to inject a custom list of countries using `CountryCodePicker.countryList` +- minor fixes + ## 2.0.1 - Use `universal_platform` package diff --git a/example/pubspec.yaml b/example/pubspec.yaml index 99d4f1b4..d96115ce 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -8,6 +8,7 @@ description: Demonstrates how to use the hello plugin. # build by specifying --build-name and --build-number, respectively. # Read more about versioning at semver.org. version: 1.0.0+1 +publish_to: none environment: sdk: ">=2.0.0-dev.68.0 <3.0.0" diff --git a/lib/country_code_picker.dart b/lib/country_code_picker.dart index 8302a7f3..27e45b6f 100644 --- a/lib/country_code_picker.dart +++ b/lib/country_code_picker.dart @@ -83,6 +83,10 @@ class CountryCodePicker extends StatefulWidget { /// Set to true if you want to use showMaterialModalBottomSheet instead of showDialog in platforms other than mobile final bool alwaysShowAsSheet; + /// An optional argument for injecting a list of countries + /// with customized codes. + final List> countryList; + CountryCodePicker({ this.onChanged, this.onInit, @@ -117,6 +121,7 @@ class CountryCodePicker extends StatefulWidget { this.dialogBackgroundColor, this.closeIcon = const Icon(Icons.close), this.alwaysShowAsSheet = false, + this.countryList = codes, Key? key, }) : super(key: key); diff --git a/lib/country_codes.dart b/lib/country_codes.dart index 993be09c..7e7c0823 100644 --- a/lib/country_codes.dart +++ b/lib/country_codes.dart @@ -1,4 +1,4 @@ -List> codes = [ +const List> codes = [ { "name": "ุงูุบุงู†ุณุชุงู†", "code": "AF", @@ -770,7 +770,7 @@ List> codes = [ "dial_code": "+674", }, { - "name": "เคจเคชเคฒ", + "name": "เคจเฅ‡เคชเคพเคฒ", "code": "NP", "dial_code": "+977", }, diff --git a/lib/country_localizations.dart b/lib/country_localizations.dart index bfa8032c..3020f78e 100644 --- a/lib/country_localizations.dart +++ b/lib/country_localizations.dart @@ -21,7 +21,6 @@ class CountryLocalizations { late Map _localizedStrings; Future load() async { - print('locale.languageCode: ${locale.languageCode}'); String jsonString = await rootBundle.loadString( 'packages/country_code_picker/i18n/${locale.languageCode}.json'); Map jsonMap = json.decode(jsonString); diff --git a/lib/i18n/en.json b/lib/i18n/en.json index db1d5294..68e89d86 100644 --- a/lib/i18n/en.json +++ b/lib/i18n/en.json @@ -264,5 +264,6 @@ "RS": "Serbia", "SX": "Sint Maarten (Dutch part)", "SS": "South Sudan", - "XK": "Kosovo" + "XK": "Kosovo", + "no_country": "No country found" } \ No newline at end of file diff --git a/lib/i18n/it.json b/lib/i18n/it.json index b229392c..b7ce5f00 100644 --- a/lib/i18n/it.json +++ b/lib/i18n/it.json @@ -248,5 +248,6 @@ "ZA": "Sudafrica", "ZM": "Zambia", "ZW": "Zimbabwe", - "XK": "Kosovo" + "XK": "Kosovo", + "no_country": "Nessun paese trovato" } \ No newline at end of file diff --git a/lib/selection_dialog.dart b/lib/selection_dialog.dart index 34f3168d..176d0ee2 100644 --- a/lib/selection_dialog.dart +++ b/lib/selection_dialog.dart @@ -1,4 +1,5 @@ import 'package:country_code_picker/country_code.dart'; +import 'package:country_code_picker/country_localizations.dart'; import 'package:flutter/material.dart'; /// selection dialog used for selection of the country code @@ -176,7 +177,8 @@ class _SelectionDialogState extends State { } return Center( - child: Text('No country found'), + child: Text(CountryLocalizations.of(context)?.translate('no_country') ?? + 'No country found'), ); } diff --git a/pubspec.yaml b/pubspec.yaml index e6c4692e..b0c5ab80 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: 2.0.1 +version: 2.0.2 homepage: https://github.com/imtoori/CountryCodePicker environment: @@ -12,7 +12,7 @@ dependencies: modal_bottom_sheet: ^2.0.0 collection: ^1.15.0 - universal_platform: ^1.0.0-nullsafety + universal_platform: ^1.0.0+1 flutter: assets: From d8560c6e3e2ac22f8020c5dbf22889bdb72e0b83 Mon Sep 17 00:00:00 2001 From: Joachim Schmidt Date: Fri, 9 Jul 2021 13:59:35 +0200 Subject: [PATCH 4/4] performance improvements --- lib/selection_dialog.dart | 157 +++++++++++++++++++------------------- 1 file changed, 80 insertions(+), 77 deletions(-) diff --git a/lib/selection_dialog.dart b/lib/selection_dialog.dart index 176d0ee2..ad64bfaa 100644 --- a/lib/selection_dialog.dart +++ b/lib/selection_dialog.dart @@ -58,90 +58,95 @@ class _SelectionDialogState extends State { /// this is useful for filtering purpose late List filteredElements; + int get _itemCount => + (widget.favoriteElements.length > 0 ? 1 : 0) + filteredElements.length; + + bool get _displayFavoriteElements => widget.favoriteElements.length > 0; + @override - Widget build(BuildContext context) => Padding( - padding: const EdgeInsets.all(0.0), - child: Container( - clipBehavior: Clip.hardEdge, - width: widget.size?.width ?? MediaQuery.of(context).size.width, - height: - widget.size?.height ?? MediaQuery.of(context).size.height * 0.85, - decoration: widget.boxDecoration ?? - BoxDecoration( - color: widget.backgroundColor ?? Colors.white, - borderRadius: BorderRadius.all(Radius.circular(8.0)), - boxShadow: [ - BoxShadow( - color: widget.barrierColor ?? Colors.grey.withOpacity(1), - spreadRadius: 5, - blurRadius: 7, - offset: Offset(0, 3), // changes position of shadow - ), - ], - ), - child: Column( - mainAxisSize: MainAxisSize.min, - crossAxisAlignment: CrossAxisAlignment.end, - children: [ - IconButton( - padding: const EdgeInsets.all(0), - iconSize: 20, - icon: widget.closeIcon!, - onPressed: () => Navigator.pop(context), + Widget build(BuildContext context) { + return Container( + clipBehavior: Clip.hardEdge, + width: widget.size?.width ?? MediaQuery.of(context).size.width, + height: widget.size?.height ?? MediaQuery.of(context).size.height * 0.85, + decoration: widget.boxDecoration ?? + BoxDecoration( + color: widget.backgroundColor ?? Colors.white, + borderRadius: BorderRadius.all(Radius.circular(8.0)), + boxShadow: [ + BoxShadow( + color: widget.barrierColor ?? Colors.grey.withOpacity(1), + spreadRadius: 5, + blurRadius: 7, + offset: Offset(0, 3), // changes position of shadow + ) + ], + ), + child: Column( + mainAxisSize: MainAxisSize.min, + crossAxisAlignment: CrossAxisAlignment.end, + children: [ + IconButton( + padding: EdgeInsets.zero, + iconSize: 20, + icon: widget.closeIcon!, + onPressed: () => Navigator.pop(context), + ), + if (!widget.hideSearch) + Padding( + padding: const EdgeInsets.symmetric(horizontal: 24), + child: TextField( + style: widget.searchStyle, + decoration: widget.searchDecoration, + onChanged: _filterElements, ), - if (!widget.hideSearch) - Padding( - padding: const EdgeInsets.symmetric(horizontal: 24), - child: TextField( - style: widget.searchStyle, - decoration: widget.searchDecoration, - onChanged: _filterElements, - ), - ), - Expanded( - child: ListView( - children: [ - widget.favoriteElements.isEmpty - ? const DecoratedBox(decoration: BoxDecoration()) - : Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - ...widget.favoriteElements.map( - (f) => SimpleDialogOption( - child: _buildOption(f), - onPressed: () { - _selectItem(f); - }, - ), - ), - const Divider(), - ], + ), + Expanded( + child: ListView.builder( + itemCount: _itemCount, + itemBuilder: (BuildContext context, int index) { + if (index == 0) { + if (_displayFavoriteElements || + filteredElements.length == 0) { + return Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + if (_displayFavoriteElements) ...[ + ...widget.favoriteElements.map( + (favoriteItem) => SimpleDialogOption( + child: _buildOption(favoriteItem), + onPressed: () => _selectItem(favoriteItem), + ), ), - if (filteredElements.isEmpty) - _buildEmptySearchWidget(context) - else - ...filteredElements.map( - (e) => SimpleDialogOption( - child: _buildOption(e), - onPressed: () { - _selectItem(e); - }, - ), - ), - ], - ), - ), - ], + const Divider(), + ], + if (filteredElements.length == 0) + _buildEmptySearchWidget(context) + ], + ); + } else { + return SizedBox.shrink(); + } + } + final filteredItem = filteredElements[index - 1]; + return SimpleDialogOption( + child: _buildOption(filteredItem), + onPressed: () => _selectItem(filteredItem), + ); + }, + ), ), - ), - ); + ], + ), + ); + } Widget _buildOption(CountryCode e) { return Container( width: 400, child: Flex( direction: Axis.horizontal, - children: [ + children: [ if (widget.showFlag!) Flexible( child: Container( @@ -200,7 +205,5 @@ class _SelectionDialogState extends State { }); } - void _selectItem(CountryCode e) { - Navigator.pop(context, e); - } + void _selectItem(CountryCode e) => Navigator.pop(context, e); }