Skip to content

Commit

Permalink
[Murat] Remove MaskSplitCharacter enum and add comments to explain fu…
Browse files Browse the repository at this point in the history
…nction and enums
  • Loading branch information
Murat Gun committed Nov 29, 2023
1 parent 53d8f37 commit 0d015dc
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 67 deletions.
23 changes: 12 additions & 11 deletions lib/src/email_phone_textfield.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import 'enums/country.dart';
import 'enums/country_picker_height.dart';
import 'enums/country_picker_menu.dart';
import 'enums/ephone_textfield_type.dart';
import 'enums/mask_split_character.dart';

class EPhoneField extends StatefulWidget {
const EPhoneField({
Expand Down Expand Up @@ -39,7 +38,7 @@ class EPhoneField extends StatefulWidget {
hintText: 'Email or phone number',
),
this.countryPickerButtonIcon = Icons.arrow_drop_down,
this.phoneNumberMaskSplitter = MaskSplitCharacter.space,
this.phoneNumberMaskSplitter,
this.inputFormatters,
this.emailValidator,
this.phoneValidator,
Expand Down Expand Up @@ -149,7 +148,7 @@ class EPhoneField extends StatefulWidget {
/// If the [MaskSplitCharacter] is [MaskSplitCharacter.none], the [PhoneNumberMaskFormatter] is not used.
/// If the [MaskSplitCharacter] is [MaskSplitCharacter.space], the [PhoneNumberMaskFormatter] is used with the space character as the mask splitter.
/// If the [MaskSplitCharacter] is [MaskSplitCharacter.dash], the [PhoneNumberMaskFormatter] is used with the dash character as the mask splitter.
final MaskSplitCharacter phoneNumberMaskSplitter;
final String? phoneNumberMaskSplitter;

/// The [List<TextInputFormatter>] to be used as the input formatters of the input field.
/// As default, the [PhoneNumberMaskFormatter] and [PhoneNumberDigistOnlyFormatter] are used when the [EphoneFieldType] is [EphoneFieldType.phone].
Expand Down Expand Up @@ -199,11 +198,11 @@ class _EphoneFieldState extends State<EPhoneField> {
controller: _controller,
focusNode: _focusNode,
autovalidateMode: widget.autovalidateMode,
onChanged: _type.onChanged(_selectedCountry, widget.phoneNumberMaskSplitter.value, widget.onChanged),
onSaved: _type.onSaved(_selectedCountry, widget.phoneNumberMaskSplitter.value, widget.onSaved),
onChanged: _type.onChanged(_selectedCountry, widget.phoneNumberMaskSplitter, widget.onChanged),
onSaved: _type.onSaved(_selectedCountry, widget.phoneNumberMaskSplitter, widget.onSaved),
onFieldSubmitted: _type.onFieldSubmitted(
_selectedCountry,
widget.phoneNumberMaskSplitter.value,
widget.phoneNumberMaskSplitter,
widget.onFieldSubmitted,
),
initialValue: widget.initialValue,
Expand All @@ -214,11 +213,10 @@ class _EphoneFieldState extends State<EPhoneField> {
validator: _type.validator(
_selectedValidator,
_selectedCountry,
widget.phoneNumberMaskSplitter.value,
widget.phoneNumberMaskSplitter,
),
inputFormatters: widget.inputFormatters ??
_type.inputFormatters(_selectedCountry, widget.phoneNumberMaskSplitter != MaskSplitCharacter.none,
widget.phoneNumberMaskSplitter.value),
inputFormatters:
widget.inputFormatters ?? _type.inputFormatters(_selectedCountry, widget.phoneNumberMaskSplitter),
);
}

Expand Down Expand Up @@ -249,7 +247,10 @@ class _EphoneFieldState extends State<EPhoneField> {

/// Updates the [_type] of the input field based on the [_controller] text.
void _updateTextFieldType() {
final text = _controller.text.replaceAll(widget.phoneNumberMaskSplitter.value, '');
String text = _controller.text;
if (widget.phoneNumberMaskSplitter != null) {
text.replaceAll(widget.phoneNumberMaskSplitter!, '');
}
if (text.isEmpty) {
setState(() {
_type = widget.initialType;
Expand Down
3 changes: 3 additions & 0 deletions lib/src/enums/country.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/// This class is used to get the country data
/// It staticly contains all the countries in the world with their data
class Country {
final String name;
final String alpha2;
Expand All @@ -20,6 +22,7 @@ class Country {
required this.maxLength,
required this.mask});

/// Returns the all the countries in the world that are supported by this package
static const List<Country> values = <Country>[
Country.afghanistan,
Country.albania,
Expand Down
18 changes: 11 additions & 7 deletions lib/src/enums/country_picker_height.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/widgets.dart';

/// This enum is used to set the height of the country picker
enum CountryPickerHeigth {
/// 100% of screen height
h100,
Expand All @@ -14,22 +15,25 @@ enum CountryPickerHeigth {
h25
}

// This extension is used to get the height of the CountryPicker based on the CountryPickerHeigth enum
// Example: CountryPickerHeigth.h100.height(context) will return the height of the CountryPicker based on the screen height
/// This extension is used to get the height of the country picker
/// based on the [CountryPickerHeigth] enum
extension CountryPickerHeigthExtension on CountryPickerHeigth {
/// Returns the height of the CountryPicker based on the CountryPickerHeigth enum
/// Returns the height of the country picker based on the [CountryPickerHeigth] enum
double height(BuildContext context) {
switch (this) {
// 100% of screen height
/// 100% of screen height
case CountryPickerHeigth.h100:
return MediaQuery.of(context).size.height;
// 75% of screen height

/// 75% of screen height
case CountryPickerHeigth.h50:
return MediaQuery.of(context).size.height / 2;
// 50% of screen height

/// 50% of screen height
case CountryPickerHeigth.h75:
return MediaQuery.of(context).size.height * 0.75;
// 25% of screen height

/// 25% of screen height
case CountryPickerHeigth.h25:
return MediaQuery.of(context).size.height * 0.25;
}
Expand Down
1 change: 1 addition & 0 deletions lib/src/enums/country_picker_menu.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/// The type of the menu to show when the country picker is clicked
enum PickerMenuType {
/// Show the picker in a dialog
dialog,
Expand Down
1 change: 0 additions & 1 deletion lib/src/enums/enums.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export './country_picker_height.dart';
export './country_picker_menu.dart';
export './mask_split_character.dart';
export './ephone_textfield_type.dart';
export './country.dart';
31 changes: 25 additions & 6 deletions lib/src/enums/ephone_textfield_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ import 'package:flutter/services.dart';
import '../formatters/formatters.dart';
import 'country.dart';

/// This enum is used to set the type of the [EphoneField]
enum EphoneFieldType { initial, email, phone }

/// This extension is used to get the type of the [EphoneField]
/// based on the [EphoneFieldType] enum
extension EPhoneTextFielExtension on EphoneFieldType {
/// Returns the keyboard type of the [EphoneField] based on the [EphoneFieldType] enum
TextInputType get keyboardType {
switch (this) {
case EphoneFieldType.initial:
Expand All @@ -18,22 +22,29 @@ extension EPhoneTextFielExtension on EphoneFieldType {
}
}

List<TextInputFormatter> inputFormatters(Country country, bool useMask, String maskSplitCharacter) {
/// Returns the input formatters of the [EphoneField] based on the [EphoneFieldType] enum
/// Given the [Country] and the [useMask] boolean
/// If [useMask] is true, the [PhoneNumberMaskFormatter] will be used
/// If [useMask] is false, the [LengthLimitingTextInputFormatter] will be used
/// In both cases, the [PhoneNumberDigistOnlyFormatter] will be used
List<TextInputFormatter> inputFormatters(Country country, String? maskSplitCharacter) {
switch (this) {
case EphoneFieldType.initial:
return [];
case EphoneFieldType.email:
return [];
case EphoneFieldType.phone:
return [
useMask
maskSplitCharacter != null
? PhoneNumberMaskFormatter(country: country, maskSplitCharacter: maskSplitCharacter)
: LengthLimitingTextInputFormatter(country.maxLength),
PhoneNumberDigistOnlyFormatter(maskSplitCharacter: maskSplitCharacter)
];
}
}

/// Returns the label text of the [EphoneField] based on the [EphoneFieldType] enum
/// Given the [emptyLabelText], [emailLabelText] and [phoneLabelText]
String labelText(String emptyLabelText, String emailLabelText, String phoneLabelText) {
switch (this) {
case EphoneFieldType.initial:
Expand All @@ -45,8 +56,10 @@ extension EPhoneTextFielExtension on EphoneFieldType {
}
}

/// Returns the hint text of the [EphoneField] based on the [EphoneFieldType] enum
/// Given the [validator], [Country] and [maskSplitCharacter]
String? Function(String?)? validator(
String? Function(String?)? typeValidator, Country country, String maskSplitCharacter) {
String? Function(String?)? typeValidator, Country country, String? maskSplitCharacter) {
switch (this) {
case EphoneFieldType.initial:
return typeValidator;
Expand All @@ -58,8 +71,10 @@ extension EPhoneTextFielExtension on EphoneFieldType {
}
}

/// Returns the hint text of the [EphoneField] based on the [EphoneFieldType] enum
/// Given the [Country], [maskSplitCharacter] and [onFieldSubmitted]
void Function(String?)? onFieldSubmitted(
Country country, String maskSplitCharacter, void Function(String?)? onFieldSubmitted) {
Country country, String? maskSplitCharacter, void Function(String?)? onFieldSubmitted) {
switch (this) {
case EphoneFieldType.initial:
return onFieldSubmitted;
Expand All @@ -71,7 +86,9 @@ extension EPhoneTextFielExtension on EphoneFieldType {
}
}

void Function(String?)? onSaved(Country country, String maskSplitCharacter, void Function(String?)? onSaved) {
/// Returns the hint text of the [EphoneField] based on the [EphoneFieldType] enum
/// Given the [Country], [maskSplitCharacter] and [onSaved]
void Function(String?)? onSaved(Country country, String? maskSplitCharacter, void Function(String?)? onSaved) {
switch (this) {
case EphoneFieldType.initial:
return onSaved;
Expand All @@ -82,7 +99,9 @@ extension EPhoneTextFielExtension on EphoneFieldType {
}
}

void Function(String)? onChanged(Country country, String maskSplitCharacter, void Function(String)? onChanged) {
/// Returns the hint text of the [EphoneField] based on the [EphoneFieldType] enum
/// Given the [Country], [maskSplitCharacter] and [onChanged]
void Function(String)? onChanged(Country country, String? maskSplitCharacter, void Function(String)? onChanged) {
switch (this) {
case EphoneFieldType.initial:
return onChanged;
Expand Down
18 changes: 0 additions & 18 deletions lib/src/enums/mask_split_character.dart

This file was deleted.

8 changes: 3 additions & 5 deletions lib/src/formatters/phone_number_digits_only_formatter.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import 'package:flutter/services.dart';

class PhoneNumberDigistOnlyFormatter extends TextInputFormatter {
final String maskSplitCharacter;
final String? maskSplitCharacter;

const PhoneNumberDigistOnlyFormatter({required this.maskSplitCharacter});
@override
TextEditingValue formatEditUpdate(
TextEditingValue oldValue, TextEditingValue newValue) {
TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) {
// Remove all non-digits characters without mask split character from the input string
final String newText =
newValue.text.replaceAll(RegExp('[^0-9$maskSplitCharacter]'), '');
final String newText = newValue.text.replaceAll(RegExp('[^0-9$maskSplitCharacter]'), '');
final int selectionIndex = newText.length;

return TextEditingValue(
Expand Down
25 changes: 13 additions & 12 deletions lib/src/formatters/phone_number_mask.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@ import '../enums/enums.dart';

class PhoneNumberMaskFormatter extends TextInputFormatter {
final Country country;
final String maskSplitCharacter;
final String? maskSplitCharacter;

const PhoneNumberMaskFormatter(
{required this.country, required this.maskSplitCharacter});
const PhoneNumberMaskFormatter({required this.country, required this.maskSplitCharacter});

@override
TextEditingValue formatEditUpdate(
TextEditingValue oldValue, TextEditingValue newValue) {
TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) {
final String mask = country.mask;
final String maskCharacter = country.mask[0];
final String text = newValue.text.replaceAll(maskSplitCharacter, '');
final String oldText = oldValue.text.replaceAll(maskSplitCharacter, '');
final String newText =
_applyMask(mask, maskCharacter, maskSplitCharacter, text, oldText);
String text = newValue.text;
String oldText = oldValue.text;
if (maskSplitCharacter != null) {
text = text.replaceAll(maskSplitCharacter!, '');
oldText = oldText.replaceAll(maskSplitCharacter!, '');
}
final String newText = _applyMask(mask, maskCharacter, maskSplitCharacter, text, oldText);
final int selectionIndex = newText.length;

return TextEditingValue(
Expand All @@ -29,10 +30,11 @@ class PhoneNumberMaskFormatter extends TextInputFormatter {
String _applyMask(
String mask,
String maskCharacter,
String maskSplitCharacter,
String? maskSplitCharacter,
String text,
String oldText,
) {
if (maskSplitCharacter == null) return text;
final StringBuffer newText = StringBuffer();

int textIndex = 0;
Expand All @@ -50,8 +52,7 @@ class PhoneNumberMaskFormatter extends TextInputFormatter {
}
}

if (oldText.length > text.length &&
newText.toString().endsWith(maskSplitCharacter)) {
if (oldText.length > text.length && newText.toString().endsWith(maskSplitCharacter)) {
newText.write(oldText[oldText.length - 1]);
}

Expand Down
6 changes: 3 additions & 3 deletions lib/src/utils/ephone_field_utils.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class EphoneFieldUtils {
static String? combinePrefix(
int prefix, String? phoneNumber, String maskSplitter) {
static String? combinePrefix(int prefix, String? phoneNumber, String? maskSplitter) {
if (phoneNumber == null || phoneNumber.isEmpty) return null;
return '+${prefix.toString() + phoneNumber.replaceAll(maskSplitter, '')}';
final String phoneNumberWithPrefix = '+${prefix.toString() + phoneNumber}';
return maskSplitter == null ? phoneNumberWithPrefix : phoneNumberWithPrefix.replaceAll(maskSplitter, '');
}
}
8 changes: 4 additions & 4 deletions test/unit/ephone_field_type_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ void main() {
'should EphoneFieldType inputFormatters match successfully',
() {
expect(
EphoneFieldType.email.inputFormatters(Country.unitedStates, true, " "),
EphoneFieldType.email.inputFormatters(Country.unitedStates, " "),
[],
);
expect(
EphoneFieldType.phone.inputFormatters(Country.unitedStates, true, " ").length,
EphoneFieldType.phone.inputFormatters(Country.unitedStates, " ").length,
2,
);
expect(
EphoneFieldType.phone.inputFormatters(Country.unitedStates, false, "").length,
EphoneFieldType.phone.inputFormatters(Country.unitedStates, "").length,
2,
);
expect(
EphoneFieldType.initial.inputFormatters(Country.unitedStates, true, " "),
EphoneFieldType.initial.inputFormatters(Country.unitedStates, " "),
[],
);
},
Expand Down

0 comments on commit 0d015dc

Please sign in to comment.