I need to disable TextFormField occasionally. I couldn\'t find a flag in the widget, or the controller to simply make it read only or disable. What is the best way to do it?
There is now a way to disable TextField
and TextFormField
. See github here.
Use it like this:
TextField(enabled: false)
This is another way of somehow disabling a TextField
but keeping it's functionality. I mean for onTap
usage
TextField(
enableInteractiveSelection: false,
onTap: () { FocusScope.of(context).requestFocus(new FocusNode()); },
)
enableInteractiveSelection
will disable content selection of TextField
FocusScope.of(context).requestFocus(new FocusNode());
change the focus to an unused focus node
Using this way, you can still touch TextField
but can't type in it or select it's content. Believe me it's going to be useful sometimes ( datepicker, timepicker, ... ) :)
Similar to readonly like in html
TextField(
enableInteractiveSelection: false,
focusNode: FocusNode(),
)
This can response to onTap.
Similar to disabled like in html
TextField(
enable: false
)
This can not response to onTap.
TextFormField(
readOnly: true,
)
TextField
and TextFormField
both have an argument called enabled
. You can control it using a boolean variable. enabled=true
means it will act as an editing text field whereas enabled=false
will disable the TextField
.
Use readOnly: true
TextField(
readOnly: true,
controller: controller,
obscureText: obscureText,
onChanged: (value) {
onValueChange();
},
style: TextStyle(
color: ColorResource.COLOR_292828,
fontFamily: Font.AvenirLTProMedium.value,
fontSize: ScreenUtil().setHeight(Size.FOURTEEN)),
decoration: InputDecoration(
border: InputBorder.none,
hintText: hint,
hintStyle: TextStyle(
fontSize: ScreenUtil().setHeight(Size.FOURTEEN),
color: ColorResource.COLOR_HINT,
fontFamily: Font.AvenirLTProBook.value)),
),