Skip to content
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

remove padding, should be managed by ThemeData #383

Open
wants to merge 2 commits into
base: develop
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
71 changes: 24 additions & 47 deletions lib/src/widgets/input_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class InternationalPhoneNumberInput extends StatefulWidget {
final TextStyle? selectorTextStyle;
final InputBorder? inputBorder;
final InputDecoration? inputDecoration;
final InputDecoration? buttonDecoration;
final InputDecoration? searchBoxDecoration;
final Color? cursorColor;
final TextAlign textAlign;
Expand Down Expand Up @@ -118,6 +119,7 @@ class InternationalPhoneNumberInput extends StatefulWidget {
this.inputBorder,
this.inputDecoration,
this.searchBoxDecoration,
this.buttonDecoration,
this.textAlign = TextAlign.start,
this.textAlignVertical = TextAlignVertical.center,
this.scrollPadding = const EdgeInsets.all(20.0),
Expand Down Expand Up @@ -179,14 +181,10 @@ class _InputWidgetState extends State<InternationalPhoneNumberInput> {
if (widget.initialValue!.phoneNumber != null &&
widget.initialValue!.phoneNumber!.isNotEmpty &&
(await PhoneNumberUtil.isValidNumber(
phoneNumber: widget.initialValue!.phoneNumber!,
isoCode: widget.initialValue!.isoCode!))!) {
String phoneNumber =
await PhoneNumber.getParsableNumber(widget.initialValue!);
phoneNumber: widget.initialValue!.phoneNumber!, isoCode: widget.initialValue!.isoCode!))!) {
String phoneNumber = await PhoneNumber.getParsableNumber(widget.initialValue!);

controller!.text = widget.formatInput
? phoneNumber
: phoneNumber.replaceAll(RegExp(r'[^\d+]'), '');
controller!.text = widget.formatInput ? phoneNumber : phoneNumber.replaceAll(RegExp(r'[^\d+]'), '');

phoneNumberControllerListener();
}
Expand All @@ -196,8 +194,7 @@ class _InputWidgetState extends State<InternationalPhoneNumberInput> {
/// loads countries from [Countries.countryList] and selected Country
void loadCountries({Country? previouslySelectedCountry}) {
if (this.mounted) {
List<Country> countries =
CountryProvider.getCountriesData(countries: widget.countries);
List<Country> countries = CountryProvider.getCountriesData(countries: widget.countries);

Country country = previouslySelectedCountry ??
Utils.getInitialSelectedCountry(
Expand All @@ -208,8 +205,7 @@ class _InputWidgetState extends State<InternationalPhoneNumberInput> {
// Remove potential duplicates
countries = countries.toSet().toList();

final CountryComparator? countryComparator =
widget.selectorConfig.countryComparator;
final CountryComparator? countryComparator = widget.selectorConfig.countryComparator;
if (countryComparator != null) {
countries.sort(countryComparator);
}
Expand All @@ -225,20 +221,15 @@ class _InputWidgetState extends State<InternationalPhoneNumberInput> {
/// the `ValueCallback` [widget.onInputValidated]
void phoneNumberControllerListener() {
if (this.mounted) {
String parsedPhoneNumberString =
controller!.text.replaceAll(RegExp(r'[^\d+]'), '');
String parsedPhoneNumberString = controller!.text.replaceAll(RegExp(r'[^\d+]'), '');

getParsedPhoneNumber(parsedPhoneNumberString, this.country?.alpha2Code)
.then((phoneNumber) {
getParsedPhoneNumber(parsedPhoneNumberString, this.country?.alpha2Code).then((phoneNumber) {
if (phoneNumber == null) {
String phoneNumber =
'${this.country?.dialCode}$parsedPhoneNumberString';
String phoneNumber = '${this.country?.dialCode}$parsedPhoneNumberString';

if (widget.onInputChanged != null) {
widget.onInputChanged!(PhoneNumber(
phoneNumber: phoneNumber,
isoCode: this.country?.alpha2Code,
dialCode: this.country?.dialCode));
phoneNumber: phoneNumber, isoCode: this.country?.alpha2Code, dialCode: this.country?.dialCode));
}

if (widget.onInputValidated != null) {
Expand All @@ -248,9 +239,7 @@ class _InputWidgetState extends State<InternationalPhoneNumberInput> {
} else {
if (widget.onInputChanged != null) {
widget.onInputChanged!(PhoneNumber(
phoneNumber: phoneNumber,
isoCode: this.country?.alpha2Code,
dialCode: this.country?.dialCode));
phoneNumber: phoneNumber, isoCode: this.country?.alpha2Code, dialCode: this.country?.dialCode));
}

if (widget.onInputValidated != null) {
Expand All @@ -264,16 +253,13 @@ class _InputWidgetState extends State<InternationalPhoneNumberInput> {

/// Returns a formatted String of [phoneNumber] with [isoCode], returns `null`
/// if [phoneNumber] is not valid or if an [Exception] is caught.
Future<String?> getParsedPhoneNumber(
String phoneNumber, String? isoCode) async {
Future<String?> getParsedPhoneNumber(String phoneNumber, String? isoCode) async {
if (phoneNumber.isNotEmpty && isoCode != null) {
try {
bool? isValidPhoneNumber = await PhoneNumberUtil.isValidNumber(
phoneNumber: phoneNumber, isoCode: isoCode);
bool? isValidPhoneNumber = await PhoneNumberUtil.isValidNumber(phoneNumber: phoneNumber, isoCode: isoCode);

if (isValidPhoneNumber!) {
return await PhoneNumberUtil.normalizePhoneNumber(
phoneNumber: phoneNumber, isoCode: isoCode);
return await PhoneNumberUtil.normalizePhoneNumber(phoneNumber: phoneNumber, isoCode: isoCode);
}
} on Exception {
return null;
Expand Down Expand Up @@ -318,13 +304,11 @@ class _InputWidgetState extends State<InternationalPhoneNumberInput> {
///
/// Also updates [selectorButtonBottomPadding]
String? validator(String? value) {
bool isValid =
this.isNotValid && (value!.isNotEmpty || widget.ignoreBlank == false);
bool isValid = this.isNotValid && (value!.isNotEmpty || widget.ignoreBlank == false);
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
if (isValid && widget.errorMessage != null) {
setState(() {
this.selectorButtonBottomPadding =
widget.selectorButtonOnErrorPadding;
this.selectorButtonBottomPadding = widget.selectorButtonOnErrorPadding;
});
} else {
setState(() {
Expand All @@ -346,17 +330,12 @@ class _InputWidgetState extends State<InternationalPhoneNumberInput> {

void _phoneNumberSaved() {
if (this.mounted) {
String parsedPhoneNumberString =
controller!.text.replaceAll(RegExp(r'[^\d+]'), '');
String parsedPhoneNumberString = controller!.text.replaceAll(RegExp(r'[^\d+]'), '');

String phoneNumber =
'${this.country?.dialCode ?? ''}' + parsedPhoneNumberString;
String phoneNumber = '${this.country?.dialCode ?? ''}' + parsedPhoneNumberString;

widget.onSaved?.call(
PhoneNumber(
phoneNumber: phoneNumber,
isoCode: this.country?.alpha2Code,
dialCode: this.country?.dialCode),
PhoneNumber(phoneNumber: phoneNumber, isoCode: this.country?.alpha2Code, dialCode: this.country?.dialCode),
);
}
}
Expand All @@ -370,20 +349,17 @@ class _InputWidgetState extends State<InternationalPhoneNumberInput> {
String? get locale {
if (widget.locale == null) return null;

if (widget.locale!.toLowerCase() == 'nb' ||
widget.locale!.toLowerCase() == 'nn') {
if (widget.locale!.toLowerCase() == 'nb' || widget.locale!.toLowerCase() == 'nn') {
return 'no';
}
return widget.locale;
}
}

class _InputWidgetView
extends WidgetView<InternationalPhoneNumberInput, _InputWidgetState> {
class _InputWidgetView extends WidgetView<InternationalPhoneNumberInput, _InputWidgetState> {
final _InputWidgetState state;

_InputWidgetView({Key? key, required this.state})
: super(key: key, state: state);
_InputWidgetView({Key? key, required this.state}) : super(key: key, state: state);

@override
Widget build(BuildContext context) {
Expand All @@ -406,6 +382,7 @@ class _InputWidgetView
selectorConfig: widget.selectorConfig,
selectorTextStyle: widget.selectorTextStyle,
searchBoxDecoration: widget.searchBoxDecoration,
buttonColor: widget.buttonDecoration?.fillColor,
locale: state.locale,
isEnabled: widget.isEnabled,
autoFocusSearchField: widget.autoFocusSearch,
Expand Down
39 changes: 21 additions & 18 deletions lib/src/widgets/selector_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class SelectorButton extends StatelessWidget {
final String? locale;
final bool isEnabled;
final bool isScrollControlled;
final Color? buttonColor;

final ValueChanged<Country?> onCountryChanged;

Expand All @@ -32,6 +33,7 @@ class SelectorButton extends StatelessWidget {
required this.onCountryChanged,
required this.isEnabled,
required this.isScrollControlled,
this.buttonColor,
}) : super(key: key);

@override
Expand Down Expand Up @@ -64,18 +66,20 @@ class SelectorButton extends StatelessWidget {
)
: MaterialButton(
key: Key(TestHelper.DropdownButtonKeyValue),
padding: EdgeInsets.zero,
minWidth: 0,
height: 0,
color: buttonColor, // background
elevation: 0,
hoverElevation: 0,
focusElevation: 0,
highlightElevation: 0,
onPressed: countries.isNotEmpty && countries.length > 1 && isEnabled
? () async {
Country? selected;
if (selectorConfig.selectorType ==
PhoneInputSelectorType.BOTTOM_SHEET) {
selected = await showCountrySelectorBottomSheet(
context, countries);
if (selectorConfig.selectorType == PhoneInputSelectorType.BOTTOM_SHEET) {
selected = await showCountrySelectorBottomSheet(context, countries);
} else {
selected =
await showCountrySelectorDialog(context, countries);
selected = await showCountrySelectorDialog(context, countries);
}

if (selected != null) {
Expand All @@ -84,7 +88,7 @@ class SelectorButton extends StatelessWidget {
}
: null,
child: Padding(
padding: const EdgeInsets.only(right: 8.0),
padding: const EdgeInsets.only(right: 8.0, top: 1, bottom: 1),
child: Item(
country: country,
showFlag: selectorConfig.showFlags,
Expand All @@ -98,8 +102,7 @@ class SelectorButton extends StatelessWidget {
}

/// Converts the list [countries] to `DropdownMenuItem`
List<DropdownMenuItem<Country>> mapCountryToDropdownItem(
List<Country> countries) {
List<DropdownMenuItem<Country>> mapCountryToDropdownItem(List<Country> countries) {
return countries.map((country) {
return DropdownMenuItem<Country>(
value: country,
Expand All @@ -117,8 +120,7 @@ class SelectorButton extends StatelessWidget {
}

/// shows a Dialog with list [countries] if the [PhoneInputSelectorType.DIALOG] is selected
Future<Country?> showCountrySelectorDialog(
BuildContext inheritedContext, List<Country> countries) {
Future<Country?> showCountrySelectorDialog(BuildContext inheritedContext, List<Country> countries) {
return showDialog(
context: inheritedContext,
barrierDismissible: true,
Expand All @@ -142,25 +144,26 @@ class SelectorButton extends StatelessWidget {
}

/// shows a Dialog with list [countries] if the [PhoneInputSelectorType.BOTTOM_SHEET] is selected
Future<Country?> showCountrySelectorBottomSheet(
BuildContext inheritedContext, List<Country> countries) {
Future<Country?> showCountrySelectorBottomSheet(BuildContext inheritedContext, List<Country> countries) {
return showModalBottomSheet(
context: inheritedContext,
clipBehavior: Clip.hardEdge,
isScrollControlled: isScrollControlled,
useSafeArea: true,
backgroundColor: Colors.transparent,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(12), topRight: Radius.circular(12))),
borderRadius: BorderRadius.only(topLeft: Radius.circular(12), topRight: Radius.circular(12))),
builder: (BuildContext context) {
return Stack(children: [
GestureDetector(
onTap: () => Navigator.pop(context),
),
Padding(
padding: EdgeInsets.only(
bottom: MediaQuery.of(context).viewInsets.bottom),
padding: EdgeInsets.only(bottom: MediaQuery.of(context).viewInsets.bottom),
child: DraggableScrollableSheet(
// initialChildSize: 0.5,
// minChildSize: 0.25,
// maxChildSize: 0.7,
builder: (BuildContext context, ScrollController controller) {
return Directionality(
textDirection: Directionality.of(inheritedContext),
Expand Down