FocusNode
对象传递给文本字段的focusNode
属性。 FocusNode
具有addListener
方法,在其中您可以调用setState
,从而重新呈现小部件。class _ChangingColorsExampleState extends State<ChangingColorsPage> {
FocusNode _focusNode;
@override
void dispose() {
super.dispose();
_focusNode.dispose();
}
@override
void initState() {
super.initState();
_focusNode = new FocusNode();
_focusNode.addListener(_onOnFocusNodeEvent);
}
_onOnFocusNodeEvent() {
setState(() {
// Re-renders
});
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
backgroundColor: _getAppBarBackgroundColor(),
title: new Text('Changing Colors'),
),
body: new Container(
color: _getContainerBackgroundColor(),
padding: new EdgeInsets.all(40.0),
child: new TextField(
style: new TextStyle(color: _getInputTextColor()),
focusNode: _focusNode,
)
),
);
}
Color _getContainerBackgroundColor() {
return _focusNode.hasFocus ? Colors.blueGrey : Colors.white;
}
Color _getAppBarBackgroundColor() {
return _focusNode.hasFocus ? Colors.green : Colors.red;
}
Color _getInputTextColor() {
return _focusNode.hasFocus ? Colors.white : Colors.pink;
}
}
isFocused
状态。但是对于像fillColor
这样的属性不是这种情况。 - eldos