Disable a text edit field in flutter

2020-05-18 11:28发布

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?

标签: dart flutter
8条回答
Bombasti
2楼-- · 2020-05-18 11:49

There is now a way to disable TextField and TextFormField. See github here. Use it like this:

TextField(enabled: false)
查看更多
够拽才男人
3楼-- · 2020-05-18 11:52

I tried using FocuseNode(), enabled = false yet not working, Instead of that I use a widget called AbsorbPointer. It's use for preventing a widget from touch or tap, I wrap my TextFormField or other widget inside AbsordPointer Widgets and give a parameter to disable from touch

Example

AbsorbPointer(
      absorbing: true,  //To disable from touch use false while **true** for otherwise
      child: Your WidgetsName
);
查看更多
三岁会撩人
4楼-- · 2020-05-18 11:55

There is another way this can be achieved which also does not cause this issue. Hope this might help someone.

Create AlwaysDisabledFocusNode and pass it to the focusNode property of a TextField.

class AlwaysDisabledFocusNode extends FocusNode {
  @override
  bool get hasFocus => false;
}

then

new TextField(
    enableInteractiveSelection: false, // will disable paste operation
    focusNode: new AlwaysDisabledFocusNode(),
    ...
    ...
)

Update: TextField now has enabled property. Where you can just disable the TextField like

new TextField(
    enabled: false, 
    ...
    ...
)

Note: This will also disable icon associated with text input.

查看更多
叛逆
5楼-- · 2020-05-18 11:58

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.

查看更多
Rolldiameter
6楼-- · 2020-05-18 12:02

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.

查看更多
姐就是有狂的资本
7楼-- · 2020-05-18 12:03
TextFormField(
readOnly: true,
)
查看更多
登录 后发表回答