[英]How to change TextField's textLable color on focus
我應該在代碼中添加什么來在焦點上實現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)
),
),
)
創建一個FocusNode
並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,
...
),
...
);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.