Skip to content

Commit

Permalink
Merge pull request #2 from hkk97/add-feature-ref-country-code
Browse files Browse the repository at this point in the history
Add feature ref country code
  • Loading branch information
hkk97 committed Nov 12, 2022
2 parents 4f60721 + 7ce8c43 commit d0ed130
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 5 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,11 @@
## 0.0.7

* Update README.md and Solve misspelling

## 0.0.8

* Add refCountryCode and Update README.md

## 0.0.9

* Fix refCountryCode issues
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ showCountrySelectorBottomSheet(

| Parameter | Description |
|---|---|
| `refCountryCode` | Sets the default selected country |
| `customAppBar` | Sets the Custom AppBar instead of using provided default AppBar |
| `bottomAppBarHeight` | Sets the height for the bottom `Continue Section` widget |
| `continueBtnPadding` | The Padding between the bottom `Continue Section` and `Continue Button` widget |
Expand Down Expand Up @@ -89,11 +90,11 @@ showCountrySelectorBottomSheet(

## Demonstration of CountrySelectorWidget - Flutter Web

!["CountrySelectorWidget - SelectedLocale.zhCH"](example/demo_gifs/countrySelectorWidget_demo.gif?raw=true)
!["CountrySelectorWidget - SelectedLocale.zhCH"](https://github.com/hkk97/country_selector_widget/blob/master/example/demo_gifs/countrySelectorWidget_demo.gif?raw=true)

## Demonstration of showCountrySelectorBottomSheet - Flutter Web

!["showCountrySelectorBottomSheet - SelectedLocale.en"](example/demo_gifs/showCountrySelectorBottomSheet.gif)
!["showCountrySelectorBottomSheet - SelectedLocale.en"](https://github.com/hkk97/country_selector_widget/blob/master/example/demo_gifs/showCountrySelectorBottomSheet.gif?raw=true)

## Demonstration of CountrySelectorWidget - Android

Expand Down
4 changes: 3 additions & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,10 @@ class App extends StatelessWidget {
],
),
body: CountrySelectorWidget(
// select the Chinese locale to label the text rather than English
// select the Chinese locale to label the text rather than English
selectedLocale: SelectedLocale.zhCH,
// set the default selected country
refCountryCode: "hk",
// callback of the selected country
onSelectedCountry: (country) async {
// dialog to show the info of the country object
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ packages:
path: ".."
relative: true
source: path
version: "0.0.5"
version: "0.0.6"
cupertino_icons:
dependency: "direct main"
description:
Expand Down
26 changes: 26 additions & 0 deletions lib/country_selector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import 'package:country_selector_widget/util/text_util.dart';
// func to show countryselectorbottom sheet
Future<void> showCountrySelectorBottomSheet({
required BuildContext context,
// The refCountryCode used to indicate the default selected country
String? refCountryCode,
// the height of the bottomsheet
double? bottomSheetHeight,
// the radius of the top left and top right corner
Expand Down Expand Up @@ -127,6 +129,8 @@ Future<void> showCountrySelectorBottomSheet({
}

class CountrySelectorWidget extends StatefulWidget {
// The refCountryCode used to indicate the default selected country
final String? refCountryCode;
// Sets the Custom AppBar instead of using provided default AppBar
final PreferredSizeWidget? customAppBar;
// Sets the height for the bottom `Continue Section` widget
Expand Down Expand Up @@ -183,6 +187,7 @@ class CountrySelectorWidget extends StatefulWidget {

const CountrySelectorWidget({
super.key,
this.refCountryCode,
this.customAppBar,
this.bottomAppBarHeight = 75,
this.continueBtnPadding = const EdgeInsets.symmetric(vertical: 13.5),
Expand Down Expand Up @@ -230,6 +235,7 @@ class CountrySelectorWidget extends StatefulWidget {
class CountrySelectorWidgetState extends State<CountrySelectorWidget> {
late ScrollController _scrollController;
late List<Country> _countries;
late List<double> _widgetHeight;
late ValueNotifier<List<Country>?> _countriesNotifi;
late ValueNotifier<Country?> _selectedCountryNotifi;
late TextUtil _textUtil;
Expand All @@ -241,19 +247,39 @@ class CountrySelectorWidgetState extends State<CountrySelectorWidget> {
super.initState();
_textUtil = TextUtil(selectedLocale: widget.selectedLocale);
_scrollController = ScrollController();
_widgetHeight = [];
_selectedCountryNotifi = ValueNotifier(null);
_countries = [];
_countriesNotifi = ValueNotifier(null);
for (int i = 0; i < countriesMap.length; i++) {
final country = Country.fromJson(countriesMap[i]);
_countries.add(country);
}
if (widget.refCountryCode != null) {
List<Country> refCountry = _countries
.where(
(country) => country.code == widget.refCountryCode!.toUpperCase())
.toList();
if (refCountry.length == 1) {
_selectedCountryNotifi.value = refCountry.first;
} else {
throw ("refCountry does not valid in CountryCode");
}
}
_countriesNotifi.value = _countries;
_focusNode = FocusNode();
_focusNotifi = ValueNotifier(false);
_focusNode.addListener(() {
_focusNotifi.value = _focusNode.hasFocus;
});
WidgetsBinding.instance.addPostFrameCallback(
(_) {
if (_selectedCountryNotifi.value != null) {
final index = _countries.indexOf(_selectedCountryNotifi.value!);
_scrollController.jumpTo(64.5 * index);
}
},
);
}

@override
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: country_selector_widget
description: Country Selector Widget provides CountrySelectorWidget and showCountrySelectorBottomSheet to fulfill the needed usage.
version: 0.0.7
version: 0.0.9
homepage: https://hkk97.github.io/country_selector_widget/

environment:
Expand Down

0 comments on commit d0ed130

Please sign in to comment.