我有 8 个 TextFiled,对于每个我想指定一个焦点,以便颜色发生变化。但是我有一个错误 - 在 null 上调用了 getter 'hasFocus'。我该如何解决?
class _EditAccountScreenState extends State<EditAccountScreen> {
FocusNode _focusNodeFio;
FocusNode _focusNodeCompany;
...
@override
void initState() {
super.initState();
_focusNodeFio = FocusNode();
_focusNodeCompany = FocusNode();
...
}
@override
void dispose() {
_focusNodeFio.dispose();
_focusNodeCompany.dispose();
...
super.dispose();
}
@override
Widget build(BuildContext context) {
.......
Padding(
child: TextField(
focusNode: _focusNodeFio,
onTap: () {
setState(() {
FocusScope.of(context).requestFocus(_focusNodeFio);
});
},
decoration: InputDecoration(
labelText: 'Contacts',
labelStyle: TextStyle(
color: _focusNodeFio.hasFocus ? Colors.teal[300] : Colors.grey
),
contentPadding:
答案 0 :(得分:0)
这是有效的!
final FocusNode _fNode = FocusNode();
@override
Widget build(BuildContext context) {
return Scaffold(
body: TextField(
focusNode: _fNode,
onTap: () => FocusScope.of(context).requestFocus(_fNode),
decoration: InputDecoration(
labelText: 'Contacts',
labelStyle: TextStyle(color: _fNode.hasFocus ? Colors.teal[300] : Colors.grey),
),
),
);
}