Reputation: 1793
I have a textformfield in my app which looks like below:
TextFormField(
//maxLength: 02,
textAlign: TextAlign.center,
keyboardType: TextInputType.numberWithOptions(decimal: true),
decoration: InputDecoration(
labelText: textFieldInitVal.toString(),
contentPadding: const EdgeInsets.all(0),
border: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.black54,
),
),
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.blue,
),
),
),
//initialValue: textFieldInitVal.toString(),
onChanged: (val) {
if (int.parse(val) > 24) {
setState(() {
errorTxt = 'ERROR: Value cannot be more than 24 Hrs';
});
} else {
setState(() {errorTxt = '';});
setState(() {value1 = int.parse(val).toDouble();});
//setState(() {value1 = double.parse(val);});
}
}
),
In the above snippet, both textFieldInitVal
and value1
are of type double.
I am facing two different problems:
Even if I use textAlign: TextAlign.center
, I am unable to center the labelText: textFieldInitVal.toString()
. The label goes and sticks to the left edge of the TextFormField. Adding some padding
helps, but it also affects how the input text is placed, hence I am trying to avoid it.
In the onChanged: (val) {
piece, when I do a print(val)
, I notice that the TextFormField fails to take anything after decimal value and hence while converting to double from string for val
, I am only getting whole numbers.
They look like very small issues, but am unable to figure a way out of this situation. What am I doing wrong?
Upvotes: 0
Views: 148
Reputation: 80934
Even if I use textAlign: TextAlign.center, I am unable to center the labelText: textFieldInitVal.toString(). The label goes and sticks to the left edge of the TextFormField. Adding some padding helps, but it also affects how the input text is placed, hence I am trying to avoid it.
The property textAlign
will align the text that you write inside the field in the middle of the field, but the label will stay at the left.
In the onChanged: (val) { piece, when I do a print(val), I notice that the TextFormField fails to take anything after decimal value and hence while converting to double from string for val, I am only getting whole numbers.
Regarding this question, you need to use the double
class instead of the int
class to get the decimal when converting.
onChanged: (val) {
if (double.parse(val) > 24) {
setState(() {
errorTxt = 'ERROR: Value cannot be more than 24 Hrs';
});
} else {
print(double.parse(val).toDouble());
setState(() {errorTxt = '';});
setState(() {value1 = int.parse(val).toDouble();});
}
}
Upvotes: 1