Skip to content

Commit

Permalink
Merge pull request #9 from wisemen-digital/bugfix/text-field-icon
Browse files Browse the repository at this point in the history
text field icon bugfix and textCapitalization on email/password fields
  • Loading branch information
JanVeestraetenn committed Jun 14, 2024
2 parents d7e4cca + 22bc06e commit ef4dec1
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 107 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 1.0.12

* fixed password toggle misalign when error message is shown beneath `ValidatedTextFormField`
* set TextCapitalization to none when `isEmail` or `isPassword` in `ValidatedTextFormField`

## 1.0.11

* fixed labels not showing on Android in the `ValidatedTextFormField`
Expand Down
218 changes: 112 additions & 106 deletions lib/widgets/valided_form_text_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ShowPassword extends _$ShowPassword {
void toggleValue() => state = !state;
}

class ValidatedTextFormField extends ConsumerWidget {
class ValidatedTextFormField extends HookConsumerWidget {
const ValidatedTextFormField({
super.key,
this.decoration = const InputDecoration(),
Expand Down Expand Up @@ -110,6 +110,11 @@ class ValidatedTextFormField extends ConsumerWidget {
@override
Widget build(BuildContext context, WidgetRef ref) {
final passwordProvider = showPasswordProvider(key: key);

void togglePassword() {
ref.read(passwordProvider.notifier).toggleValue();
}

return WidgetWrapper(
wrapper: (child) {
if (showCupertinoLabel) {
Expand All @@ -129,112 +134,105 @@ class ValidatedTextFormField extends ConsumerWidget {
return child;
}
},
child: Stack(
alignment: Alignment.centerRight,
children: [
TextFormField(
cursorColor: cursorColor,
keyboardType: _getKeyboardType(),
textCapitalization: textCapitalization,
textInputAction: textInputAction,
onFieldSubmitted: (text) => onFieldSubmitted?.call(text, ref),
initialValue: initialValue,
focusNode: focusNode,
enabled: !disabled,
onEditingComplete: onEditingComplete,
onChanged: onChanged,
controller: controller,
decoration: decoration.copyWith(
errorStyle: hideErrorText ? const TextStyle(height: 0) : null,
label: showAndroidLabel && label != null ? Text(label!) : null,
labelStyle: _labelStyle(
context,
),
floatingLabelStyle: _labelStyle(
context,
),
),
readOnly: readOnly,
inputFormatters: inputFormatters,
obscureText: isPassword ? !ref.watch(passwordProvider) : isPassword,
scrollPadding: scrollPadding,
style: style,
autovalidateMode: autovalidateMode,
onTapOutside: onTapOutside,
minLines: minLines,
maxLines: maxLines,
validator: (value) {
//* Value null check:
if (value == null) {
return Swl.of(context).fieldRequired;
}

//* If non-required value is empty
if (!isRequired && value.isEmpty) {
return null;
}

//* Empty validation check:
if (value.isEmpty) {
return Swl.of(context).fieldRequired;
}

//* Email validation:
if (isEmail) {
if (!EmailValidator.validate(value)) {
return Swl.of(context).emailValidation;
}
}
//* Phone number validation:
if (isPhoneNumber) {
if (!RegExp(
r'^[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$',
).hasMatch(value)) {
return Swl.of(context).phoneNumberIsInvalid;
}
}
//* Password validation:
if (isPassword && (passwordValidation == null)) {
if (value.length < minimumPasswordLength) {
return Swl.of(context).passwordIsTooShort;
}
} else if (isPassword && (passwordValidation != null)) {
return passwordValidation!(value);
}
//* Custom validation
if (customValidation != null) {
return customValidation!(value);
}
return null;
},
child: TextFormField(
cursorColor: cursorColor,
keyboardType: _getKeyboardType(),
textCapitalization: _getTextCapitalization(),
textInputAction: textInputAction,
onFieldSubmitted: (text) => onFieldSubmitted?.call(text, ref),
initialValue: initialValue,
focusNode: focusNode,
enabled: !disabled,
onEditingComplete: onEditingComplete,
onChanged: onChanged,
controller: controller,
decoration: decoration.copyWith(
errorStyle: hideErrorText ? const TextStyle(height: 0) : null,
label: showAndroidLabel && label != null ? Text(label!) : null,
labelStyle: _labelStyle(
context,
),
if (isPassword && showObscureIcon)
Positioned(
top: 20,
bottom: 0,
child: IconButton(
hoverColor: Colors.transparent,
icon: ref.watch(passwordProvider)
? SvgPicture.asset(
hidePasswordIcon,
colorFilter:
ColorFilter.mode(iconColor, BlendMode.srcIn),
height: iconSize,
width: iconSize,
)
: SvgPicture.asset(
showPasswordIcon,
colorFilter:
ColorFilter.mode(iconColor, BlendMode.srcIn),
height: iconSize,
width: iconSize,
),
splashColor: Colors.transparent,
onPressed: () =>
ref.read(passwordProvider.notifier).toggleValue(),
),
),
],
floatingLabelStyle: _labelStyle(
context,
),
suffixIcon: !isPassword
? null
: IconButton(
hoverColor: Colors.transparent,
icon: ref.watch(passwordProvider)
? SvgPicture.asset(
hidePasswordIcon,
colorFilter:
ColorFilter.mode(iconColor, BlendMode.srcIn),
height: iconSize,
width: iconSize,
)
: SvgPicture.asset(
showPasswordIcon,
colorFilter:
ColorFilter.mode(iconColor, BlendMode.srcIn),
height: iconSize,
width: iconSize,
),
splashColor: Colors.transparent,
onPressed: togglePassword,
),
),
readOnly: readOnly,
inputFormatters: inputFormatters,
obscureText: isPassword ? !ref.watch(passwordProvider) : isPassword,
scrollPadding: scrollPadding,
style: style,
autovalidateMode: autovalidateMode,
onTapOutside: onTapOutside,
minLines: minLines,
maxLines: maxLines,
validator: (value) {
//* Value null check:
if (value == null) {
return Swl.of(context).fieldRequired;
}

value = value.trim();

//* If non-required value is empty
if (!isRequired && value.isEmpty) {
return null;
}

//* Empty validation check:
if (value.isEmpty) {
return Swl.of(context).fieldRequired;
}

//* Email validation:
if (isEmail) {
if (!EmailValidator.validate(value)) {
return Swl.of(context).emailValidation;
}
}
//* Phone number validation:
if (isPhoneNumber) {
if (!RegExp(
r'^[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$',
).hasMatch(value)) {
return Swl.of(context).phoneNumberIsInvalid;
}
}
//* Password validation:
if (isPassword && (passwordValidation == null)) {
if (value.length < minimumPasswordLength) {
return Swl.of(context).passwordIsTooShort;
}
} else if (isPassword && (passwordValidation != null)) {
return passwordValidation!(value);
}
//* Custom validation
if (customValidation != null) {
return customValidation!(value);
}
return null;
},
),
);
}
Expand All @@ -248,4 +246,12 @@ class ValidatedTextFormField extends ConsumerWidget {
return TextInputType.text;
}
}

TextCapitalization _getTextCapitalization() {
if (isEmail || isPassword) {
return TextCapitalization.none;
} else {
return textCapitalization;
}
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: wisewidgetslibrary
description: Wisemen widgets library containing platform specific widgets and other commonly useful widgets.
version: 1.0.11
version: 1.0.12
homepage: https://github.com/wisemen-digital/flutter-widgets-library
repository: https://github.com/wisemen-digital/flutter-widgets-library

Expand Down

0 comments on commit ef4dec1

Please sign in to comment.