Using a textformfield widget, I need to change the color in the background according to the focus and unfocus status, but this change does not change on the screen at the time of focus and unfocus.
Below is a code sample.
Here it is;
FocusNode _focusNode = FocusNode();
Container(
width: double.infinity,
color: _focusNode.hasFocus ? Colors.red : Colors.black,
child: TextFormField(
focusNode: _focusNode,
),
)
2
Answers
try another approach of doing it, by adding a listener on the
FocusNode
in theStatefullWidget
that will change the color based on it:First, in you’re
State
object:this will listen to the
FocusNode
, when it has to focus it will update the color to green, otherwise, it’s red.Then in the widget:
You can use
ValueNotifier