Skip to content

Commit

Permalink
chore: fix error states
Browse files Browse the repository at this point in the history
  • Loading branch information
ice-hector committed Nov 30, 2023
1 parent 709f5c7 commit 26ecfe7
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 23 deletions.
26 changes: 20 additions & 6 deletions lib/app/features/auth/views/pages/fill_profile/validators.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
bool validateName(String text) {
return text.trim().isNotEmpty;
String? validateName(String text) {
if (text.trim().isEmpty) {
return 'Field is required';
}

return null;
}

bool validateNickname(String text) {
return text.trim().isNotEmpty && text.contains('@') && text.length > 1;
String? validateNickname(String text) {
if (text.trim().isEmpty) {
return 'Field is required';
} else if (!text.contains('@')) {
return 'Nickname must contain @';
} else if (text.length <= 1) {
return 'Nickname too short';
}
return null;
}

bool validateWhoInvited(String text) {
return text.trim().isNotEmpty;
String? validateWhoInvited(String text) {
if (text.trim().isEmpty) {
return 'Field is required';
}
return null;
}
45 changes: 28 additions & 17 deletions lib/app/shared/widgets/text_field_wrapper/text_field_wrapper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'package:ice/generated/assets.gen.dart';
enum TextFieldState { defaultState, errorState, successState, focusedState }

typedef OnTextChanged = void Function(String text);
typedef Validator = bool Function(String text);
typedef Validator = String? Function(String text);

class TextFieldWrapper extends StatefulWidget {
const TextFieldWrapper({
Expand Down Expand Up @@ -52,15 +52,26 @@ class TextFieldWrapperState extends State<TextFieldWrapper> {

void validateText() {
setState(() {
_state = _controller.text.isNotEmpty
? widget.validator(_controller.text)
? TextFieldState.successState
: TextFieldState.errorState
: TextFieldState.defaultState;
final String? validationError = widget.validator(_controller.text);

_state = (validationError == null)
? TextFieldState.successState
: TextFieldState.errorState;
});
widget.focusNode.unfocus();
}

Color _labelColorForState(BuildContext context) {
switch (_state) {
case TextFieldState.errorState:
return context.theme.appColors.attentionRed;
case TextFieldState.focusedState:
return context.theme.appColors.primaryAccent;
default:
return context.theme.appColors.tertararyText;
}
}

Color _getBorderColor(BuildContext context) {
switch (_state) {
case TextFieldState.errorState:
Expand All @@ -74,6 +85,14 @@ class TextFieldWrapperState extends State<TextFieldWrapper> {
}
}

String placeholder() {
final String? validationError = widget.validator(_controller.text);
if (_state == TextFieldState.errorState && validationError != null) {
return validationError;
}
return widget.placeholder;
}

Widget? _buildPrefixIcon() {
final String trimmedText = _controller.text.trim();
if (!widget.focusNode.hasFocus && trimmedText.isEmpty) {
Expand Down Expand Up @@ -128,20 +147,12 @@ class TextFieldWrapperState extends State<TextFieldWrapper> {
borderRadius: BorderRadius.circular(16),
),
label: Text(
widget.placeholder,
placeholder(),
style: context.theme.appTextThemes.body.copyWith(
color: widget.focusNode.hasFocus
? context.theme.appColors.primaryAccent
: context.theme.appColors
.tertararyText, // Use the color you desire
color: _labelColorForState(context) // Use the color you desire
,
),
),
// border: OutlineInputBorder(
// borderSide: BorderSide(
// color: _getBorderColor(context),
// ),
// borderRadius: BorderRadius.circular(16),
// ),
prefixIcon: _buildPrefixIcon(),
suffixIconConstraints: const BoxConstraints(
maxHeight: 15, // Adjust the maxHeight as needed
Expand Down

0 comments on commit 26ecfe7

Please sign in to comment.