How do I supply an initial value to a text field?
FlutterDartTextfieldFlutter Problem Overview
I'd like to supply an initial value to a text field and redraw it with an empty value to clear the text. What's the best approach to do that with Flutter's APIs?
Flutter Solutions
Solution 1 - Flutter
You can use a TextFormField
instead of TextField
, and use the initialValue
property. for example
TextFormField(initialValue: "I am smart")
Solution 2 - Flutter
(From the mailing list. I didn't come up with this answer.)
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'),
),
],
);
}
}
Solution 3 - Flutter
You don't have to define a separate variable in the widget scope, just do it inline:
TextField(
controller: TextEditingController()..text = 'Your initial value',
onChanged: (text) => {},
)
Solution 4 - Flutter
I've seen many ways of doing this on here. However I think this is a little more efficient or at least concise than the other answers.
TextField(
controller: TextEditingController(text: "Initial Text here"),
)
Solution 5 - Flutter
>If you are using TextEditingController then set the text to it, like below
TextEditingController _controller = new TextEditingController();
_controller.text = 'your initial text';
final your_text_name = TextFormField(
autofocus: false,
controller: _controller,
decoration: InputDecoration(
hintText: 'Hint Value',
),
);
>and if you are not using any TextEditingController then you can directly use initialValue like below
final last_name = TextFormField(
autofocus: false,
initialValue: 'your initial text',
decoration: InputDecoration(
hintText: 'Last Name',
),
);
> For more reference TextEditingController
Solution 6 - Flutter
This can be achieved using TextEditingController
.
To have an initial value you can add
TextEditingController _controller = TextEditingController(text: 'initial value');
or
If you are using TextFormField
you have a initialValue
property there. Which basically provides this initialValue
to the widget automatically.
TextFormField(
initialValue: 'initial value'
)
To clear the text you can use
_controller.clear()
method.
Solution 7 - Flutter
If you want to handle multiple TextInput
s as asked by @MRT in the comment to the accepted answer, you can create a function that takes an initial value and returns a TextEditingController
like this:
initialValue(val) {
return TextEditingController(text: val);
}
Then, set this function as the controller for the TextInput
and supply its initial value there like this:
controller: initialValue('Some initial value here....')
You can repeat this for the other TextInput
s.
Solution 8 - Flutter
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...'),
),
);
}
}
Solution 9 - Flutter
TextEdittingController _controller = new TextEdittingController(text: "your Text");
or
@override
void initState() {
super.initState();
_Controller.text = "Your Text";
}
Solution 10 - Flutter
inside class,
final usernameController = TextEditingController(text: 'bhanuka');
TextField,
child: new TextField(
controller: usernameController,
...
)
Solution 11 - Flutter
Easy and Efficient way
Assign controller to your TextFormField
or TextField
and in initState
you can initialise it to the initial value like this
_controller = TextEditingController(text: 'Your initial value');
Solution 12 - Flutter
Since none of the answers mention it, the TextEditingController
should be disposed off after use. As in:
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();
}
}
Solution 13 - Flutter
You can do all of the above things but, if you want the API to show your data when its's get loaded it shows up like profile page. so here's the code:
TextEditingController _nameController = TextEditingController(); // initialize the controller
// when API gets the data, do this:
_nameController.text = response.data.fullName; or _nameController.text = "Apoorv Pandey"
I hope it clears everything. Happy coding!
Solution 14 - Flutter
- When you are Using TextEditingController
If you use TextEditingController, set its text field to the desired value
TextEditingController txtController = TextEditingController()..text = 'Your initial text value';
TextField( controller: txtController ..... )
2) When you are Not Using TextEditingController
If you are not using the TextEditingContller, use the initialValue field directly from the TextField widget:
TextFormField( initialValue: "Your initial text value" )