Set*_*add 67
(来自邮件列表.我没有想出这个答案.)
class _FooState extends State<Foo> {
TextEditingController _controller;
@override
void initState() {
super.initState();
_controller = new TextEditingController(text: 'Initial value');
}
@override
Widget build(BuildContext context) {
return new Column(
children: <Widget>[
new TextField(
// The TextField is first built, the controller has some initial text,
// which the TextField shows. As the user edits, the text property of
// the controller is updated.
controller: _controller,
),
new RaisedButton(
onPressed: () {
// You can also use the controller to manipuate what is shown in the
// text field. For example, the clear() method removes all the text
// from the text field.
_controller.clear();
},
child: new Text('CLEAR'),
),
],
);
}
}
Run Code Online (Sandbox Code Playgroud)
Ken*_*ent 48
我在这里看到了很多这样做的方法。但是,我认为这比其他答案更有效或至少更简洁。
TextField(
controller: TextEditingController(text: "Initial Text here"),
)
Run Code Online (Sandbox Code Playgroud)
Sam*_*ani 44
您可以使用a TextFormField
代替TextField
,并使用该initialValue
属性.例如
TextFormField(initialValue: "I am smart")
Run Code Online (Sandbox Code Playgroud)
Deb*_*kar 24
这可以使用TextEditingController
.
要获得初始值,您可以添加
TextEditingController _controller = TextEditingController(text: 'initial value');
Run Code Online (Sandbox Code Playgroud)
或者
如果您正在使用,TextFormField
您在initialValue
那里有一个财产。它基本上initialValue
自动将这个提供给小部件。
TextFormField(
initialValue: 'initial value'
)
Run Code Online (Sandbox Code Playgroud)
要清除文本,您可以使用
_controller.clear()
方法。
Nir*_*sar 22
如果您正在使用TextEditingController,则将文本设置为它,如下所示
TextEditingController _controller = new TextEditingController();
_controller.text = 'your initial text';
final your_text_name = TextFormField(
autofocus: false,
controller: _controller,
decoration: InputDecoration(
hintText: 'Hint Value',
),
);
Run Code Online (Sandbox Code Playgroud)
如果您没有使用任何TextEditingController,那么您可以直接使用initialValue,如下所示
final last_name = TextFormField(
autofocus: false,
initialValue: 'your initial text',
decoration: InputDecoration(
hintText: 'Last Name',
),
);
Run Code Online (Sandbox Code Playgroud)
有关更多参考TextEditingController
vov*_*ost 12
您不必在小部件作用域中定义单独的变量,只需内联即可:
TextField(
controller: TextEditingController()..text = 'Your initial value',
onChanged: (text) => {},
)
Run Code Online (Sandbox Code Playgroud)
TextEdittingController _controller = new TextEdittingController(text: "your Text");
Run Code Online (Sandbox Code Playgroud)
或者
@override
void initState() {
super.initState();
_Controller.text = "Your Text";
}
Run Code Online (Sandbox Code Playgroud)
小智 6
class _YourClassState extends State<YourClass> {
TextEditingController _controller = TextEditingController();
@override
void initState() {
super.initState();
_controller.text = 'Your message';
}
@override
Widget build(BuildContext context) {
return Container(
color: Colors.white,
child: TextFormField(
controller: _controller,
decoration: InputDecoration(labelText: 'Send message...'),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
如果您使用,TextEditingController
请在班级中使用该线路。
TextEditingController txtController = TextEditingController(text: 'Initial value')
TextField(
controller: txtController,
);
Run Code Online (Sandbox Code Playgroud)
如果你不使用TextEditingController
就去initialValue
TextFormField(
initialValue: 'your initial text',
);
Run Code Online (Sandbox Code Playgroud)
完整代码
class _TestScreen extends State<Test> {
TextEditingController _controller;
@override
void initState() {
super.initState();
_controller = new TextEditingController(text: 'Initial value');
}
@override
Widget build(BuildContext context) {
return new Column(
children: <Widget>[
TextFormField( // First method
initialValue: "Initial text"
),
TextField( // Second method
controller: _controller, // Controller has the initial value
),
],
);
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
如果要TextInput
按照@MRT在接受的答案的注释中的要求处理多个s,则可以创建一个采用初始值并返回TextEditingController
如下内容的函数:
initialValue(val) {
return TextEditingController(text: val);
}
Run Code Online (Sandbox Code Playgroud)
然后,将此函数设置为的控制器,TextInput
并在其中提供其初始值,如下所示:
controller: initialValue('Some initial value here....')
Run Code Online (Sandbox Code Playgroud)
您可以对其他TextInput
s 重复此操作。
课堂内,
final usernameController = TextEditingController(text: 'bhanuka');
Run Code Online (Sandbox Code Playgroud)
文本域,
child: new TextField(
controller: usernameController,
...
)
Run Code Online (Sandbox Code Playgroud)
小智 5
简单有效的方法
将控制器分配给您的TextFormField
或TextField
您initState
可以将其初始化为初始值,如下所示
_controller = TextEditingController(text: 'Your initial value');
Run Code Online (Sandbox Code Playgroud)
由于没有一个答案提到它,因此TextEditingController
应在使用后丢弃。如:
class MyWidget extends StatefulWidget {
const MyWidget({Key? key}) : super(key: key);
@override
MyWidgetState createState() => MyWidgetState();
}
class MyWidgetState extends State<MyWidget> {
final myController = TextEditingController(text: "Initial value");
@override
Widget build(BuildContext context) {
return TextField(
controller: myController,
);
}
@override
void dispose() {
// dispose it here
myController.dispose();
super.dispose();
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
35309 次 |
最近记录: |