Disable a text edit field in flutter

后端 未结 11 1519
深忆病人
深忆病人 2020-12-23 17:05

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?

相关标签:
11条回答
  • 2020-12-23 17:09

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

    TextField(enabled: false)
    
    0 讨论(0)
  • 2020-12-23 17:11

    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, ... ) :)

    0 讨论(0)
  • 2020-12-23 17:13

    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.

    0 讨论(0)
  • 2020-12-23 17:14
    TextFormField(
    readOnly: true,
    )
    
    0 讨论(0)
  • 2020-12-23 17:15

    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.

    0 讨论(0)
  • 2020-12-23 17:17

    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)),
      ),
    
    0 讨论(0)
提交回复
热议问题