Skip to content

Commit

Permalink
borderBuilder type changes and service update
Browse files Browse the repository at this point in the history
  • Loading branch information
ThickLine committed May 16, 2024
1 parent 4eeb84b commit 39dbc4f
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 60 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
14 changes: 8 additions & 6 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ class _HomePageState extends State<HomePage> {
@override
void initState() {
super.initState();
selectedCountry = PickCountryLookupService().getCountryByIsoCode('LV');
selectedCountry = PickCountryLookupService(excludedCountryCodes: ["AX"])
.getCountryByCountryCode("358");
}

void _showCountryPicker() {
Expand All @@ -39,7 +40,7 @@ class _HomePageState extends State<HomePage> {
hideSearch: true,
backButton: Container(),
selectedCountryIsoCode: selectedCountry?.iso2Code,
excludedCountryCodes: ['AX', 'US', 'MX'],
excludedCountryCodes: ["AX"],
title: 'Select your country',
searchField: Row(
mainAxisAlignment: MainAxisAlignment.center,
Expand Down Expand Up @@ -74,12 +75,13 @@ class _HomePageState extends State<HomePage> {
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],
Expand Down
5 changes: 4 additions & 1 deletion lib/src/services/pick_country_lookup_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import 'package:pick_country_picker/src/data/country_codes.dart';

class PickCountryLookupService {
final List<Country> _allCountries;
final List<String>? overrideCountryCodes;

PickCountryLookupService({List<String>? excludedCountryCodes})

PickCountryLookupService(
{this.overrideCountryCodes, List<String>? excludedCountryCodes})
: _allCountries = countryCodes
.map((code) => Country.fromJson(code))
.where((country) =>
Expand Down
46 changes: 21 additions & 25 deletions lib/src/widgets/country_list_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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),
);
},
);
Expand Down
64 changes: 37 additions & 27 deletions lib/src/widgets/country_picker_modal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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

Expand Down

0 comments on commit 39dbc4f

Please sign in to comment.