我应该在代码中添加什么来实现textfield的文本标签颜色为绿色。
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)
),
),
)
最佳答案
创建一个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文本焦点上的标签颜色,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56187501/