Set Text Field value equal to double - flutter

I have a text field that inputs a number and want to set that value equal to a double, but whenever I try it does nothing and I am not sure why.
Below is how the textfield is implemented
TextField(
keyboardType: TextInputType.number,
controller: _incomeController,
style: const TextStyle(
color: Colors.black,
),
decoration: const InputDecoration(
filled: true,
fillColor: Colors.white,
hintText: 'Balance',
hintStyle: TextStyle(color: Colors.grey),
border: OutlineInputBorder(
borderRadius: BorderRadius.all(
Radius.circular(10.0),
),
borderSide: BorderSide.none,
),
),
),
This is how I am trying to convert the text field to the double income.
income = _incomeController.text.trim() as double;

First seem like you are using wrong controller :
_balanceController instead of _incomeController
Second, you cannot cast string to double like that, instead try this:
income = double.tryParse(_balanceController.text.trim());

Related

how to use validator with two variables in one textformfield?

I'd like to ask on if there are anyway on how to compare in a conditional statement the two variable. As you can see, couponSalePriceCtrlr and couponOrigPriceCtrlr. I'd like to validate that the user's input in sale price SHOULD not be greater than the original price, but it seems like the validator accepts only the (value) parameter and a String data type.
Widget editCouponSalePriceWidget(couponSalePriceCtrlr, couponOrigPriceCtrlr) {
// converted the variable parameters into double data type
double convertedSalePrice = double.parse(couponSalePriceCtrlr);
double convertedOrigPrice = double.parse(couponOrigPriceCtrlr);
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 0.0),
child: TextFormField(
style: const TextStyle(fontFamily: 'Poppins', fontSize: 13),
controller: couponSalePriceCtrlr,
keyboardType: TextInputType.number,
decoration: InputDecoration(
suffixText: "*",
suffixStyle: TextStyle(color: Colors.red),
labelText: 'Sale Price',
labelStyle: const TextStyle(
fontSize: 15, fontFamily: 'Poppins', color: Color(0xFF152C4C)),
isDense: true,
prefixIcon: const Icon(Icons.person),
enabledBorder: OutlineInputBorder(
borderSide: const BorderSide(color: Color(0xFFCECECE)),
borderRadius: BorderRadius.circular(12),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
borderSide: const BorderSide(color: Color(0xFFCECECE)),
),
hintText: 'Orig Price',
fillColor: const Color(0xFFFEFEFE),
filled: true,
),
// however the validator only accepts, a string data type.
validator: (convertedSalePrice,convertedOrigPrice) {
if (convertedSalePrice!.isEmpty ||
!RegExp(r'[0-9]+[,.]{0,1}[0-9]*').hasMatch(convertedSalePrice)) {
return "Please enter a valid original price.";
} else {
return null;
}
},
),
);
}
I assume you have 2 TextFormField, one for original price, another for sale price. Of course it is String type, that's the rule :) Therefore you need to convert it to integer/double type. If your keyboardType is number, it is unnecessary to check user's input is string type, else do it.
TextFormField(
controller: couponOrigPriceCtrlr,
keyboardType: TextInputType.number,
)
TextFormField(
controller: convertedSalePrice,
keyboardType: TextInputType.number,
validator: (saleStr) {
double originalDouble = double.parse(couponOrigPriceCtrlr.text);
double saleDouble = double.parse(saleStr.text);
// check what ever you want here
// ...
}
)

Text inside textform field is padded up

I am observing that the text for the below form field somehow padded up. Also the cursor has become very small like a dot. Any ideas ?
I want the text field height same as the right side round button
final border = OutlineInputBorder(
borderSide: BorderSide(
color: Colors.transparent,
),
borderRadius: const BorderRadius.all(
const Radius.circular(
10.0,
),
),
);
return TextFormField(
controller: controller,
onChanged: onChanged,
validator: validator,
autovalidateMode: AutovalidateMode.onUserInteraction,
cursorColor: Theme.of(context).highlightColor,
style: GoogleFonts.poppins(
color: Theme.of(context).highlightColor,
fontSize: 16,
fontWeight: FontWeight.w300,
height: 0.1,
),
textInputAction: TextInputAction.done,
decoration: InputDecoration(
fillColor: Theme.of(context).primaryColorLight,
filled: true,
enabledBorder: border,
focusedBorder: border,
border: border,
labelText: labelText,
// remove label while in focus
floatingLabelBehavior: FloatingLabelBehavior.never,
labelStyle: GoogleFonts.poppins(
color: Theme.of(context).highlightColor,
fontSize: 15,
fontWeight: FontWeight.w300,
),
),
);
The reason for for the additional padding and the small cursor is the font height which you are setting to 0.1 (keep the default 1.0 if you don't want the additional padding).
You can fix the cursor issue by setting cursorHeight to match your original font size
Note: use cursorHeight only if you want to keep the height as 0.1

Flutter: How to show or hide a label/text widget when a textformfield get or lost focus?

I am new with flutter.
I am making a user registration form, I want to achieve the following visual effect:
When a TextFormField is normal on the form, it looks like this:
But I want the following, when the textformfield is in "focus". When the user is typing it looks like this:
This is my average textFormField
TextFormField(
initialValue: name,
onChanged: (val) {
setState(() {
name = val;
print(name);
});
},
decoration: InputDecoration(
hintText: "Nombres",
hintStyle: TextStyle(
fontSize: scalador.setSp(22) * 2.63,
color: Color(0xFF949494)),
),
style: TextStyle(
color: Color(0xFF242427),
fontSize: scalador.setSp(22) * 2.63,
),
validator: (value) {
if (value.isEmpty) {
return 'Por favor ingrese su(s) Nombre(s)';
} else {
if (value.length < 4)
return 'El nombre debe tener mas de 4 caracteres';
}
return null;
}),
any ideas?
add labelText: 'Nombres', Property into InputDecoration :
decoration: InputDecoration(
hintText: "Nombres",
hintStyle: TextStyle(
fontSize: scalador.setSp(22) * 2.63,
color: Color(0xFF949494)),
),
labelText: 'Nombres',
)
source :https://api.flutter.dev/flutter/material/TextFormField-class.html
To add the desired textformfield in "focus" detail, you will need to include several things. To begin with, you will need a labelText which will be set to the string of your choice. In your case, it would probably be: labelText: "Nombres". Next, you will need an enabledBorder: which you can assign to OutlineInputBorder(in which you can specify border radius, border Side(color)) to your liking. Once you have the enabledBorder for when the user does not have it in "focus" then you will need focusedBorder: in which again you will assign to OutlineInputBorder() similar to enabledBorder. Lastly, you will need border: in which you can give it OutlineInputBorder(and your desired borderRadius inside).
This is an example for your reference
Padding(
padding: EdgeInsets.all(10),
child: new TextFormField(
decoration: new InputDecoration(
labelText: "Name",
labelStyle: TextStyle(
color: Color(0xFF264653),
),
fillColor: Colors.white,
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(25.0),
borderSide: BorderSide(
color: Color(0xFF264653),
),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(25.0),
borderSide: BorderSide(color: Color(0xFF264653))),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(25.0),
),
),
),
),
Depending on what you will be doing, I recommend checking out this article: https://medium.com/swlh/working-with-forms-in-flutter-a176cca9449a and/or
https://medium.com/#mahmudahsan/how-to-create-validate-and-save-form-in-flutter-e80b4d2a70a4

How to always show hint in text field not only when it is clicked in flutter?

I have a custom text field but as shown in the picture, the bottom text fields looks so vague and empty, I'd like to keep the hint showing even if the field is not focused, how do I achieve that in flutter?
here is my widget code:
Container(
margin: EdgeInsets.all(20),
child: TextFormField(
autofocus: true,
textAlign: TextAlign.right,
decoration: InputDecoration(
enabledBorder: const OutlineInputBorder(
borderSide: const BorderSide(color: Color(0xff0E9447), width: 2.0),
),
focusedBorder: const OutlineInputBorder(
borderSide: const BorderSide(color: Color(0xff0E9447), width: 2.0),
),
hintText: AppStrings.email,
labelText: AppStrings.email,
alignLabelWithHint: true,
hintStyle: TextStyle(color: AppColors.primaryColorLight),
),
),
),
If you would like the label to be visible at the top of the TextField, and the hint displayed at the same time you can simply add:
floatingLabelBehavior: FloatingLabelBehavior.always
to the TextFields InputDecoration (decoration).
(At the time of writing this, there is a bug that will only show the hint and suffix upon focus, this has been fixed in a very recent PR and will be available shortly, see GitHub issue)
Full Example
TextFormField(
controller: textController,
style: theme.textTheme.bodyText2,
keyboardType: keyboardType ?? TextInputType.number,
enableInteractiveSelection: false,
decoration: InputDecoration(
labelText: labelText,
labelStyle: theme.textTheme.headline6,
suffixText: suffixText ?? '',
border: OutlineInputBorder(
borderSide:
BorderSide(color: theme.textTheme.bodyText2.color, width: 2),
),
hintText: '0.0',
floatingLabelBehavior: FloatingLabelBehavior.always),
validator: (value) {
if (value.isEmpty) {
return 'Please enter some text';
}
return null;
},
onChanged: (String text) => onChange(text),
);
Ideally in Flutter you cannot do this as both hintText and labelText behave in two different ways. labelText is shown as hintText as long as the user does not focus on it. As soon as the user clicks on the TextField, the labelText animates to a specific position whereas a hintText remains visible until the user types something.
So using labelText and hintText together, does not make any sense as the TextField will wipe of the hintText while animating the label.
However with some extra effort, you can use Stack widget to solve your problem.
Declare a class variable (a variable within the concerned class, outside any block of code) to store a TextEditingController.
TextEditingController _controller;
And initialize in your class' initState(),
_controller= TextEditingController();
Solution Code:
Container(
margin: EdgeInsets.all(20),
child: Stack(
children : <Widget>[
TextFormField(
autofocus: true,
textAlign: TextAlign.right,
decoration: InputDecoration(
enabledBorder: const OutlineInputBorder(
borderSide: const BorderSide(color: Color(0xff0E9447), width: 2.0),
),
focusedBorder: const OutlineInputBorder(
borderSide: const BorderSide(color: Color(0xff0E9447), width: 2.0),
),
labelText: AppStrings.email,
alignLabelWithHint: true,
hintStyle: TextStyle(color: AppColors.primaryColorLight),
),
),
(_controller.text=="")
?
Text(
AppStrings.email,
style: TextStyle(
color: Colors.grey
// Style it according to your requirement / To make it look like hintText
),
)
:
Container();
],
),
),
Basic Logic of the above code: If the TextField does not have any text then display the (hint) Text
widget else don't display anything.
There is a way around this.
Use the labelText property and set floatingLabelBehavior: FloatingLabelBehavior.never.
This way you will always see the hint and when the User clicks on the TextField, it goes away.

How to get rid of padding to the left of suffixIcon? Because of the padding cursor is not centered correctly

Here is the code:
TextField(
controller: _textEditingController,
decoration: new InputDecoration(
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Theme.of(context).accentColor,
width: 4.0,
),
borderRadius: new BorderRadius.circular(8.0)),
focusedBorder: new OutlineInputBorder(
borderRadius: new BorderRadius.circular(8.0),
borderSide: new BorderSide(
style: BorderStyle.solid,
color: Theme.of(context).accentColor,
width: 4.0,
),
),
filled: true,
fillColor: _getFillColorBasedOnInput(context),
suffixIcon: IconButton(
icon: SvgPicture.asset(
'assets/images/checkmark_yellow.svg',
color: _textEditingController.text.isEmpty ? Colors.black12 : null,
width: 32.0,
height: 32.0,
)),
),
keyboardType: TextInputType.text,
textCapitalization: TextCapitalization.sentences,
textAlign: TextAlign.center,
cursorColor: Theme.of(context).accentColor,
style: new TextStyle(
fontSize: 24,
color: Theme.of(context).accentColor,
))
Here is how it looks like:
Can anyone suggest some solutions to center cursor against the beginning of an input field and a checkmark icon (see red dotted line). I have tried a lot of ideas but none of them led to the desired result. Thank you.
There's 2 things that you can do here:
1) Change the code of the suffix icon in the flutter files by removing the extra padding.
2) This is a workaround, you can use Stack Widget to keep your icon above the Textfield. This way you'll have the cursor centred.
Although, it will look unpleasant if your typed text is too long (i.e. Your typed input below icon). In that case you might limit your lnput.