简体   繁体   English

Flutter - 当文本字段有焦点时隐藏提示文本

[英]Flutter - Hide hint text when Text Field have focus

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谢谢

For doing that matter you need to make something like that为了做那件事,你需要做这样的事情

class Play extends StatefulWidget {
  @override
  _PlayState createState() => _PlayState();
}

class _PlayState extends State<Play> {
  FocusNode focusNode = FocusNode();
  String hintText = 'Hello , iam Hint';
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    focusNode.addListener(() {
      if (focusNode.hasFocus) {
        hintText = '';
      } else {
        hintText = 'Hello , iam Hint';
      }
      setState(() {});
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      floatingActionButton: FloatingActionButton(onPressed: () {
        print(focusNode.hasFocus);
      }),
      body: Padding(
        padding: const EdgeInsets.all(8.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            TextField(
              focusNode: focusNode,
              decoration: InputDecoration(
                hintText: hintText,
              ),
            ),
            TextField(
              decoration: InputDecoration(
                hintText: '!!',
              ),
            ),
          ],
        ),
      ),
    );
  }
}

Shortly i listened to TextField by its focusNode property .不久,我通过其 focusNode 属性聆听了 TextField。 When TextField has focus i make hintText property equal empty String value当 TextField 获得焦点时,我使hintText 属性等于空字符串值

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

One simple solution you can try is define labelText with FloatingBehavior.never您可以尝试的一种简单解决方案是使用 FloatingBehavior.never 定义 labelText

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

HintText will be shown when it is not focussed.未聚焦时将显示 HintText。 On focus, hint text will disappear.聚焦时,提示文本将消失。

My understanding is there is no way to implement it without custom code.我的理解是没有自定义代码就无法实现它。 Hiding hintText when focused with "border: InputBorder.none," gives perfect login widget example as FloatingLabelBehavior and having animated labelText just won't do.以“border: InputBorder.none”为焦点时隐藏hintText 提供了完美的登录小部件示例,如FloatingLabelBehavior 并且具有动画labelText 是行不通的。 floatingLabelBehavior: FloatingLabelBehavior.never - helps in some situtations but not the exact thing we wanted. floatingLabelBehavior: FloatingLabelBehavior.never - 在某些情况下有所帮助,但不是我们想要的确切内容。 If you have labelText and hintText FloatingLabelBehavior.never is helpful to control hiding and showing hintText and animating labelText above the field.如果您有 labelText 和 hintText FloatingLabelBehavior.never 有助于控制在字段上方隐藏和显示 hintText 和动画 labelText。 WIth custom code we have使用自定义代码,我们有

String emailHintText = "E-mail"; 

on the top of state class.在状态类的顶部。 Then:然后:

Container(
             decoration: BoxDecoration(
                      border: Border.all(color: white),
                      borderRadius: BorderRadius.circular(5)),
             child: Padding(
                    padding: EdgeInsets.all(10),
                child:      

    Focus(
          onFocusChange: (hasFocus) {
                if (hasFocus) {
                        setState(() {});
                        emailHintText = "";
                      } 
                else {
                        setState(() {});
                        emailHintText = "E-mail";
                      }
                    },
          child: TextFormField(
                     decoration: InputDecoration(    
                                    hintText: emailHintText,
                                    border: InputBorder.none))));

Now, you should know that using setState is a bit costly operation and may not be the best option if not used for very important functionalities.现在,您应该知道使用 setState 的操作成本有点高,如果不用于非常重要的功能,可能不是最佳选择。

This works perfectly for me.这对我来说非常有效。 Just add on InputDecoration ( floatingLabelBehavior: FloatingLabelBehavior.never) of TextField or TextFormField.只需添加 TextField 或 TextFormField 的 InputDecoration ( floatingLabelBehavior: FloatingLabelBehavior.never)。

TextFormField( controller:_controller, decoration: InputDecoration( label: Text('Entrer your code here '), floatingLabelBehavior: FloatingLabelBehavior.never, ), );

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.简单地说,如果要更改labelStyle样式,请不要在hintText中添加提示文本,而仅在InputDecoration旁边提及labelText: 'Label'

TextField(
    decoration: InputDecoration(
        labelText: "Label",
        labelStyle: TextStyle(
          color: Colors.blueGrey,
        ),
        floatingLabelBehavior: FloatingLabelBehavior.never,
    )
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM