TextFormField enters value backwards [Flutter] - flutter

As displayed in the GIF below, the TextFormField i am using is entering the values backwards. I have set the TextDirection property to ltr as well but it did not change anything. Other TextFormFields dont seem to be having this issue. The entered text is being sent back to another screen using Navigator.pop and it is sent in the same backwards manned.
Weird TextFormField
Code for the textFormField causing the issue:
TextFormField(
// validator: (String value) {
// return value.isEmpty ? "task must have a name" : null;
// },
textDirection: TextDirection.ltr,
maxLength: 100,
controller: newcontroller, // Just an ordinary TextController
onChanged: (value) {
setState(() {
newcontroller.text = value;
});
},
decoration: InputDecoration(
errorText: _validate // Just a boolean value set to false by default
? 'Value Can\'t Be Empty' : null,
labelText: "name of task"
),
style: TextStyle(height: 1.2, fontSize: 20, color: Colors.black87)
)

You don't have to set text in newcontroller.text when onChanged is called.
text enter in your TextFormField is assigned by default to newcontroller.
You are getting this error because for this piece of code,
So, try to remove below code
setState(() {
newcontroller.text = value;
});

you can send whatever you want in Navigator.pop(context,whtever you want to pass)
TextFormField(
// validator: (String value) {
// return value.isEmpty ? "task must have a name" : null;
// },
textAlign: TextAlign.end,
maxLength: 100,
controller: newcontroller, // Just an ordinary TextController
onChanged: (value) {
print(value);
},
decoration: InputDecoration(
errorText:
_validate // Just a boolean value set to false by default
? 'Value Can\'t Be Empty'
: null,
labelText: "name of task"),
style:
TextStyle(height: 1.2, fontSize: 20, color: Colors.black87),
),
MaterialButton(
onPressed: () {
Navigator.pop(context, newcontroller.text);
},
child: Text("GO BACK!"),
),

replace onChanged with onEditingComplete.
onEditingComplete: (value) {
newController.text = value;
FocusScope.of(context).unfocus(); //use this to dismiss the screen keyboard
}

Just remove the onChanged function. There is no need for it.

onChanged: (val) {
if (val.isNotEmpty) {
setState(() {
// remember to not add your controller value in onchanged
_messageController.text = val;
isShowSendButton = true;
});
} else {
setState(() {
// remember to not add your controller value in onchanged
_messageController.text = val;
isShowSendButton = false;
});
}

Unfortunately, onEditingComplete doesn't always fire (i.e. on a focus change), so if you are editing a text variable you can still use onChanged as the most reliable way to update changes, but you should not use the onChanged value parameter, nor should you use setState (which results in the reversed text).
TextEditingController tec = TextEditingController(text: myText);
return TextField(
controller: tec,
onChanged: (value) {
myText = tec.text;
},
);

Related

How to Restrict special characters like except numbers 0 to 9 in onchanged for textfield

The following is my onChanged method for phone number textfield. I want only numbers to be accepted. but it is accepted all characters except alphabets a-z and A-Z
onChanged: (value) {
if (value.contains(RegExp(r'[a-z,A-Z]')) ||
value.isEmpty) {
setState(() {
_isPhone = false;
});
make use of InputKeyboardtype
Only allow number input in flutter text field Flutter
TextFormField(
initialValue: _tip.toString(),
inputFormatters: [FilteringTextInputFormatter.digitsOnly],
keyboardType: TextInputType.number,
decoration: InputDecoration(
prefix: Text('\$'),
labelText: 'Enter Custom Tip Amount ',
hintStyle: TextStyle(fontWeight: FontWeight.w600)),
onChanged: (val) => setState(() => _tip = int.tryParse(val) ?? 0),
),
Use input formatter
inputFormatters: [FilteringTextInputFormatter.digitsOnly],
Add this to your textfield and/or if you wish to check the number in on changed then you can try this
onChanged : (value) {
final valueNum = num.tryParse(value);
if(valueNum == null) {
//Not phone
return;
}
//Is phone. Setstate here as phone true
}

How to show AllertDialog if Textfield is empty in Flutter

I'm building an application on flutter and i have a problem because of the list.
In flutter, I made an empty list, in this list filling by users. An assignment takes place when the user enters a value, but this always happens when a value is entered. If no value is entered, it reflects the previous value in the list. What I want is for the program to create an Alertdialog if no string value is entered in the TextField.
Here is my List (TaskData)
enter image description here
And my TextField
enter image description here
Try adding a errorText:
Full example
final _text = TextEditingController();
TextField(
controller: _text,
decoration: InputDecoration(
labelText: 'Enter the Value',
errorText: _validate ? 'Value Can\'t Be Empty' : null, //here
),
),
Inside a Button use:
bool _validate = false;
RaisedButton(
onPressed: () {
setState(() {
_text.text.isEmpty ? _validate = true : _validate = false;
});
},
child: Text('Submit')
textColor: Colors.white,
color: Colors.blueAccent,
),
try this in your addTask method
void addTask() {
if (name = '') showMyAlertDialog();
else {nameList.add(name);
notifyListeners();}
name = '';
}

TextFormField cuts last character input

I'm having a weird issue with a TextFormField, it cuts off the last input. If I type something like hello, it's gonna cut off the o and save hell. if I give it a space like hello , it's gonna save hello. Has anyone been through the same issue and know how to fix it?
here is my code:
String messageText;
_ChatScreenState() {
_formKey = GlobalKey<FormState>();
_scrollController = ScrollController();
messageText = "";
}
Widget messageTextField() {
return SizedBox(
width: deviceWidth * 0.55,
child: TextFormField(
style: TextStyle(color: Colors.white),
validator: (text) {
if (text.isEmpty) {
return 'Digite sua mensagem';
}
return null;
},
onChanged: (text) {
_formKey.currentState.save();
},
onSaved: (text) {
setState(() {
messageText = text;
});
},
cursorColor: Colors.white,
decoration: InputDecoration(
border: InputBorder.none,
hintText: 'Digite uma mensagem',
hintStyle: TextStyle(color: Colors.white.withAlpha(100))),
autocorrect: false,
),
);
}
I do not think save() is intended to be used with onChanged.
If you place a button somewhere and perform _formKey.currentState.save(); within onPressed of the button, you should see the entire text value being saved.

how do you do the textfield when first opened, will it give you the value of thousand (,)?

how do you do the textfield when first opened, will it give you the value of thousand (,)?
how do I do the textfield when it will be opened the first time, will give the value of thousand (,) ?, this TextField will give (,) when I give a new input.
I was a little bit confused at the controller: priceController
because the controller can not be converted into a currency value
final priceController = TextEditingController();
enabled: !_isOwned,
controller: priceController,
cursorColor: Theme.of(context).primaryColor,
keyboardType: TextInputType.number,
maxLength: 11,
inputFormatters: [
NumericTextFormatter(),
],
decoration: InputDecoration(
hintText: '0',
prefixText: 'Rp ',
counterText: '',
errorText:
!_validate ? 'Biaya harus diisi' : null,
),
onChanged: (value) {
print("VALUE $value ${value.length}");
if (value.length > 0) {
setState(() {
_validate = true;
});
} else {
setState(() {
_validate = false;
});
}
},
)
If you want to have a default value on your TextFormField, you can do it in two different ways.
You can set an initialValue on the Widget:
TextFormField(
initialValue: '1000',
),
Or you can define it on the TextEditingController:
TextEditingController priceController = TextEditingController(text: '1000');

how to save input value of text form field as soon as type completed in flutter

i have a text form field that user enter to it a code that sent to it and i must decide if input code is true or false but can not save value of text form field i have a button like it
GestureDetector(
onTap: () {
_formKey.currentState.save();
setState(() {
inputcode=key.getter("sms");
print(inputcode);
print(verifycode);
});
},
child: inputcode==verifycode?send:
VirtualBotton("please enter correct code","ok",40.0,10.0,width: 120.0,),
)
but i must press two times button for done this work
in this code when i enter correct code first run virtual Button class and second times that i press button run correct code how to run on tap before build child in button this is text form field
TextFormField(
inputFormatters: [
new LengthLimitingTextInputFormatter(30),
],
onSaved: (value) {
key.setter(id, value);
},
textAlign: alignment == null ? TextAlign.right : alignment,
maxLines: maxlines==null?1:maxlines,
style: TextStyle(
fontSize: 14,
),
textDirection: TextDirection.rtl,
controller: ctrl,
validator: (String value) {
if (value.isEmpty) {
return "";
}
return null;
},
decoration: InputDecoration(
border: InputBorder.none,
contentPadding: const EdgeInsets.all(0.0),
hintText: _hint,
errorStyle: TextStyle(height: 0),
hintStyle: TextStyle(
color: hintconlor == null ? Colors.grey : hintconlor,
fontSize: hintsize == null ? 13 : hintsize)),
),
);
you can add TextEditingController and listen to controller changes. Here are docs with examples