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

Refactoring of input_widget and validator, added ability to include padding below selector button #421

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
4 changes: 2 additions & 2 deletions example/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:4.2.2'
classpath 'com.android.tools.build:gradle:7.4.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
Expand All @@ -26,6 +26,6 @@ subprojects {
project.evaluationDependsOn(':app')
}

task clean(type: Delete) {
tasks.register("clean", Delete) {
delete rootProject.buildDir
}
3 changes: 1 addition & 2 deletions example/android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#Fri Jun 23 08:50:38 CEST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-all.zip
11 changes: 8 additions & 3 deletions lib/src/utils/phone_number/phone_number_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,22 @@ class PhoneNumberUtil {
/// [isValidNumber] checks if a [phoneNumber] is valid.
/// Accepts [phoneNumber] and [isoCode]
/// Returns [Future<bool>].
static Future<bool?> isValidNumber(
static Future<bool> isValidNumber(
{required String phoneNumber, required String isoCode}) async {
if (phoneNumber.length < 2) {
return false;
}
return p.PhoneNumberUtil.isValidPhoneNumber(phoneNumber, isoCode);
// The .then is needed to make sure return Future<bool>, instead of Future<bool?>. It appears
// that the libphonenumber_plugin packages should be returning Future<bool> anyway, but the error
// of returning Future<bool?> runs deep into the dependencies. So fix here until those are fixed
// to return false if validation result is null.
return p.PhoneNumberUtil.isValidPhoneNumber(phoneNumber, isoCode)
.then((v) => v ?? false);
}

/// [normalizePhoneNumber] normalizes a string of characters representing a phone number
/// Accepts [phoneNumber] and [isoCode]
/// Returns [Future<String>]
/// Returns [Future<String?>]
static Future<String?> normalizePhoneNumber(
{required String phoneNumber, required String isoCode}) async {
return p.PhoneNumberUtil.normalizePhoneNumber(phoneNumber, isoCode);
Expand Down
64 changes: 26 additions & 38 deletions lib/src/widgets/input_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class InternationalPhoneNumberInput extends StatefulWidget {
final String? hintText;
final String? errorMessage;

final double selectorButtonBottomPadding;
final double selectorButtonOnErrorPadding;

/// Ignored if [setSelectorButtonAsPrefixIcon = true]
Expand Down Expand Up @@ -102,6 +103,7 @@ class InternationalPhoneNumberInput extends StatefulWidget {
this.initialValue,
this.hintText = 'Phone number',
this.errorMessage = 'Invalid phone number',
this.selectorButtonBottomPadding = 0,
this.selectorButtonOnErrorPadding = 24,
this.spaceBetweenSelectorAndTextField = 12,
this.maxLength = 15,
Expand Down Expand Up @@ -133,17 +135,18 @@ class InternationalPhoneNumberInput extends StatefulWidget {

class _InputWidgetState extends State<InternationalPhoneNumberInput> {
TextEditingController? controller;
double selectorButtonBottomPadding = 0;
late double selectorButtonBottomPadding;

Country? country;
List<Country> countries = [];
bool isNotValid = true;
bool isValidPhone = false;

@override
void initState() {
super.initState();
loadCountries();
controller = widget.textFieldController ?? TextEditingController();
selectorButtonBottomPadding = widget.selectorButtonBottomPadding;
initialiseWidget();
}

Expand Down Expand Up @@ -180,7 +183,7 @@ class _InputWidgetState extends State<InternationalPhoneNumberInput> {
widget.initialValue!.phoneNumber!.isNotEmpty &&
(await PhoneNumberUtil.isValidNumber(
phoneNumber: widget.initialValue!.phoneNumber!,
isoCode: widget.initialValue!.isoCode!))!) {
isoCode: widget.initialValue!.isoCode!))) {
String phoneNumber =
await PhoneNumber.getParsableNumber(widget.initialValue!);

Expand Down Expand Up @@ -230,33 +233,16 @@ class _InputWidgetState extends State<InternationalPhoneNumberInput> {

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

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

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

if (widget.onInputValidated != null) {
widget.onInputValidated!(true);
}
this.isNotValid = false;
if (widget.onInputChanged != null) {
widget.onInputChanged!(PhoneNumber(
phoneNumber: phoneNumber ??
'${this.country?.dialCode}$parsedPhoneNumberString',
isoCode: this.country?.alpha2Code,
dialCode: this.country?.dialCode));
}

if (widget.onInputValidated != null) {
widget.onInputValidated!(this.isValidPhone);
}
});
}
Expand All @@ -268,14 +254,16 @@ class _InputWidgetState extends State<InternationalPhoneNumberInput> {
String phoneNumber, String? isoCode) async {
if (phoneNumber.isNotEmpty && isoCode != null) {
try {
bool? isValidPhoneNumber = await PhoneNumberUtil.isValidNumber(
this.isValidPhone = await PhoneNumberUtil.isValidNumber(
phoneNumber: phoneNumber, isoCode: isoCode);

if (isValidPhoneNumber!) {
if (this.isValidPhone) {
return await PhoneNumberUtil.normalizePhoneNumber(
phoneNumber: phoneNumber, isoCode: isoCode);
}
} on Exception {
// ignore: unused_catch_clause, unused_catch_stack
} on Exception catch (e, s) {
//Keep catch clause for easier debugging in editors that can hover over e and s.
return null;
}
}
Expand Down Expand Up @@ -318,22 +306,22 @@ class _InputWidgetState extends State<InternationalPhoneNumberInput> {
///
/// Also updates [selectorButtonBottomPadding]
String? validator(String? value) {
bool isValid =
this.isNotValid && (value!.isNotEmpty || widget.ignoreBlank == false);
bool isValidInput =
this.isValidPhone && (value!.isNotEmpty || widget.ignoreBlank == false);
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
if (isValid && widget.errorMessage != null) {
if (!isValidInput && widget.errorMessage != null) {
setState(() {
this.selectorButtonBottomPadding =
widget.selectorButtonOnErrorPadding;
});
} else {
setState(() {
this.selectorButtonBottomPadding = 0;
this.selectorButtonBottomPadding = widget.selectorButtonBottomPadding;
});
}
});

return isValid ? widget.errorMessage : null;
return !isValidInput ? widget.errorMessage : null;
}

/// Changes Selector Button Country and Validate Change.
Expand Down