Skip to content

Added dialog mode feature #118

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

Open
wants to merge 5 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
11 changes: 6 additions & 5 deletions example/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
.buildlog/
.history
.svn/
migrate_working_dir/

# IntelliJ related
*.iml
Expand All @@ -22,6 +23,7 @@

# Flutter/Dart/Pub related
**/doc/api/
**/ios/Flutter/.last_build_id
.dart_tool/
.flutter-plugins
.flutter-plugins-dependencies
Expand All @@ -30,14 +32,13 @@
.pub/
/build/

# Web related
lib/generated_plugin_registrant.dart

# Symbolication related
app.*.symbols

# Obfuscation related
app.*.map.json

# Exceptions to above rules.
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages
# Android Studio will place build artifacts here
/android/app/debug
/android/app/profile
/android/app/release
39 changes: 37 additions & 2 deletions example/.metadata
Original file line number Diff line number Diff line change
@@ -1,10 +1,45 @@
# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled and should not be manually edited.
# This file should be version controlled.

version:
revision: 8af6b2f038c1172e61d418869363a28dffec3cb4
revision: 4d9e56e694b656610ab87fcf2efbcd226e0ed8cf
channel: stable

project_type: app

# Tracks metadata for the flutter migrate command
migration:
platforms:
- platform: root
create_revision: 4d9e56e694b656610ab87fcf2efbcd226e0ed8cf
base_revision: 4d9e56e694b656610ab87fcf2efbcd226e0ed8cf
- platform: android
create_revision: 4d9e56e694b656610ab87fcf2efbcd226e0ed8cf
base_revision: 4d9e56e694b656610ab87fcf2efbcd226e0ed8cf
- platform: ios
create_revision: 4d9e56e694b656610ab87fcf2efbcd226e0ed8cf
base_revision: 4d9e56e694b656610ab87fcf2efbcd226e0ed8cf
- platform: linux
create_revision: 4d9e56e694b656610ab87fcf2efbcd226e0ed8cf
base_revision: 4d9e56e694b656610ab87fcf2efbcd226e0ed8cf
- platform: macos
create_revision: 4d9e56e694b656610ab87fcf2efbcd226e0ed8cf
base_revision: 4d9e56e694b656610ab87fcf2efbcd226e0ed8cf
- platform: web
create_revision: 4d9e56e694b656610ab87fcf2efbcd226e0ed8cf
base_revision: 4d9e56e694b656610ab87fcf2efbcd226e0ed8cf
- platform: windows
create_revision: 4d9e56e694b656610ab87fcf2efbcd226e0ed8cf
base_revision: 4d9e56e694b656610ab87fcf2efbcd226e0ed8cf

# User provided section

# List of Local paths (relative to this file) that should be
# ignored by the migrate tool.
#
# Files that are not part of the templates will be ignored by default.
unmanaged_files:
- 'lib/main.dart'
- 'ios/Runner.xcodeproj/project.pbxproj'
6 changes: 6 additions & 0 deletions example/android/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@ gradle-wrapper.jar
/gradlew.bat
/local.properties
GeneratedPluginRegistrant.java

# Remember to never publicly share your keystore.
# See https://flutter.dev/docs/deployment/android#reference-the-keystore-from-the-app
key.properties
**/*.keystore
**/*.jks
2 changes: 2 additions & 0 deletions example/ios/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
**/dgph
*.mode1v3
*.mode2v3
*.moved-aside
Expand All @@ -18,6 +19,7 @@ Flutter/App.framework
Flutter/Flutter.framework
Flutter/Flutter.podspec
Flutter/Generated.xcconfig
Flutter/ephemeral/
Flutter/app.flx
Flutter/app.zip
Flutter/flutter_assets/
Expand Down
127 changes: 90 additions & 37 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,45 +62,98 @@ class HomePage extends StatelessWidget {
return Scaffold(
appBar: AppBar(title: Text('Demo for country picker')),
body: Center(
child: ElevatedButton(
onPressed: () {
showCountryPicker(
context: context,
//Optional. Can be used to exclude(remove) one ore more country from the countries list (optional).
exclude: <String>['KN', 'MF'],
favorite: <String>['SE'],
//Optional. Shows phone code before the country name.
showPhoneCode: true,
onSelect: (Country country) {
print('Select country: ${country.displayName}');
},
// Optional. Sets the theme for the country list picker.
countryListTheme: CountryListThemeData(
// Optional. Sets the border radius for the bottomsheet.
borderRadius: BorderRadius.only(
topLeft: Radius.circular(40.0),
topRight: Radius.circular(40.0),
),
// Optional. Styles the search field.
inputDecoration: InputDecoration(
labelText: 'Search',
hintText: 'Start typing to search',
prefixIcon: const Icon(Icons.search),
border: OutlineInputBorder(
borderSide: BorderSide(
color: const Color(0xFF8C98A8).withOpacity(0.2),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: ElevatedButton(
onPressed: () {
showCountryPicker(
context: context,
//Optional. Can be used to exclude(remove) one ore more country from the countries list (optional).
exclude: <String>['KN', 'MF'],
favorite: <String>['SE'],
//Optional. Shows phone code before the country name.
showPhoneCode: true,
onSelect: (Country country) {
print('Select country: ${country.displayName}');
},
// Optional. Sets the theme for the country list picker.
countryListTheme: CountryListThemeData(
// Optional. Sets the border radius for the bottomsheet.
borderRadius: BorderRadius.only(
topLeft: Radius.circular(40.0),
topRight: Radius.circular(40.0),
),
// Optional. Styles the search field.
inputDecoration: InputDecoration(
labelText: 'Search',
hintText: 'Start typing to search',
prefixIcon: const Icon(Icons.search),
border: OutlineInputBorder(
borderSide: BorderSide(
color: const Color(0xFF8C98A8).withOpacity(0.2),
),
),
),
// Optional. Styles the text in the search field
searchTextStyle: TextStyle(
color: Colors.blue,
fontSize: 18,
),
),
),
),
// Optional. Styles the text in the search field
searchTextStyle: TextStyle(
color: Colors.blue,
fontSize: 18,
),
);
},
child: const Text('Show country picker bottom sheet'),
),
);
},
child: const Text('Show country picker'),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: ElevatedButton(
onPressed: () {
showCountryPicker(
context: context,
//Optional. Can be used to exclude(remove) one ore more country from the countries list (optional).
exclude: <String>['KN', 'MF'],
favorite: <String>['SE', 'CU'],
//Optional. Shows phone code before the country name.
showPhoneCode: true,
onSelect: (Country country) {
print('Select country: ${country.displayName}');
},
// Optional. Sets the theme for the country list picker.
countryListTheme: CountryListThemeData(
// Optional. Sets the border radius for the bottomsheet.
borderRadius: BorderRadius.all(
Radius.circular(40.0),
),
// Optional. Styles the search field.
inputDecoration: InputDecoration(
labelText: 'Search',
hintText: 'Start typing to search',
prefixIcon: const Icon(Icons.search),
border: OutlineInputBorder(
borderSide: BorderSide(
color: const Color(0xFF8C98A8).withOpacity(0.2),
),
),
),
// Optional. Styles the text in the search field
searchTextStyle: TextStyle(
color: Colors.blue,
fontSize: 18,
),
height: 300,
weight: 300,
),
dialogMode: true,
);
},
child: const Text('Show country picker dialog'),
),
),
],
),
),
);
Expand Down
2 changes: 2 additions & 0 deletions lib/country_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ void showCountryPicker({
bool showWorldWide = false,
bool showSearch = true,
bool useSafeArea = false,
bool dialogMode = false,
}) {
assert(
exclude == null || countryFilter == null,
Expand All @@ -73,5 +74,6 @@ void showCountryPicker({
showWorldWide: showWorldWide,
showSearch: showSearch,
useSafeArea: useSafeArea,
dialogMode: dialogMode,
);
}
91 changes: 65 additions & 26 deletions lib/src/country_list_bottom_sheet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,71 @@ void showCountryListBottomSheet({
bool showWorldWide = false,
bool showSearch = true,
bool useSafeArea = false,
bool dialogMode = false,
}) {
showModalBottomSheet(
context: context,
isScrollControlled: true,
backgroundColor: Colors.transparent,
useSafeArea: useSafeArea,
builder: (context) => _builder(
context,
onSelect,
favorite,
exclude,
countryFilter,
showPhoneCode,
countryListTheme,
searchAutofocus,
showWorldWide,
showSearch,
),
).whenComplete(() {
if (onClosed != null) onClosed();
});
if (dialogMode) {
showDialog(
context: context,
builder: (context) {
return Center(
child: SizedBox(
height: getHeight(context, countryListTheme),
width: getWeight(context, countryListTheme),
child: Material(
color: Colors.transparent,
child: _builder(
context,
onSelect,
favorite,
exclude,
countryFilter,
showPhoneCode,
countryListTheme,
searchAutofocus,
showWorldWide,
showSearch,
),
),
),
);
},
).whenComplete(() {
if (onClosed != null) onClosed();
});
} else {
showModalBottomSheet(
context: context,
isScrollControlled: true,
backgroundColor: Colors.transparent,
useSafeArea: useSafeArea,
builder: (context) => _builder(
context,
onSelect,
favorite,
exclude,
countryFilter,
showPhoneCode,
countryListTheme,
searchAutofocus,
showWorldWide,
showSearch,
),
).whenComplete(() {
if (onClosed != null) onClosed();
});
}
}

double getHeight(BuildContext context, CountryListThemeData? countryListTheme) {
final device = MediaQuery.of(context).size.height;
final statusBarHeight = MediaQuery.of(context).padding.top;
final height = countryListTheme?.height ??
device - (statusBarHeight + (kToolbarHeight / 1.5));
return height;
}

double getWeight(BuildContext context, CountryListThemeData? countryListTheme) {
return countryListTheme?.weight ?? MediaQuery.of(context).size.width * .7;
}

Widget _builder(
Expand All @@ -52,11 +96,6 @@ Widget _builder(
bool showWorldWide,
bool showSearch,
) {
final device = MediaQuery.of(context).size.height;
final statusBarHeight = MediaQuery.of(context).padding.top;
final height = countryListTheme?.bottomSheetHeight ??
device - (statusBarHeight + (kToolbarHeight / 1.5));

Color? _backgroundColor = countryListTheme?.backgroundColor ??
Theme.of(context).bottomSheetTheme.backgroundColor;
if (_backgroundColor == null) {
Expand All @@ -74,7 +113,7 @@ Widget _builder(
);

return Container(
height: height,
height: getHeight(context, countryListTheme),
padding: countryListTheme?.padding,
margin: countryListTheme?.margin,
decoration: BoxDecoration(
Expand Down
9 changes: 6 additions & 3 deletions lib/src/country_list_theme_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,26 @@ class CountryListThemeData {
///Country list modal height
///
/// By default it's fullscreen
final double? bottomSheetHeight;
final double? height;

/// the padding of the bottom sheet
final EdgeInsets? padding;

/// the margin of the bottom sheet
final EdgeInsets? margin;

final double? weight;

const CountryListThemeData({
this.backgroundColor,
this.textStyle,
this.searchTextStyle,
this.flagSize,
this.inputDecoration,
this.borderRadius,
this.bottomSheetHeight,
this.height,
this.padding,
this.margin,
this.margin,
this.weight,
});
}
Loading