在flutter中禁用文本编辑字段

时间:2017-06-12 02:30:39

标签: dart flutter

我需要偶尔禁用TextFormField。我无法在窗口小部件中找到标记,或者控制器只是将其设置为只读或禁用。 最好的方法是什么?

11 个答案:

答案 0 :(得分:22)

还有另一种方法可以实现,这也不会导致this issue。希望这可能有助于某人。

创建AlwaysDisabledFocusNode并将其传递给focusNode的{​​{1}}媒体资源。

TextField

然后

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

<强>更新 new TextField( enableInteractiveSelection: false, // will disable paste operation focusNode: new AlwaysDisabledFocusNode(), ... ... ) 现在拥有TextField财产。你可以在哪里禁用enabled之类的

TextField

注意:这也会禁用与文本输入相关联的图标。

答案 1 :(得分:17)

TextFieldTextFormField都有一个名为enabled的参数。您可以使用布尔变量来控制它。 enabled=true表示它将充当编辑文本字段,而enabled=false将禁用TextField

答案 2 :(得分:8)

我结合使用了 readOnly enableInteractiveSelection 属性来在TextField上实现所需的行为。

TextField(
  readOnly: true,
  enableInteractiveSelection: true,
  onTap: () {
    do_something(),
  },
)

enableInteractiveSelection 设置为 true ,它将允许 onTap()正常运行。

答案 3 :(得分:7)

这不是框架当前提供的功能,但您可以使用FocusScope来阻止TextFormField请求焦点。

这是禁用时的样子。

(带提示文字) empty and readonly

(具有只读值) readonly

这是启用它时的样子。

(有焦点) focused

(没有焦点) editable

此代码如下:

import 'package:flutter/material.dart';

void main() {
  runApp(new MaterialApp(
    home: new HomePage(),
  ));
}

class HomePage extends StatefulWidget {
  HomePageState createState() => new HomePageState();
}

class HomePageState extends State<HomePage> {

  TextEditingController _controller = new TextEditingController();
  bool _enabled = false;

  @override
  Widget build(BuildContext context) {
    ThemeData theme = Theme.of(context);
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('Disabled Text'),
      ),
      floatingActionButton: new FloatingActionButton(
        child: new Icon(Icons.free_breakfast),
        onPressed: () {
          setState(() {
            _enabled = !_enabled;
          });
        }
      ),
      body: new Center(
        child: new Container(
          margin: const EdgeInsets.all(10.0),
          child: _enabled ?
          new TextFormField(controller: _controller) :
          new FocusScope(
            node: new FocusScopeNode(),
            child: new TextFormField(
              controller: _controller,
              style: theme.textTheme.subhead.copyWith(
                color: theme.disabledColor,
              ),
              decoration: new InputDecoration(
                hintText: _enabled ? 'Type something' : 'You cannot focus me',
              ),
            ),
          ),
        ),
      ),
    );
  }
}

答案 4 :(得分:4)

这是禁用TextField但保留其功能的另一种方法。我的意思是使用onTap

TextField(
    enableInteractiveSelection: false,
    onTap: () { FocusScope.of(context).requestFocus(new FocusNode()); },
)
  • enableInteractiveSelection

    将禁用TextField的内容选择

  • FocusScope.of(context).requestFocus(new FocusNode());

    将焦点更改为未使用的焦点节点

使用这种方式,您仍然可以触摸TextField,但是无法输入或选择其内容。相信我,有时候它会很有用(datepicker,timepicker等):)

答案 5 :(得分:3)

类似于html中的只读

<html>

     <body>
    <div class="abbreviation" onclick="parseWord(this)">
    <p id="noun">N</p>
    <p id="nominative">N</p>
    <p id="nn">NN</p>
    <p id="na">NA</p>
    </div>

    <div id="decoder">
    <p id="part-of-speech-decoded"></p>
    <p id="case-decoded"></p>
    </div>
    </body>
    </html>

这可以响应onTap。


类似于html中的禁用

TextField(
    enableInteractiveSelection: false,
    focusNode: FocusNode(),
)

这无法响应onTap。

答案 6 :(得分:1)

我尝试使用FocuseNode(),enabled = false,但不起作用,   取而代之的是,我使用了一个名为 AbsorbPointer 小工具。它用于防止窗口小部件被触摸或点击,我将TextFormField或其他窗口小部件包装在AbsordPointer小部件内,并提供了禁止触摸的参数

示例

AbsorbPointer(
      absorbing: true,  //To disable from touch use false while **true** for otherwise
      child: Your WidgetsName
);

答案 7 :(得分:0)

使用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)),
  ),

答案 8 :(得分:0)

使用plot_surface是正确的。但是,如果您仍要关注此文本字段,则可以遵循以下代码:


values = ['red.color\xa0', '2020-11-27\xa0', 'green.color\xa0', '2020-11-27\xa0', 'blue.color\xa0', '2020-11-27\xa0']

index = 0
result = []
while index < len(values):
    result.append([values[index].strip("\xa0"), values[index + 1].strip("\xa0")])
    index += 2

print(result) # prints [['red.color', '2020-11-27'], ['green.color', '2020-11-27'], ['blue.color', '2020-11-27']]

答案 9 :(得分:0)

它有 enabled 键,这对我来说很好。

TextFormField(
    enabled: false,
)

答案 10 :(得分:-1)

使用已启用:TextField 中的 false

文本框( 启用:假, 装饰:新的 InputDecoration( 后缀图标:图标按钮( onPressed: () => {}, 图标:图标( Icons.arrow_drop_down, 颜色:Colors.black, ), ), 边框:新的下划线输入边框( borderSide: new BorderSide(color: Colors.red))), )