Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

login screen

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),
                          ),
                    ),
                  ),


like image 288
Ghada Shebl Avatar asked Sep 02 '19 14:09

Ghada Shebl


People also ask

How do you change the hint in text on Flutter?

You can change hint text color in Flutter, by adding style to the TextField widget. Basically, you provide the styling instructions by using the InputDecoration widget.

How do you hide hint text on Flutter?

Just add on InputDecoration ( floatingLabelBehavior: FloatingLabelBehavior. never) of TextField or TextFormField. Show activity on this post. Simply, don't add the hintText in the InputDecoration and mention only the labelText: 'Label' alongside labelStyle if you want to change the style of the label.

How do you set the hint color TextField in Flutter?

If you want to change the hintColor of all the TextField Widget in the app, you can apply it in the Theme. Show activity on this post. After digging the source code for the InputDecorator used to determine the label color, here's what I found. In short, to change the hint color, set hintColor using Theme and ThemeData.

How do you make a TextField Uneditable in Flutter?

Use the enabled: property of the TextField widget by setting it to false : TextField( enabled: false, ... ) This field won't respond to onTap events - it is similar to a disabled field in HTML.


3 Answers

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),
    );
like image 115
Phill Wiggins Avatar answered Oct 09 '22 10:10

Phill Wiggins


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.

like image 35
Frankdroid7 Avatar answered Oct 09 '22 12:10

Frankdroid7


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.

like image 6
Son of Stackoverflow Avatar answered Oct 09 '22 12:10

Son of Stackoverflow