我有一个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。
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)
Cop*_*oad 15
截屏:
创建一个TextEditingController:
final TextEditingController _controller = TextEditingController();
Run Code Online (Sandbox Code Playgroud)
将其分配给您的TextField或TextFormField:
TextField(controller: _controller)
Run Code Online (Sandbox Code Playgroud)
要使用光标位置的按钮更新文本(假设文本字段中已经有文本):
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)
如果您以成员身份使用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)
第一件事
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)
_mytexteditingcontroller.value = new TextEditingController.fromValue(new TextEditingValue(text: "My String")).value;
Run Code Online (Sandbox Code Playgroud)
如果有人有更好的方法,这似乎可行,请随时告诉我。
您可以使用文本编辑控制器来操纵文本字段中的值。
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)
如果您只是想替换文本编辑控制器内的整个文本,那么这里的其他答案都可以。但是,如果您想以编程方式插入、替换所选内容或删除,则需要更多代码。
\n制作您自己的自定义键盘是这种情况的一个用例。下面的所有插入和删除都是通过编程完成的:
\n\n这里_controller是TextEditingController一个TextField.
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}\nRun Code Online (Sandbox Code Playgroud)\n感谢Stack Overflow 的回答对此提供的帮助。
\n有几种不同的情况需要考虑:
\n这是实现:
\nvoid _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}\nRun Code Online (Sandbox Code Playgroud)\n您可以在我的文章《Flutter 中的自定义应用内键盘》中找到完整的代码和更多说明。
\n