我正试着在对焦时改变我的labelText色.我可以更改文本 colored颜色 ,但不能在聚焦时更改.

我已经try 了所有的提示文本 colored颜色 和标签文本 colored颜色 ,但都没有用.

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

以下是正在发生的事情的图像:

enter image description here

enter image description here

推荐答案

你需要有一种方法来确定它的焦点状态,然后在此基础上为它的 colored颜色 创建一个条件.这就是focusNode会有帮助的地方.创建小部件时构造一个新的FocusNode,将其用作TextFormField中的focusNode属性.然后,可以在TextFormFieldTextStyle属性的color属性中添加如下内容:

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.否则,您不会看到任何更改.

Flutter相关问答推荐

我遇到了一个问题,从showDialogue()调用Navigator.pop()两次导致屏幕空白

如何使一个悬浮的appbar在flutter?

如何将 colored颜色 阴影作为默认容器 colored颜色 应用于自定义窗口小工具?

通过Ffltter应用程序与Docker服务器进行交互以进行身份验证

如何确定Flutter 中的文本 colored颜色 ?

如何在Flutter 中使用滚动展开窗口小部件

如何从flutter中不同类别的列表中获取所有产品

如何获取flutter中gridview项目占用的高度和宽度?

更改下拉按钮的背景 colored颜色 - flutter

Flutter ListView 在 Column 中工作但在 Row 中不工作

底部工作表顶部的一块 Flutter

Android Studio - Electric Eel 2022.1.1 更新 destruct 了 dart 文件图标

在 Flutter 中使用 HardwareKeyboard 时所有键都没有响应

有没有办法为文本中的下划线创建自定义样式?

Future.wait() 中的 futures 可以顺序调用吗?

从物理设备 Flutter 中移除 USB 后启动画面不可见?

我一直在try 创建按钮以导航到第二页,但不知何故 RaisedButton 功能无法正常工作

Positioned 的宽度或 SizedBox 的宽度都不适用于堆栈小部件 - Flutter

为什么这个参数类型不能分配给参数类型?

小部件库捕获的 Flutter 异常