Skip to content

Commit

Permalink
Support padding around the selector button.
Browse files Browse the repository at this point in the history
  • Loading branch information
darkstarx committed Mar 4, 2023
1 parent 75fdfb5 commit c43b7e6
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 64 deletions.
8 changes: 4 additions & 4 deletions lib/src/utils/selector_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@ class SelectorConfig {
/// [setSelectorButtonAsPrefixIcon], this sets/places the selector button inside the [TextField] as a prefixIcon.
final bool setSelectorButtonAsPrefixIcon;

/// Space before the flag icon
final double? leadingPadding;

/// Add white space for short dial code
final bool trailingSpace;

/// The padding around the selector.
final EdgeInsets? padding;

const SelectorConfig({
this.selectorType = PhoneInputSelectorType.DROPDOWN,
this.showFlags = true,
this.useEmoji = false,
this.countryComparator,
this.setSelectorButtonAsPrefixIcon = false,
this.leadingPadding,
this.trailingSpace = true,
this.padding,
});
}
3 changes: 1 addition & 2 deletions lib/src/widgets/input_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,7 @@ class _InputWidgetState extends State<InternationalPhoneNumberInput> {
);

if (widget.selectorConfig.setSelectorButtonAsPrefixIcon) {
return value.copyWith(
prefixIcon: SelectorButton(
return value.copyWith(prefixIcon: SelectorButton(
country: country,
countries: countries,
onCountryChanged: onCountryChanged,
Expand Down
3 changes: 0 additions & 3 deletions lib/src/widgets/item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class Item extends StatelessWidget {
final bool? useEmoji;
final TextStyle? textStyle;
final bool withCountryNames;
final double? leadingPadding;
final bool trailingSpace;

const Item({
Expand All @@ -19,7 +18,6 @@ class Item extends StatelessWidget {
this.useEmoji,
this.textStyle,
this.withCountryNames = false,
this.leadingPadding = 12,
this.trailingSpace = true,
}) : super(key: key);

Expand All @@ -34,7 +32,6 @@ class Item extends StatelessWidget {
mainAxisAlignment: MainAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
SizedBox(width: leadingPadding),
_Flag(
country: country,
showFlag: showFlag,
Expand Down
111 changes: 56 additions & 55 deletions lib/src/widgets/selector_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,65 +36,66 @@ class SelectorButton extends StatelessWidget {

@override
Widget build(BuildContext context) {
return selectorConfig.selectorType == PhoneInputSelectorType.DROPDOWN
? countries.isNotEmpty && countries.length > 1
? DropdownButtonHideUnderline(
child: DropdownButton<Country>(
key: Key(TestHelper.DropdownButtonKeyValue),
hint: Item(
country: country,
showFlag: selectorConfig.showFlags,
useEmoji: selectorConfig.useEmoji,
leadingPadding: selectorConfig.leadingPadding,
trailingSpace: selectorConfig.trailingSpace,
textStyle: selectorTextStyle,
),
value: country,
items: mapCountryToDropdownItem(countries),
onChanged: isEnabled ? onCountryChanged : null,
Widget button = selectorConfig.selectorType == PhoneInputSelectorType.DROPDOWN
? countries.isNotEmpty && countries.length > 1
? DropdownButtonHideUnderline(
child: DropdownButton<Country>(
key: Key(TestHelper.DropdownButtonKeyValue),
hint: Item(
country: country,
showFlag: selectorConfig.showFlags,
useEmoji: selectorConfig.useEmoji,
trailingSpace: selectorConfig.trailingSpace,
textStyle: selectorTextStyle,
),
)
: Item(
country: country,
showFlag: selectorConfig.showFlags,
useEmoji: selectorConfig.useEmoji,
leadingPadding: selectorConfig.leadingPadding,
trailingSpace: selectorConfig.trailingSpace,
textStyle: selectorTextStyle,
)
: MaterialButton(
key: Key(TestHelper.DropdownButtonKeyValue),
padding: EdgeInsets.zero,
minWidth: 0,
onPressed: countries.isNotEmpty && countries.length > 1 && isEnabled
? () async {
Country? selected;
if (selectorConfig.selectorType ==
PhoneInputSelectorType.BOTTOM_SHEET) {
selected = await showCountrySelectorBottomSheet(
context, countries);
} else {
selected =
await showCountrySelectorDialog(context, countries);
}
value: country,
items: mapCountryToDropdownItem(countries),
onChanged: isEnabled ? onCountryChanged : null,
),
)
: Item(
country: country,
showFlag: selectorConfig.showFlags,
useEmoji: selectorConfig.useEmoji,
trailingSpace: selectorConfig.trailingSpace,
textStyle: selectorTextStyle,
)
: MaterialButton(
key: Key(TestHelper.DropdownButtonKeyValue),
padding: EdgeInsets.zero,
minWidth: 0,
onPressed: countries.isNotEmpty && countries.length > 1 && isEnabled
? () async {
Country? selected;
if (selectorConfig.selectorType ==
PhoneInputSelectorType.BOTTOM_SHEET) {
selected = await showCountrySelectorBottomSheet(
context, countries);
} else {
selected =
await showCountrySelectorDialog(context, countries);
}

if (selected != null) {
onCountryChanged(selected);
}
if (selected != null) {
onCountryChanged(selected);
}
: null,
child: Padding(
padding: const EdgeInsets.only(right: 8.0),
child: Item(
country: country,
showFlag: selectorConfig.showFlags,
useEmoji: selectorConfig.useEmoji,
leadingPadding: selectorConfig.leadingPadding,
trailingSpace: selectorConfig.trailingSpace,
textStyle: selectorTextStyle,
),
}
: null,
child: Padding(
padding: const EdgeInsets.only(right: 8.0),
child: Item(
country: country,
showFlag: selectorConfig.showFlags,
useEmoji: selectorConfig.useEmoji,
trailingSpace: selectorConfig.trailingSpace,
textStyle: selectorTextStyle,
),
);
),
);
if (selectorConfig.padding != null) {
button = Padding(padding: selectorConfig.padding!, child: button);
}
return button;
}

/// Converts the list [countries] to `DropdownMenuItem`
Expand Down

0 comments on commit c43b7e6

Please sign in to comment.