I want to change the text input type and input formatters of a text field dynamically on tap. But the problem is once the text input type is done it is not changed on tap whereas the label text acts as expected.
I have done like below
bool joinlinkname = false;
joinchanged() {
if (joinlinkname == false) {
setState(() {
joinlinkname = true;
});
} else {
setState(() {
joinlinkname = false;
});
}
}
TextField(
keyboardType: joinlinkname? TextInputType.text : TextInputType.phone,
labelText: joinlinkname ? 'num' : "text",
inputFormatters: [joinlinkname ?
FilteringTextInputFormatter.allow(RegExp('[azAZ09]')):FilteringTextInputFormatter.allow(RegExp('[0-9]')),
],
),
GestureDetector(
onTap: () {
joinchanged();
},
child: Text(joinlinkname ? 'number' : 'text',
style: TextStyle(
color: Colors.blue,
fontSize: 12,
),
),
),
Please can anyone tell how to do it?
You can copy paste run full code below
You can use
ValueListenableBuilderandValueNotifierYou also need
FocusNodeto control keyboardYou can see working demo below
code snippet
working demo
full code