Flutter TextFormField controller into Text - flutter

i have a problem :( I have a Detailform with Text Widget wehre im getting values from my DB.
I have one TextFormField with a controller, because i need to set the actual price, after i submitted it, the new value will updated in a TextValueField, but i want it to show on a normal Text widget.
i hope you can understand my problem. but here my code
TextField(
decoration: InputDecoration(labelText: 'actual cost'),
controller: _profitController,
keyboardType:
TextInputType.numberWithOptions(decimal: true),
onSubmitted: (val) {
double amount =
double.parse(selectedEntry.amount); // 1 value
double aktPrice =
double.parse(val); // 2 value from this textfield
double profit = aktPrice * amount; // calc
_endProfitController.text = profit.toString();
newProfit = profit;
},
),
TextFormField(
readOnly: true,
decoration: InputDecoration(labelText: "profit"),
controller:
_endProfitController, //this is getting updated with the controller
textAlign: TextAlign.left,
style: TextStyle(
fontSize: 20,
color: Colors.grey[700],
),
),
Text(
'test profit: ' +
_endProfitController.text.toString() + newProfit.toString() +
' €', // this is not getting updated, and newProfit says "Null"...
textAlign: TextAlign.left,
style: TextStyle(
fontSize: 20,
color: Colors.grey[700],
),
),
alright, how can i get the value in my Text Widget ?
I tried to setState in the onSubmitted but then the controller also isn't updating.
I tried to declare a new double newProfit but after submit it still gives me null back....
hope someone can help me :)
best regards
And thanks for your time :)

Its not updating because the widgets are already built when you change the text value of the controller if you are creating the form inside a stateful widget you can call setState() inside the onChanged so the value of the field updates the String you'll use to show the text, something like this.
TextFormField(
decoration: InputDecoration(labelText: "profit"),
controller:
_endProfitController, //this is getting updated with the controller
textAlign: TextAlign.left,
onChanged: (value) {
setState(() {
valueText = _endProfitController.text;
});
print(valueText);
},
style: TextStyle(
fontSize: 20,
color: Colors.grey[700],
),
),
Text(
'test profit: ' +
valueText +
' €', // this is not getting updated, and newProfit says "Null"...
style: TextStyle(
fontSize: 20,
color: Colors.grey[700],
),
),
The variable that will get the value of the controller's text has to be outside the build method
String valueText = '';
If you are not building this inside a stateful widget you can look at the Provider package to update a value that has no state

Related

How do I use the credit card date format mm/yy in FLUTTER

child: new TextFormField(
maxLength:5,
controller:
TextEditingController(text: donation.date),
onChanged: (value) {
donation.date = value;
},
decoration: InputDecoration(
suffixText: '',
suffixStyle: TextStyle(
color: Color(0xffde0486),
),
labelText: 'Expiry Date',
border: OutlineInputBorder(),
),
keyboardType: TextInputType.number,
textInputAction: TextInputAction.done,
style: TextStyle(color: Colors.white),
autofocus: true,
),
I have this piece of code for a credit card that I am implementing in Flutter. I want to put the mm/yy format on the textfield. When the user has entered the month, the text field should automatically show the '/'. How do I do this?
You can achieve your result using the onChange method. You can check the length of the input and insert a '/' into the text if it is 2 i.e. dd
You need to store the controller in a seperate variable, because we want to use it later for inserting '/':
var _controller = new TextEditingController(text: donation.date);
Add this _controller and onChange callback to your TextFormField:
TextFormField(
controller: _controller, //<-- Add controller here
onChanged: (value) {
if(value.length == 2) _controller.text += "/"; //<-- Automatically show a '/' after dd
donation.date = value;
},
...
),
Yo can try this package flutter_multi_formatter to format the Expiration Date field as well as the credit card number field.
Update !
I just found a package: flutter_masked_text2 that can help you solve the problem!
var controller = new MaskedTextController(mask: '00/00');

Variable value gets null outside setState() in Flutter

TextField(
style: TextStyle(
fontFamily: "Averta",
fontWeight: FontWeight.bold,
fontSize: 16,
color: Colors.black),
enableSuggestions: true,
onChanged: (value) {
setState(() {
final title = value;
print(title);
});
},
decoration: InputDecoration(
border: InputBorder.none,
contentPadding:
EdgeInsets.all(18),
hintText: "Enter Task Title",
hintStyle: TextStyle(
fontFamily: "Averta",
fontWeight: FontWeight.normal,
fontSize: 12,
color: Colors.grey.shade300),
),
),
Here,I have a title variable which I update with onChanged() function.
Here the print() function shows exact change in value which I do in TextField.
But I don't want to pass it here, I want to take some more data and then pass it when I click the CREATE button here-
InkWell(
onTap: () {
print(title);
setState(() {
DatabaseHelper _dbhelper =
DatabaseHelper();
Task _newTask = Task(
title: title,
year: year,
month: month,
day: daye,
hour: hour,
minute: minute,
weekday: weekday);
_dbhelper
.insertTask(_newTask);
});
}
Here the print statement shows "null",but I want to use the value which was updated above in TextField.
How can I achieve this in Flutter?
This line creates a new variable called title and sets it:
final title = value;
You want to access the already existing variable:
title = value;
That should work.

TextFormFields not getting focus - Flutter

Currently I am working on login screen.
Below is how my screen must work.
User have 2 options to login/register. With email or mobile number.
User can select how they want to login/register from the option given on the screen.
All my TextFormFields are getting displayed as per the choice made by user (they are not visible together).
So, the difficulty I am facing here is my TextFormFields are not getting focused as per user selection. Default focus must be on mobile number field which is working fine but when user selects email option then focus is not going on email textbook and that's why keyboard also not getting displayed.Same happening with passport textbox which must be focused once user enters email address.
I have attached video link for more clarity.
https://drive.google.com/file/d/1GbOeJhftI4hIkT4RTROoQ1DoYRoWh5gu/view?usp=sharing
below is the code for all 3 Textformfields.Same code for other two textfields with controller and focus node diffrent.
final emailInputController = TextEditingController();
final mobileInputController = TextEditingController();
final passwordInputController = TextEditingController();
final emailFocusNode = FocusNode();
final mobileFocusNode = FocusNode();
final passwordFocusNode = FocusNode();
TextFormField(
autofocus: true,
focusNode: passwordFocusNode,
onFieldSubmitted: (a) {
_handleSubmittedMobile(a);
},
keyboardType: TextInputType.text,
controller: passwordInputController,
style: TextStyle(
fontSize: 20.0,
color: Theme.of(context)
.primaryColorDark,
fontWeight: FontWeight.bold,
),
)),
TextFormField(
autofocus: true,
focusNode: mobileFocusNode,
onFieldSubmitted: (a) {
_handleSubmittedMobile(a);
},
keyboardType: TextInputType.text,
controller: mobileInputController,
style: TextStyle(
fontSize: 20.0,
color: Theme.of(context)
.primaryColorDark,
fontWeight: FontWeight.bold,
),
)),
TextFormField(
autofocus: true,
focusNode: emailFocusNode,
onFieldSubmitted: (a) {
_handleSubmittedMobile(a);
},
keyboardType: TextInputType.text,
controller: emailInputController,
style: TextStyle(
fontSize: 20.0,
color: Theme.of(context)
.primaryColorDark,
fontWeight: FontWeight.bold,
),
)),
How i can achieve this working?
thanks in advance.

How to add red asterisk in label of TextFormField In Flutter

As we are not able to make widget like RichText/Text Span For styling TextFormField,
Can anyone help me out regarding this...
Now Getting:-
Expected Result:-
How we can achieve a result like this?
Simplest way, but not exactly equals:
TextField(
decoration: InputDecoration(
hintText: 'Ciao',
suffixText: '*',
suffixStyle: TextStyle(
color: Colors.red,
),
),
),
Or create a custom TextField to use hint as Widget instead of String
You can use my two customized files:
input_decorator.dart
text_field_custom.dart
TextFieldCustom(
hintText: Text.rich(
TextSpan(
text: 'FIRST NAME',
children: <InlineSpan>[
TextSpan(
text: '*',
style: TextStyle(color: Colors.red),
),
],
style: TextStyle(color: Colors.black54),
),
),
),
Try these two files I had same requirement.
Just add attribute isMandate: true in CInputDecoration and use CTextField.
CTextField(
...
decoration: new CInputDecoration(
isMandate: true,
...
))
https://github.com/sumit1954/Flutter/blob/master/CInputDecorator.dart
https://github.com/sumit1954/Flutter/blob/master/CTextField.dart
Place this code in TextFormField or TextField
this label works as an hint property
label: RichText(
text: TextSpan(
text: 'Full Name',
style: const TextStyle(
color: Colors.grey),
children: [
TextSpan(
text: ' *',
style: TextStyle(
color: Colors.red,
)
)
]
),
floatingLabelBehavior:FloatingLabelBehavior.never,
look at above screenshot of label parameter holding a Widget not a String without any error
Achieved this in hard fast way. Replace input_decorator.dart with below code:
https://github.com/neal786y/InputDecoratorForMandatoryFields/blob/master/input_decorator.dart
In your InputDecoration scope add a property "isMandatoryField: true"
Worked for me on temporary basis.
With the latest version of flutter you can also send a widget in label parameter. So, something like this is possible
label: Row(
mainAxisSize: MainAxisSize.min,
children: [
Text(label.replaceAll('*', '')),
label.contains('*')
? Text(
'*',
style: TextStyle(color: Colors.red),
)
: Container(),
],
),
Use label property inside InputDecorator
See this answer https://stackoverflow.com/a/72209425/8913302

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