(颤振)TextFormField更改labelColor的焦点 [英] (Flutter) TextFormField Change labelColor on Focus

查看:281
本文介绍了(颤振)TextFormField更改labelColor的焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在聚焦时更改我的labelText颜色.我可以更改文本颜色,但不能更改焦点颜色.

I am trying to change my labelText color when focused. I can change the text color but not when focused.

我尝试了所有提示文本颜色和标签文本颜色,但是没有帮助.

I have tried all the hint text colors and label text colors, but nothing helps.

Container(
  padding: EdgeInsets.fromLTRB(15, 10, 15, 0),
  child: TextFormField(
    cursorColor: Colors.lightGreen,
    keyboardType: TextInputType.phone,
    decoration: InputDecoration(
      labelText: 'Phone Number',
      hintText: 'Enter a Phone Number',
      focusedBorder: OutlineInputBorder(
        borderSide: BorderSide(
          color: Colors.lightGreen
        )
      ),
      border: OutlineInputBorder(
        borderSide: BorderSide()
      ),
    )
  ),
),

以下是发生的情况的图像:

Here are images of what is happening:

推荐答案

您需要有一种方法来确定其焦点状态,然后根据该状态为其颜色创建条件.这是focusNode会有所帮助的地方.在小部件创建后构造一个新的FocusNode,将其用作TextFormField中的focusNode属性.然后在TextFormFieldTextStyle属性的color属性中,您可以添加以下内容:

You'd need to have a way determine its focus state and then create a conditional for its color based off of that. This is where a focusNode would be helpful. Construct a new FocusNode off the widget creation, use that as the focusNode property in the TextFormField. Then in color property of the TextStyle property of the TextFormField you could add something like:

FocusNode myFocusNode = new FocusNode();

...

  return TextFormField(
    focusNode: myFocusNode,
    decoration: InputDecoration(
      labelText: 'test',
      labelStyle: TextStyle(
        color: myFocusNode.hasFocus ? Colors.blue : Colors.black
      )
    ),
  );

简要说明一下,您可能需要确保它位于StatefulWidget中,然后将侦听器添加到您创建的focusNode中,并在该focusNode上的任何事件上调用setState .否则,您将看不到任何更改.

EDIT : Just a quick note, you'll probably need to make sure this is in a StatefulWidget and then add a listener to the focusNode you created and call setState on any events on that focusNode. Otherwise you wont see any changes.

这篇关于(颤振)TextFormField更改labelColor的焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆