I already have the value displayed by
lattextController.text = addr_data!.latitude!;
lngtextController.text = addr_data!.longitude!;
then i get data value sent from another page but when i update data It updates to the old value. It's not a new value. But new information was sent to textformfield It has to type something to update the latest information. Please help.
if (widget.lng != null && widget.lng!.isNotEmpty) {
lngtextController.text = widget.lng!;
print(widget.lng!);
}
if (widget.lat != null && widget.lat!.isNotEmpty) {
lattextController.text = widget.lat!;
print(widget.lat);
}
}
This is textformfield
Widget lngTextFormField(String label, String keyword, String text) {
lngtextController.selection = TextSelection.collapsed(offset: lngtextController.text.length);
return TextFormField(
controller: lngtextController,
onChanged: (value) {
saveAd(value, keyword);
},
decoration: InputDecoration(
labelText: label,
labelStyle: TextStyle(
fontFamily: 'supermarket',
fontSize: 16,
),
isDense: true,
),
);
}
Related
How to automatically change label text color of TextFormField in flutter?
Try this:
class MyViewState extends State<MyView> {
bool _invalidValue = false;
...
TextFormField(
autovalidateMode: AutovalidateMode.always,
decoration: InputDecoration(
icon: const Icon(Icons.person),
hintText: 'What is your name?',
labelText: 'Name',
labelStyle:
TextStyle(color: _invalidValue ? Colors.red : Colors.blue),
),
validator: (String? value) {
final b = (value != null && value.contains('#'));
SchedulerBinding.instance!.addPostFrameCallback((timestamp) {
if (_invalidValue != b) {
setState(() {
_invalidValue = b;
});
}
});
return b ? 'Do not use the # char.' : null;
},
)
The b variable is for preventing setState to be called every time, only when needed
I am working on making some label texts for Text Fields respond to if certain text fields have text in them. Example being if fields B and C are blank, the labelText for field A will have an asterisk before it ("* Label"). Or if field A is blank the labelText for B and C will have an asterisk before it ("* Label"). I have it sort of working currently, but to have the correct asterisk appear after I type in a field I have to swipe the drawer away Im doing this in and reopen it. Kinda like setting the state so it knows that theres new stuff to display. How would I get it to change the correct label right when I type instead of having to do it manually? Thanks!
Field A
TextField(
controller: Data.NumberEditingController,
obscureText: false,
decoration: InputDecoration(
fillColor: Colors.white,
border: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(12.0)),
),
labelText: requiredNumber(),
labelStyle: TextStyle(
color: Color.fromRGBO(105, 105, 105, 10),
)),
),
Field B
TextField(
controller: Data.NameEditingController,
obscureText: false,
decoration: InputDecoration(
fillColor: Colors.white,
border: OutlineInputBorder(
borderRadius:
BorderRadius.all(Radius.circular(12.0)),
),
labelText: requiredName(),
labelStyle: TextStyle(
color: Color.fromRGBO(105, 105, 105, 10),
)),
),
Field C
TextField(
controller: Data.addressEditingController,
obscureText: false,
decoration: InputDecoration(
fillColor: Colors.white,
border: OutlineInputBorder(
borderRadius:
BorderRadius.all(Radius.circular(12.0)),
),
labelText: requiredAddress(),
labelStyle: TextStyle(
color: Color.fromRGBO(105, 105, 105, 10),
)),
),
These are the functions I use to check for any text in the controllers so they can return the correct label to use. As stated above they work correctly but only upon dismissing the drawer and opening it again so everything is refreshed and I want it to be automatic
requiredNumber() {
if (Data.addressEditingController.text == "") {
return "* Number";
}
else if (Data.nameEditingController.text == "") {
return "* Number";
}
else {
return "Number";
}
}
requiredName() {
if (Data.numberEditingController.text == "") {
return "* Name";
}
else {
return "Name";
}
}
requiredAddress() {
if (Data.numberEditingController.text == "") {
return "* Address";
}
else {
return "Address";
}
}
Best solution and cleanest for your case would be to put the logic inside the onChanged property of the TextField. I'll give an example using your first function:
Change this:
requiredName() {
if (Data.numberEditingController.text == "") {
return "* Name";
}
else {
return "Name";
}
}
into this:
String requiredNumber = "Number";
String requiredName = "Name";
String Address = "Address";
//and inside your TextField for the Number for example, use onChanged
onChanged: (value) {
if (value == '') {
setState(() {
requiredName = '* Name';
});
} else {
setState(() {
requiredName = 'Name';
});
}
}
define String variables for all your labels in your stateful widget:
String numberLabel = '';
Add listeners on all your textControllers during initState like so:
#override
void initState() {
super.initState();
// Start listening to changes.
addressEditingController.addListener(setState((){numberLabel=requiredNumber()})); // Im not sure here callback may be wrapped in () {}
}
Then use this variables in your labelText properties:
labelText: numberLabel,
Remove registered listeners before dispose method:
#override
void dispose() {
addressEditingController.removeListener(setState((){numberLabel=requiredNumber()}));
addressEditingController.dispose();
super.dispose();
}
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.
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;
},
);
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