gpt4 book ai didi

flutter - 如何在焦点上更改 TextField textLable 颜色

转载 作者:IT老高 更新时间:2023-10-28 12:46:33 30 4
gpt4 key购买 nike

我应该在我的代码中添加什么来实现 TextField 的 textLable 颜色为绿色焦点。

 TextField(
keyboardType: TextInputType.emailAddress,
cursorColor: CustomColors.seaweed,
keyboardAppearance: Brightness.light,
autocorrect: false,
style: TextStyle(
color: Colors.black
),
decoration: InputDecoration(
fillColor: CustomColors.seaweed,
hasFloatingPlaceholder: true,
labelText: "Email",
hintText: "Please enter email",
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: CustomColors.seaweed)
),

),

)

enter image description here

最佳答案

创建一个 FocusNode 并向其添加监听器。聚焦后,将标签颜色更改为绿色。

class Foo extends StatefulWidget {
@override
createState() => _FooState();
}

class _FooState extends State<Foo> {
final FocusNode focusNode = FocusNode();

TextStyle labelStyle;

@override
void initState() {
super.initState();
focusNode.addListener(onFocusChange);
}

void onFocusChange() {
setState(() {
labelStyle = focusNode.hasFocus ? TextStyle(color: Colors.green) : null;
});
}

@override
void dispose() {
focusNode.removeListener(onFocusChange);
super.dispose();
}

...

TextField buildTextField() {
return TextField(
focusNode: focusNode,
decoration: InputDecoration(
labelStyle: labelStyle,
...
),
...
);
}
}

关于flutter - 如何在焦点上更改 TextField textLable 颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56187501/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com