how add text and icon inside TextFormField in flutter - 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 🙏

Related

I want the value of textfield when user prints enter

I am a beginner in flutter, I was working with the textfield widget. I want the value of textfield when user click enter or when the value is completed. Thanks for the help in advance.
TextField(
cursorColor: primaryColor,
decoration: InputDecoration(
icon: Icon(
Icons.search,
color: primaryColor,
),
hintText: 'Search for your favourite',
border: InputBorder.none,
),
),
To get the text field value once it is completed, you can use the text field onSubmitted function. It provides you with the value once the user submits it. Here is the line of code you need to add.
TextField(
cursorColor: primaryColor,
decoration: InputDecoration(
icon: Icon(
Icons.search,
color: primaryColor,
),
hintText: 'Search for your favourite',
border: InputBorder.none,
),
onSubmitted: (value) {
print(value); // This will print the value submitted by the user. You can add your function inside this.
},
You can use onChanged to print it out.
TextField(
keyboardType: TextInputType.emailAddress,
textAlign: TextAlign.center,
onChanged: (value) {
email = value;
//Do something with the user input.
},
decoration: kTextFieldDecoration.copyWith(hintText: 'Eenter your email')),
if you would like to use an advanced validator to check input value.
Please refer to this page flutter-how-to-change-border-of-text-form-field-on-error

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 Disabled TextField Not Show ErrorText

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.

How to center text in AutoCompleteTextField Flutter

is it possible to center text inside SimpleAutoCompleteTextField?
I have tried Directionaly but no 'center' there. Couldn't find align option also. Any suggestions?
SimpleAutoCompleteTextField(
controller: widget._ingi_name_controller,
key: searchIngredientKey,
decoration: InputDecoration(
border: UnderlineInputBorder(
borderSide:
BorderSide(color: Theme.of(context).primaryColor),
),
hintText: 'hint',
),
suggestions: widget.ingredientSuggestions,
clearOnSubmit: false,
textChanged: (text) {
setState(() {});
},
submitOnSuggestionTap: true,
textSubmitted: (text) {}),

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.