Reputation: 7948
I have set value in dynamic textfield
through the controller but it does not allow me to edit value only cursor is moving around the entered text.
I have tried to set value in this way
shippingNameController.value = shippingNameController.value.copyWith(text:name);
But before the above solution I had tried this, I'm having dynamic textfield named as Billing name and I want to update the value of billing name in the shipping name.
shippingNameController.text=name;
this was not working when I have used this it was showing cursor at the start of textfield and not updating the value.
new TextField(
style: TextStyle(fontFamily: 'semibold',color: MyColors.colorPrimary),
keyboardType:field.type=='STR'||field.type=='EMAIL'? TextInputType.text:TextInputType.number,
textInputAction: TextInputAction.done,
decoration: InputDecoration(contentPadding:EdgeInsets.all(8.0),
hintStyle: TextStyle(fontFamily: 'semibold',color: MyColors.colorPrimary),
border: InputBorder.none,),
controller: shippingNameController,
onChanged: (String val) {
enteredValue(field,val);
setState(() {
shippingNameController.text = val;
sShppingfieldList[p].fieldvalue=val;
});
},),
Upvotes: 2
Views: 9588
Reputation: 757
Use controller with text
final TextEditingController _email = TextEditingController(text: '');
And set the data on controller
setState: (){
_email.text = "New Text";
}
Code of textfield:
TextFormField(
controller: _email,
textAlign: TextAlign.center,
autofocus: false,
style: TextStyle(
color: green3,
fontWeight: FontWeight.bold,
fontFamily: 'Roboto',
fontSize: 16.sp),
decoration: InputDecoration(
hintText: 'Email Id',
hintStyle: TextStyle(
color: placeholder_color,
fontSize: 16.sp,
fontFamily: 'Roboto'),
counterText: "",
contentPadding: EdgeInsets.fromLTRB(
20.0, 10.0, 20.0, 10.0),
border: InputBorder.none,
),
),
Upvotes: 0
Reputation: 1422
onSaved: (value){
setState(() {
_state = value
},
onChanged:(v){
_state = v;
print(_state)
},
This worked for me.
Upvotes: 1
Reputation: 265
Have you adding listener to the controller. Add the full code.
Upvotes: 0
Reputation: 2623
Something like this should works:
child: TextField(
keyboardType: TextInputType.text,
onChanged: (name) {
setState((){
shippingNameController.text = name;
});
},
),
Upvotes: 3
Reputation: 1472
Try it this way
I think if you want to set text then you have to do like this
setState((){
shippingNameController.text = "abc";
});
Upvotes: 0