Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter - Hide hint text when Text Field have focus

Tags:

flutter

dart

I need to know how to hide the hint text when I focus on the text field. This is my code:

class _ContactoState extends State<Contacto> {
  FocusNode focusMsj;

  @override
  void initState() {
    super.initState();

    focusMsj = FocusNode();
    focusMsj.addListener(() {
      if (!focusMsj.hasFocus) {
        FocusScope.of(context).requestFocus(focusMsj);
      }
    });
  }

TextField(
          focusNode: focusMsj,
            hintText: focusMsj.hasFocus ? ' ' : 'Hint Text',)

return WillPopScope(
          child: Listener(
            onPointerUp: (e) {
              focusMsj.hasFocus ? FocusScope.of(context).requestFocus(FocusNode()): '';
            },

Thank you

like image 854
Cristian F. Bustos Avatar asked Dec 23 '19 18:12

Cristian F. Bustos


2 Answers

There is a property for that:

TextField(decoration: InputDecoration(hasFloatingPlaceholder: false));

Edit: The version above is deprecated, the new version is:

TextField(decoration: InputDecoration(floatingLabelBehavior: FloatingLabelBehavior.never,),),
like image 197
flow Avatar answered Oct 22 '22 15:10

flow


One simple solution you can try is define labelText with FloatingBehavior.never

TextField(
    decoration: InputDecoration(
        labelText: "Search",
        floatingLabelBehavior: FloatingLabelBehavior.never,
    )
)

HintText will be shown when it is not focussed. On focus, hint text will disappear.

like image 45
Tasneem Haider Avatar answered Oct 22 '22 13:10

Tasneem Haider