Flutter Disabled TextField Not Show ErrorText - flutter

I am working with flutter disabled text field, its work with alert which provide some choices when clicked.
I validate the value using stream like code below in my case:
final validateGender =
StreamTransformer<String, String>.fromHandlers(handleData: (text, sink) {
if (text.isEmpty ||
text.toLowerCase() != 'male' ||
text.toLowerCase() != 'female') {
sink.addError('* Not choose yet');
} else {
sink.add(text);
}
});
and i make a textfield that can show errorText:
StreamBuilder<String>(
stream: stream,
builder: (context, snapshot) {
return TextField(
enabled: enable,
autofocus: isAutoFocus ?? false,
onTap: onTap,
controller: controller,
onChanged: onChanged,
keyboardType: TextInputType.text,
decoration: InputDecoration(
errorText: snapshot.error,
border: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(10.0)),
),
disabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(10.0)),
borderSide: BorderSide(color: Colors.gray, width: 1.0),
),
errorBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(10.0)),
borderSide: BorderSide(color: Colors.red, width: 1.0),
),
filled: true,
hintStyle: TextStyle(color: Colors.grey),
hintText: hintText,
fillColor: Colors.white,
),
);
},
),
it supposed to be show "* Not choose yet" below of the text field, but it show red border only. the result below:
my stream using rxdart, i think i don't need to put it here,

Set the errorStyle property of the TextFormField's input decoration property to use any color you want for the error (for both enabled and disabled fields):
TextFormField(
...
decoration: InputDecoration(
...
errorStyle: TextStyle(
color: Theme.of(context).errorColor,
),
),
),

If it's disabled, it's pretty unlikely that the form validator will run the validation for that field. (Put a print() trace in to find out.) Do you really want Flutter wasting time on disabled form elements? :)

Have your choose anything? Please check what's your snapshot.error is printing,
Your border is red because you added error text field, please handle it, After validation then add error text, either null.

Related

Change prefix icon color of text form field in flutter on clicking the field

I have a name form field in flutter app. There's a prefix icon in it. When I click the form field the color of icon changes to blue, rather I want to change it to green. How can I do that, can anyone please guide me? This is its code :
TextFormField(
decoration: InputDecoration(
focusedBorder: OutlineInputBorder(
borderSide: const BorderSide(color: Colors.green, width: 2.0),
borderRadius: BorderRadius.circular(10.0),
),
prefixIcon: const Icon(Icons.person),
hintText: "Name",
border: OutlineInputBorder(borderRadius: BorderRadius.circular(10)),
),
);
Flutter's way to go is to use resolveWith. You can check the current state to check if the text field is focused, shows an error, etc. And depending on that you set the color.
From the docs (https://api.flutter.dev/flutter/material/InputDecoration-class.html):
final ThemeData themeData = Theme.of(context);
return Theme(
data: themeData.copyWith(
inputDecorationTheme: themeData.inputDecorationTheme.copyWith(
prefixIconColor:
MaterialStateColor.resolveWith((Set<MaterialState> states) {
if (states.contains(MaterialState.focused)) {
return Colors.green;
}
if (states.contains(MaterialState.error)) {
return Colors.red;
}
return Colors.grey;
}),
)),
child: TextFormField(
initialValue: 'abc',
decoration: const InputDecoration(
prefixIcon: Icon(Icons.person),
),
),
);
Use Theme color to change then define the focus node to determine when the field is on focus in order to apply these color changes
...
FocusNode _fieldNode = FocusNode(); //<-Define this then
...
TextFormField(
decoration: InputDecoration(
focusedBorder: OutlineInputBorder(
borderSide: const BorderSide(color: Colors.green, width: 2.0),
borderRadius: BorderRadius.circular(10.0),
),
prefixIcon: Icon(Icons.person,
color: _fieldNode.hasFocus
? Theme.of(context).primaryColor
: Colors.purple)),
hintText: "Name",
border: OutlineInputBorder(borderRadius: BorderRadius.circular(10)),
),
);

how add text and icon inside TextFormField in flutter

i want to make the text and icon inside TextFormField
this is my code
enter image description here
_FormField() {
return TextFormField(
validator: (value) {
if (value!.isEmpty) {
print("test");
}
},
decoration: InputDecoration(
fillColor: kPrimaryLightColor,
filled: true,
focusColor: kPrimaryLightColor,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(30),
borderSide: BorderSide.none),
//hintText: 'Email'
//hintStyle: TextStyle(color: Colors.black38),
labelText: 'Email',
labelStyle: TextStyle(color: kPrimaryColor),
//icon: Icon(Icons.email),
icon: Icon(Icons.email_outlined),
),
);
}
i want it like that
enter image description here
You can use prefixIcon and suffixIcon of inputDecoration to add icons inside the field. If you want the field label only within the field and not as a label above it, specify only hintText and leave out LabelText. Check the following code, you can wrap the icons inside a Padding if you need more space.
TextFormField(
validator: (value) {
if (value!.isEmpty) {
print("test");
}
},
decoration: InputDecoration(
prefixIcon: Icon(Icons.email_outlined),
fillColor: Colors.grey,
filled: true,
focusColor: Colors.blue,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(30),
borderSide: BorderSide.none),
hintText: 'Email',
suffixIcon: Icon(Icons.email_outlined),
),
)
your can use prefixIcon and suffixIcon which takes widget. In which you can give Icon to the prefixIcon and to the suffixIcon you can give IconButton in which you can give the logic to hide/show password. To implement the logic declare a boolean variable which is used for if the password is to be shown or hidden. the logic must be like this.
bool showPassword=false;
_FormField() {
return TextFormField(
obscureText: !showPassword,
validator: (value) {
if (value!.isEmpty) {
print("test");
}
},
decoration: InputDecoration(
suffixIcon:IconButton(icon:showPassword?Icon(Icons.lock):Icon(Icons.lock_open),
onPressed:(){
setState(() {
showPassword=!showPassword;
});
}
)
),
);
}
The code is not formatted well sorry for that I write it manual here 🙏

How to hide error message and error border from textfield when writing on it in flutter

I'm trying to add validation on textfield, i want when i leave any textfield empty it change its border color into red and display a error message, so and when i write something in it then it should hide the border error and message, which is happening but not in efficient way, here is what i'm doing.
i created the custom textfield
Widget textformfieldCustom(context,keyboardType,width,icon, controller,errortext,onchanged, hintText, labelText) {
return Container(
width: width,
child:TextFormField(
keyboardType:keyboardType,
decoration: InputDecoration(
contentPadding:EdgeInsets.symmetric(vertical: 20.0, horizontal: 10.0),
errorText:errortext,
labelText: labelText,
labelStyle: GoogleFonts.montserrat(color: HexColor("#6e6b7b")),
hintStyle: GoogleFonts.montserrat(),
hintText: hintText,
prefixIcon: icon,
focusedBorder: OutlineInputBorder(borderRadius: BorderRadius.all(Radius.circular(10.0)),borderSide: BorderSide(color: HexColor("#6610f2"))),
border: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(10.0))),
errorBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(10.0)),
borderSide: BorderSide(color: Colors.red, width: 1))),
onSaved: (String? value) {
// This optional block of code can be used to run
// code when the user saves the form.
},
onChanged:onchanged,
controller: controller,
));
}
and calling it as like this
bool _validatetex = false;
textformfieldCustom(
context,
TextInputType.number,
MediaQuery.of(context).size.width * 0.9,
Icon(Icons.email,color: iconColor,),
phoneNoController,
_validatetex ? 'This is the required field' : null,
(value) {
setState(() {
phoneNoController.text.isEmpty ? _validatetex = true : _validatetex = false;
});
},
'Enter your phone number',
'Phone number'
),
i'm using a bool type variable in errortext and changing its state in onchanged, so i want to do it in efficient way, like if i have 10 textfields so i have to initialize 10 bool variables so this is not a good way to go. please help how to achieve this in efficient way.
You can use the validator property in `TextField``
validator: (value) {
if (value == null || value.isEmpty) {
return 'This is the required field';
}
return null;
},
Then your code,
Widget textformfieldCustom(context,keyboardType,width,icon, controller,errortext,onchanged, hintText, labelText) {
return Container(
width: width,
child:TextFormField(
keyboardType:keyboardType,
decoration: InputDecoration(
contentPadding:EdgeInsets.symmetric(vertical: 20.0, horizontal: 10.0),
labelText: labelText,
labelStyle: GoogleFonts.montserrat(color: HexColor("#6e6b7b")),
hintStyle: GoogleFonts.montserrat(),
hintText: hintText,
prefixIcon: icon,
focusedBorder: OutlineInputBorder(borderRadius: BorderRadius.all(Radius.circular(10.0)),borderSide: BorderSide(color: HexColor("#6610f2"))),
border: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(10.0))),
errorBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(10.0)),
borderSide: BorderSide(color: Colors.red, width: 1))),
onSaved: (String? value) {
// This optional block of code can be used to run
// code when the user saves the form.
},
onChanged: onchanged,
controller: controller,
// Validator
validator: (value) {
if (value == null || value.isEmpty) {
return 'This is the required field';
}
return null;
},
));
}
I got my answer from this tutorial.

Flutter: How to show or hide a label/text widget when a textformfield get or lost focus?

I am new with flutter.
I am making a user registration form, I want to achieve the following visual effect:
When a TextFormField is normal on the form, it looks like this:
But I want the following, when the textformfield is in "focus". When the user is typing it looks like this:
This is my average textFormField
TextFormField(
initialValue: name,
onChanged: (val) {
setState(() {
name = val;
print(name);
});
},
decoration: InputDecoration(
hintText: "Nombres",
hintStyle: TextStyle(
fontSize: scalador.setSp(22) * 2.63,
color: Color(0xFF949494)),
),
style: TextStyle(
color: Color(0xFF242427),
fontSize: scalador.setSp(22) * 2.63,
),
validator: (value) {
if (value.isEmpty) {
return 'Por favor ingrese su(s) Nombre(s)';
} else {
if (value.length < 4)
return 'El nombre debe tener mas de 4 caracteres';
}
return null;
}),
any ideas?
add labelText: 'Nombres', Property into InputDecoration :
decoration: InputDecoration(
hintText: "Nombres",
hintStyle: TextStyle(
fontSize: scalador.setSp(22) * 2.63,
color: Color(0xFF949494)),
),
labelText: 'Nombres',
)
source :https://api.flutter.dev/flutter/material/TextFormField-class.html
To add the desired textformfield in "focus" detail, you will need to include several things. To begin with, you will need a labelText which will be set to the string of your choice. In your case, it would probably be: labelText: "Nombres". Next, you will need an enabledBorder: which you can assign to OutlineInputBorder(in which you can specify border radius, border Side(color)) to your liking. Once you have the enabledBorder for when the user does not have it in "focus" then you will need focusedBorder: in which again you will assign to OutlineInputBorder() similar to enabledBorder. Lastly, you will need border: in which you can give it OutlineInputBorder(and your desired borderRadius inside).
This is an example for your reference
Padding(
padding: EdgeInsets.all(10),
child: new TextFormField(
decoration: new InputDecoration(
labelText: "Name",
labelStyle: TextStyle(
color: Color(0xFF264653),
),
fillColor: Colors.white,
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(25.0),
borderSide: BorderSide(
color: Color(0xFF264653),
),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(25.0),
borderSide: BorderSide(color: Color(0xFF264653))),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(25.0),
),
),
),
),
Depending on what you will be doing, I recommend checking out this article: https://medium.com/swlh/working-with-forms-in-flutter-a176cca9449a and/or
https://medium.com/#mahmudahsan/how-to-create-validate-and-save-form-in-flutter-e80b4d2a70a4

How to always show hint in text field not only when it is clicked in flutter?

I have a custom text field but as shown in the picture, the bottom text fields looks so vague and empty, I'd like to keep the hint showing even if the field is not focused, how do I achieve that in flutter?
here is my widget code:
Container(
margin: EdgeInsets.all(20),
child: TextFormField(
autofocus: true,
textAlign: TextAlign.right,
decoration: InputDecoration(
enabledBorder: const OutlineInputBorder(
borderSide: const BorderSide(color: Color(0xff0E9447), width: 2.0),
),
focusedBorder: const OutlineInputBorder(
borderSide: const BorderSide(color: Color(0xff0E9447), width: 2.0),
),
hintText: AppStrings.email,
labelText: AppStrings.email,
alignLabelWithHint: true,
hintStyle: TextStyle(color: AppColors.primaryColorLight),
),
),
),
If you would like the label to be visible at the top of the TextField, and the hint displayed at the same time you can simply add:
floatingLabelBehavior: FloatingLabelBehavior.always
to the TextFields InputDecoration (decoration).
(At the time of writing this, there is a bug that will only show the hint and suffix upon focus, this has been fixed in a very recent PR and will be available shortly, see GitHub issue)
Full Example
TextFormField(
controller: textController,
style: theme.textTheme.bodyText2,
keyboardType: keyboardType ?? TextInputType.number,
enableInteractiveSelection: false,
decoration: InputDecoration(
labelText: labelText,
labelStyle: theme.textTheme.headline6,
suffixText: suffixText ?? '',
border: OutlineInputBorder(
borderSide:
BorderSide(color: theme.textTheme.bodyText2.color, width: 2),
),
hintText: '0.0',
floatingLabelBehavior: FloatingLabelBehavior.always),
validator: (value) {
if (value.isEmpty) {
return 'Please enter some text';
}
return null;
},
onChanged: (String text) => onChange(text),
);
Ideally in Flutter you cannot do this as both hintText and labelText behave in two different ways. labelText is shown as hintText as long as the user does not focus on it. As soon as the user clicks on the TextField, the labelText animates to a specific position whereas a hintText remains visible until the user types something.
So using labelText and hintText together, does not make any sense as the TextField will wipe of the hintText while animating the label.
However with some extra effort, you can use Stack widget to solve your problem.
Declare a class variable (a variable within the concerned class, outside any block of code) to store a TextEditingController.
TextEditingController _controller;
And initialize in your class' initState(),
_controller= TextEditingController();
Solution Code:
Container(
margin: EdgeInsets.all(20),
child: Stack(
children : <Widget>[
TextFormField(
autofocus: true,
textAlign: TextAlign.right,
decoration: InputDecoration(
enabledBorder: const OutlineInputBorder(
borderSide: const BorderSide(color: Color(0xff0E9447), width: 2.0),
),
focusedBorder: const OutlineInputBorder(
borderSide: const BorderSide(color: Color(0xff0E9447), width: 2.0),
),
labelText: AppStrings.email,
alignLabelWithHint: true,
hintStyle: TextStyle(color: AppColors.primaryColorLight),
),
),
(_controller.text=="")
?
Text(
AppStrings.email,
style: TextStyle(
color: Colors.grey
// Style it according to your requirement / To make it look like hintText
),
)
:
Container();
],
),
),
Basic Logic of the above code: If the TextField does not have any text then display the (hint) Text
widget else don't display anything.
There is a way around this.
Use the labelText property and set floatingLabelBehavior: FloatingLabelBehavior.never.
This way you will always see the hint and when the User clicks on the TextField, it goes away.