diff --git a/CHANGELOG.md b/CHANGELOG.md index 2516bfb..7a8d21d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,3 +12,9 @@ ## 1.1.0 * Added `excludedCountryCodes` option to exclude specific countries from code execution + +## 1.2.0 +* Breaking Changes + Breaking: `borderBuilder` now accepts a `Widget` input instead of `Border` +* New Features + Added `excludedCountryCodes` from `PickCountryLookupService` diff --git a/example/lib/main.dart b/example/lib/main.dart index 74884ed..7c7e000 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -25,7 +25,8 @@ class _HomePageState extends State { @override void initState() { super.initState(); - selectedCountry = PickCountryLookupService().getCountryByIsoCode('LV'); + selectedCountry = PickCountryLookupService(excludedCountryCodes: ["AX"]) + .getCountryByCountryCode("358"); } void _showCountryPicker() { @@ -39,7 +40,7 @@ class _HomePageState extends State { hideSearch: true, backButton: Container(), selectedCountryIsoCode: selectedCountry?.iso2Code, - excludedCountryCodes: ['AX', 'US', 'MX'], + excludedCountryCodes: ["AX"], title: 'Select your country', searchField: Row( mainAxisAlignment: MainAxisAlignment.center, @@ -74,12 +75,13 @@ class _HomePageState extends State { height: 20, ); }, - borderBuilder: (Country country) { - return Border.all( + borderBuilder: Container( + decoration: BoxDecoration( + border: Border.all( color: Colors.grey[400]!, width: 0.5, - ); - }, + )), + ), // selectedIcon: Icon( // Icons.fingerprint_outlined, // color: Colors.pink[200], diff --git a/lib/src/services/pick_country_lookup_service.dart b/lib/src/services/pick_country_lookup_service.dart index 16d5705..69f8a5f 100644 --- a/lib/src/services/pick_country_lookup_service.dart +++ b/lib/src/services/pick_country_lookup_service.dart @@ -4,8 +4,11 @@ import 'package:pick_country_picker/src/data/country_codes.dart'; class PickCountryLookupService { final List _allCountries; + final List? overrideCountryCodes; - PickCountryLookupService({List? excludedCountryCodes}) + + PickCountryLookupService( + {this.overrideCountryCodes, List? excludedCountryCodes}) : _allCountries = countryCodes .map((code) => Country.fromJson(code)) .where((country) => diff --git a/lib/src/widgets/country_list_widget.dart b/lib/src/widgets/country_list_widget.dart index 6921050..4320dfb 100644 --- a/lib/src/widgets/country_list_widget.dart +++ b/lib/src/widgets/country_list_widget.dart @@ -9,7 +9,7 @@ class CountryListWidget extends StatelessWidget { final Widget? selectedIcon; final String Function(Country country)? countryDisplayBuilder; final String? Function(Country country)? subtitleBuilder; - final Border Function(Country country)? borderBuilder; + final Widget? borderBuilder; const CountryListWidget({ super.key, @@ -25,36 +25,32 @@ class CountryListWidget extends StatelessWidget { @override Widget build(BuildContext context) { - return ListView.builder( + return ListView.separated( itemCount: availableCountries.length, + separatorBuilder: (_, __) => borderBuilder ?? const SizedBox.shrink(), itemBuilder: (context, index) { final country = availableCountries[index]; final isSelected = selectedCountry != null && country.iso2Code == selectedCountry!.iso2Code; - return Container( - decoration: BoxDecoration( - border: borderBuilder != null ? borderBuilder!(country) : null, - ), - child: ListTile( - leading: flagBuilder != null - ? flagBuilder!(country) - : defaultFlagWidget(country), - title: Text(countryDisplayBuilder != null - ? countryDisplayBuilder!(country) - : defaultCountryDisplay(country)), - subtitle: subtitleBuilder != null - ? Text(subtitleBuilder!(country) ?? '') - : null, - trailing: isSelected - ? selectedIcon ?? - const Icon( - Icons.check_circle, - color: Colors.green, - ) - : null, - onTap: () => onCountrySelected(country), - ), + return ListTile( + leading: flagBuilder != null + ? flagBuilder!(country) + : defaultFlagWidget(country), + title: Text(countryDisplayBuilder != null + ? countryDisplayBuilder!(country) + : defaultCountryDisplay(country)), + subtitle: subtitleBuilder != null + ? Text(subtitleBuilder!(country) ?? '') + : null, + trailing: isSelected + ? selectedIcon ?? + const Icon( + Icons.check_circle, + color: Colors.green, + ) + : null, + onTap: () => onCountrySelected(country), ); }, ); diff --git a/lib/src/widgets/country_picker_modal.dart b/lib/src/widgets/country_picker_modal.dart index 92dad13..9e5c6b7 100644 --- a/lib/src/widgets/country_picker_modal.dart +++ b/lib/src/widgets/country_picker_modal.dart @@ -92,33 +92,43 @@ class CountryPickerModal extends StatefulWidget { /// A callback function that allows for customized border rendering for each country /// item in the list. If provided, this function will be used to generate the border /// for each ListTile. - final Border Function(Country country)? borderBuilder; - - const CountryPickerModal( - {super.key, - required this.onCountryChanged, - this.selectedCountry, - this.selectedCountryCode, - this.selectedCountryName, - this.selectedCountryIsoCode, - this.title = 'Select Country', - this.priorityCountryCodes, - this.overrideCountryCodes, - this.excludedCountryCodes, - this.hideSearch = false, - this.hideCloseIcon = false, - this.useCupertinoModal = false, - this.searchField, - this.backButton, - this.countryListItemBuilder, - this.selectedIcon, - this.countryDisplayBuilder, - this.flagBuilder, - this.borderRadius, - this.subtitleBuilder, - this.cancelText = 'Cancel', - this.placeholderText = 'Search for a country', - this.borderBuilder}); + final Widget? borderBuilder; + + CountryPickerModal({ + super.key, + required this.onCountryChanged, + this.selectedCountry, + this.selectedCountryCode, + this.selectedCountryName, + this.selectedCountryIsoCode, + this.title = 'Select Country', + this.priorityCountryCodes, + this.overrideCountryCodes, + this.excludedCountryCodes, + this.hideSearch = false, + this.hideCloseIcon = false, + this.useCupertinoModal = false, + this.searchField, + this.backButton, + this.countryListItemBuilder, + this.selectedIcon, + this.countryDisplayBuilder, + this.flagBuilder, + this.borderRadius, + this.subtitleBuilder, + this.cancelText = 'Cancel', + this.placeholderText = 'Search for a country', + this.borderBuilder, + }) : assert( + overrideCountryCodes == null || + excludedCountryCodes == null || + overrideCountryCodes + .toSet() + .intersection(excludedCountryCodes.toSet()) + .isEmpty, + 'overrideCountryCodes and excludedCountryCodes must not contain any of the same values.', + ); + @override CountryPickerModalState createState() => CountryPickerModalState(); diff --git a/pubspec.yaml b/pubspec.yaml index 3ff0cd2..0178798 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: pick_country_picker description: A highly customizable Flutter package for selecting a country from a modal list. -version: 1.1.0 +version: 1.2.0 homepage: repository: https://github.com/ThickLine/pick_country_picker