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 23a2f05
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 57 deletions.
4 changes: 4 additions & 0 deletions lib/src/utils/selector_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ class SelectorConfig {
/// 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,
Expand All @@ -41,5 +44,6 @@ class SelectorConfig {
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
114 changes: 59 additions & 55 deletions lib/src/widgets/selector_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,65 +36,69 @@ 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,
leadingPadding: selectorConfig.leadingPadding,
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,
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);
}

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,
leadingPadding: selectorConfig.leadingPadding,
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 23a2f05

Please sign in to comment.