你如何改变文本域内的价值?

Dan*_*iel 33 dart flutter

我有一个TextEditingController地方,如果用户点击它填写信息的按钮.我似乎无法弄清楚如何更改Textfield或内部的文本TextFormField.有解决方案吗?

rfa*_*ias 49

只是设置的问题

_controller.text = "New value";
Run Code Online (Sandbox Code Playgroud)

是光标将重新定位到开头(在材料的 TextField 中)。使用

_controller.text = "Hello";
_controller.selection = TextSelection.fromPosition(
    TextPosition(offset: _controller.text.length),
);
setState(() {});
Run Code Online (Sandbox Code Playgroud)

效率不高,因为它重建小部件的次数超过了必要的程度(在设置 text 属性和调用 setState 时)。

——

我相信最好的方法是将所有内容组合成一个简单的命令:

final _newValue = "New value";
_controller.value = TextEditingValue(
      text: _newValue,
      selection: TextSelection.fromPosition(
        TextPosition(offset: _newValue.length),
      ),
    );
Run Code Online (Sandbox Code Playgroud)

它适用于 Material 和 Cupertino Textfields。

  • 还有一个建议,在提供偏移量时,我们可以这样做: int currentOffset = _textEditingController.selection.base.offset; 然后提供 currentOffset 作为偏移量。这也将处理用户位于字符串之间的情况。 (7认同)
  • 这应该是正确的答案,因为它处理更改控制器的值和光标始终返回到文本开头的问题 (2认同)

Rao*_*che 47

只需更改text房产

    TextField(
      controller: txt,
    ),
    RaisedButton(onPressed: () {
        txt.text = "My Stringt";
    }),
Run Code Online (Sandbox Code Playgroud)

虽然txt只是一个TextEditingController

  var txt = TextEditingController();
Run Code Online (Sandbox Code Playgroud)

  • 使用此代码,光标将转到Field的左侧.无论如何还有吗? (13认同)
  • @rickdroio查看[这个答案](/sf/answers/4006290361/),它会将光标放在末尾。 (2认同)

Cop*_*oad 15

截屏:

在此处输入图片说明

  1. 创建一个TextEditingController

    final TextEditingController _controller = TextEditingController();
    
    Run Code Online (Sandbox Code Playgroud)
  2. 将其分配给您的TextFieldTextFormField

    TextField(controller: _controller)
    
    Run Code Online (Sandbox Code Playgroud)
  3. 要使用光标位置的按钮更新文本(假设文本字段中已经有文本):

    ElevatedButton(
      onPressed: () {
        final yourText = 'Hello World';
        _controller.value = _controller.value.copyWith(
          text: _controller.text + yourText,
          selection: TextSelection.collapsed(offset: _controller.value.selection.baseOffset + yourText.length,),
        );
      },
    )
    
    Run Code Online (Sandbox Code Playgroud)


Arr*_*ray 9

如果您以成员身份使用StatefulWidgetwith,则不会出现此问题。_controller听起来很奇怪,但从无状态转移到有状态效果很好(这是因为在文本编辑控制器的每个输入上都会重新绘制小部件,而不会保留状态)例如:

有状态:(工作中)

class MyWidget extends StatefulWidget {
  const MyWidget(
      {Key? key})
      : super(key: key);

  @override
  _MyWidgetState createState() =>
      _MyWidgetState();
}

class _MyWidgetState
    extends State<MyWidget> {
  late TextEditingController _controller;

  @override
  void initState() {
    super.initState();

    _controller = TextEditingController(text: "My Text");
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        TextField(
          controller: _controller,
        ),
      ],
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

无国籍:(问题)

class MyWidget extends StatelessWidget {
  const MyWidget(
      {Key? key})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        TextField(
          controller: TextEditingController(text: "My Text"),
        ),
      ],
    );
  }
}
Run Code Online (Sandbox Code Playgroud)


Muh*_*raf 8

第一件事

  TextEditingController MyController= new TextEditingController();
Run Code Online (Sandbox Code Playgroud)

然后将其添加到 init State 或任何 SetState

    MyController.value = TextEditingValue(text: "ANY TEXT");
Run Code Online (Sandbox Code Playgroud)


Dan*_*iel 7

_mytexteditingcontroller.value = new TextEditingController.fromValue(new TextEditingValue(text: "My String")).value;
Run Code Online (Sandbox Code Playgroud)

如果有人有更好的方法,这似乎可行,请随时告诉我。

  • 怎么样只是:_mytexteditingcontroller.text =“你的文字” (3认同)

Cha*_*air 6

您可以使用文本编辑控制器来操纵文本字段中的值。

var textController = new TextEditingController();
Run Code Online (Sandbox Code Playgroud)

现在,创建一个新的文本字段,并将其设置textController为该文本字段的控制器,如下所示。

 new TextField(controller: textController)
Run Code Online (Sandbox Code Playgroud)

现在,RaisedButton在代码中的任意位置创建一个,并在中的onPressed方法中设置所需的文本RaisedButton

new RaisedButton(
       onPressed: () {
          setState(() {
             textController.text = "New text";
          });
       }
    ),
Run Code Online (Sandbox Code Playgroud)

  • 更改 TextField 的文本时无需调用 setState (10认同)

Sur*_*gch 5

如果您只是想替换文本编辑控制器内的整个文本,那么这里的其他答案都可以。但是,如果您想以编程方式插入、替换所选内容或删除,则需要更多代码。

\n

制作您自己的自定义键盘是这种情况的一个用例。下面的所有插入和删除都是通过编程完成的:

\n

在此输入图像描述

\n

插入文本

\n

这里_controllerTextEditingController一个TextField.

\n
void _insertText(String myText) {\n  final text = _controller.text;\n  final textSelection = _controller.selection;\n  final newText = text.replaceRange(\n    textSelection.start,\n    textSelection.end,\n    myText,\n  );\n  final myTextLength = myText.length;\n  _controller.text = newText;\n  _controller.selection = textSelection.copyWith(\n    baseOffset: textSelection.start + myTextLength,\n    extentOffset: textSelection.start + myTextLength,\n  );\n}\n
Run Code Online (Sandbox Code Playgroud)\n

感谢Stack Overflow 的回答对此提供的帮助。

\n

删除文本

\n

有几种不同的情况需要考虑:

\n
    \n
  1. 有一个选择(删除选择)
  2. \n
  3. 光标位于开头(don\xe2\x80\x99t 执行任何操作)
  4. \n
  5. 其他任何内容(删除前一个字符)
  6. \n
\n

这是实现:

\n
void _backspace() {\n  final text = _controller.text;\n  final textSelection = _controller.selection;\n  final selectionLength = textSelection.end - textSelection.start;\n\n  // There is a selection.\n  if (selectionLength > 0) {\n    final newText = text.replaceRange(\n      textSelection.start,\n      textSelection.end,\n      \'\',\n    );\n    _controller.text = newText;\n    _controller.selection = textSelection.copyWith(\n      baseOffset: textSelection.start,\n      extentOffset: textSelection.start,\n    );\n    return;\n  }\n\n  // The cursor is at the beginning.\n  if (textSelection.start == 0) {\n    return;\n  }\n\n  // Delete the previous character\n  final newStart = textSelection.start - 1;\n  final newEnd = textSelection.start;\n  final newText = text.replaceRange(\n    newStart,\n    newEnd,\n    \'\',\n  );\n  _controller.text = newText;\n  _controller.selection = textSelection.copyWith(\n    baseOffset: newStart,\n    extentOffset: newStart,\n  );\n}\n
Run Code Online (Sandbox Code Playgroud)\n

完整代码

\n

您可以在我的文章《Flutter 中的自定义应用内键盘》中找到完整的代码和更多说明。

\n