Flutter | How to show a Hint Text when a value is entered - flutter

I want the Hint Text to remain even when Text is entered in the TextField:
But if I give TextField something like "hintText:" in simple way, Hint Text disappears when TextField is entered:
What should I do so that the hintText doesn't disappear even when a value is entered in the TextField?
.
.
.
I tried the following:
I. I tried using the suffix widget. But it appears from the end of the TextField. (If it was possible to make the suffix widget appear after the text, I think the problem would have been solved.) :
II. Obviously Prefix Widget can't help here :
Any answers are welcome, thanks.

You can use label to be visible at the top of the TextField & the hint u can simply add below line:
floatingLabelBehavior: FloatingLabelBehavior.always
full example is given below
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),
);

Related

Remove error message in TextFormField in Flutter

Hi I want to remove error message caused by validator in TextFormField widget after I submit a form in flutter.
Ps : I want the error message disappeared after I select again the TextFormField to write into it again.
TextFormField(
controller: _emailController,
keyboardType: TextInputType.emailAddress,
textInputAction: TextInputAction.next,
validator: (value) {
if (value == null ||
value.isEmpty ||
!EmailValidator.validate(value)) {
return nullEmailMsg;
}
return null;
},
decoration: InputDecoration(
hintText: emailHint,
prefixIcon: Icon(Icons.mail),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
),
),
),
If you put your TextFormField inside a Form with the property autovalidateMode: AutovalidateMode.onUserInteraction, the validation function should trigger when you select again or change the text.
If the validation function is satisfied, the error message will be removed.
You have to use the autoValidateMode method in TextFormField.
Here, is the some of the reference:
https://api.flutter.dev/flutter/widgets/AutovalidateMode-class.html

How to make TextField which has 2 hintText?

I have ui of text field from Figma.
There are 2 hint texts where 1st hintText is located at the beginning of textfield and other one is at the end.
As can be seen, trailing is hint text which is disappearing when user inputs number.
If you have any idea please share how to make this kind of text field)
Try this:
TextField(
textAlign: TextAlign.end,
decoration: InputDecoration(
prefixIcon: Padding(
padding: EdgeInsets.all(15), child: Text('Prefix')),
border: OutlineInputBorder(),
hintText: "Cym",
),
),
You can just use the trailing for the hint and can you onChanged method to handle changes.. Like you will be managing your text on the behavior of input. For example if the input is empty then you text will be something like "A" if you input something text will be changed.
TextFormField(
decoration: InputDecoration(
suffix: Text(text)
),
onChanged: (value) {
// handle or change your text here
if(value.isEmpty){
setState(() { text = "A"});
}
else{
setState(() { myState = "any text"});
}
},
)

Showing an error under a TextField in Flutter

Material design allows a text field to indicate an error via a small red label under the input box: https://material.io/components/text-fields (see screenshot below).
Is there a way to achieve this for a TextField field in Flutter? I expected this to be a property of either TextField or TextEditingController, but haven't found anything like it.
It is present in the decoration property in TextField, also you can style it using it's style property.
TextField(
decoration: InputDecoration(
errorStyle: TextStyle(),
errorText: 'Please enter something'
),
),
You show errors based on the validation results which are returned by the validator function provided by TextFormField, You check for some conditions there and return an error message or null based on what you want to show and when, or if you don't want to show anything.
child: new TextFormField(
autocorrect: false,
validator: (value) {
if (value.isEmpty) {
return 'Error Message';
}
return null;
},
onSaved: (val) => //do something...,
decoration: new InputDecoration(labelText: "Label*"),
),

How to invert the color of TextFormField in flutter?

I changed my application color. However TextFormField have got very invisible.
Are there any way to modify the TextFormField as it is better.
Thanks.
LoVe is correct. There are constructors for the TextFormField class that you can use here: Flutter documentation. Another example:
TextFormField(
decoration: InputDecoration(
filled: true,
fillColor: Colors.lightBlue[50],
border: OutlineInputBorder(),
icon: Icon(Icons.person),
hintText: 'What do people call you?',
labelText: 'Name',
),
onSaved: (String value) {
// This optional block of code can be used to run
// code when the user saves the form.
},
),
you can change the style and fill color to make it fit with the current theme:
TextFormField(
style: someTextStyle,
decoration: InputDecoration(
hintStyle:someTextStyle
fillColor: someColor,
filled: true,
),
)

Change focus to next textfield in flutter

I have a code for making Text Fields as shown below
...Column(
children: <Widget>[
textField(text: 'User Name'),
SizedBox(height: 20),
textField(text: 'Password'), ])...
textField is a class that I made that has the following code to make a textfield. I did this to avoid code duplication,since I use textfields a lot in my applicatin.
...TextField(
textInputAction: TextInputAction.next,
obscureText: text == 'Password' ? true : false,
textCapitalization: TextCapitalization.words,
cursorColor: primaryColor,
textAlign: TextAlign.center,
decoration: InputDecoration(
labelText: text,
labelStyle: TextStyle(color: primaryColor, fontFamily: 'Bellotta'),
filled: true,
fillColor: Colors.white,
border: OutlineInputBorder(...
Since I use this function to create the text field I cant use focusNode to change focus.
How can I solve this issue??
u can specify two more parameter one for focusNode of current textfield & another one for focus u want to change e.g.
textField(
text: 'User Name',
focusNode: // your focus node,
nextFocusNode: // your next field focus node
),
now u can use :
FocusScope.of(context).requestFocus(nextFocus);
tip: if u want to decrease code duplication then instead of creating textfield method u should store ur InputDecoration in var/ method.