Showing TextField's error text does not work - flutter

I'm building the changePassword module on an app and I want to check that the field its not empty but its not working and I don't know why
Card(
color: Color(0xFFDBE2EF),
margin: EdgeInsets.symmetric(
vertical: 10, horizontal: 25),
child: TextField(
controller: passwordConfirmController,
obscureText: true,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Confirmar contraseña',
labelStyle:
TextStyle(fontFamily: 'Montserrat')),
),
),
SizedBox(
height: 20,
),
RaisedButton(
color: Color(0xFFDBE2EF),
onPressed: () => {
setState(() {
print("Valor _validate antes");
print(_validate);
print("Valor isEmpty");
print(passwordController.text.isEmpty);
passwordController.text.isEmpty
? _validate = true
: _validate = false;
_isLoading = true;
print("Valor _validate después");
print(_validate);
}),
changePass(passwordController.text),
},
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(5),
),
child: Text(
'Actualizar',
style: TextStyle(
fontSize: 20,
fontFamily: 'Montserrat',
color: Color(0xFF002D53),
fontWeight: FontWeight.bold),
)),
The values on raisedbutton where I check if the password is empty seems to be working printing the expected values, but the error text does not work

You need to pass the error text to the TextField
TextField(
decoration: InputDecoration(
errorText:"some error",
),

You have to change _validate property inside setState(), and based on that change TextField error text
class FirstScreen extends StatefulWidget {
#override
_FirstScreenState createState() => _FirstScreenState();
}
class _FirstScreenState extends State<FirstScreen> {
final passwordConfirmController = TextEditingController();
bool _validate = false;
void _validatePassword() {
if (passwordConfirmController.text.isEmpty) {
_validate = true;
} else {
_validate = false;
}
setState(() {
_validate;
print("Valor _validate después");
print(_validate);
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Column(
children: <Widget>[
Card(
color: Color(0xFFDBE2EF),
margin: EdgeInsets.symmetric(vertical: 10, horizontal: 25),
child: TextField(
controller: passwordConfirmController,
obscureText: true,
decoration: InputDecoration(
errorText: _validate ? "Enter Password" : null,
border: OutlineInputBorder(),
labelText: 'Confirmar contraseña',
labelStyle: TextStyle(fontFamily: 'Montserrat')),
),
),
SizedBox(
height: 20,
),
RaisedButton(
color: Color(0xFFDBE2EF),
onPressed: () => {_validatePassword()},
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(5),
),
child: Text(
'Actualizar',
style: TextStyle(
fontSize: 20,
fontFamily: 'Montserrat',
color: Color(0xFF002D53),
fontWeight: FontWeight.bold),
)),
],
),
);
}
}

Related

How to set dropdown selected item into TextFormField in Flutter

I created dropdown in TextFormField in flutter, i successfully loaded list (bankDataList) into dropdown which is dynamic list. Data is showing into dropdown list. But i have a problem to assign
"value : _bankChoose" into DropDownButton , it is not updating into TextFormField. I am using icon and text, please see screenshot.
String _bankChoose;
List<BankListDataModel> bankDataList;
Container(
margin: EdgeInsets.only(left: 15, top: 10, right: 15),
child: FormField<String>(
builder: (FormFieldState<String> state) {
return InputDecorator(
decoration: InputDecoration(
contentPadding:
EdgeInsets.fromLTRB(12, 10, 20, 20),
// labelText: "hi",
// labelStyle: textStyle,
// labelText: _dropdownValue == null
// ? 'Where are you from'
// : 'From',
errorText: _errorBank,
errorStyle: TextStyle(
color: Colors.redAccent, fontSize: 16.0),
border: OutlineInputBorder(
borderRadius:
BorderRadius.circular(10.0))),
child: DropdownButtonHideUnderline(
child: DropdownButton<BankListDataModel>(
style: TextStyle(
fontSize: 16,
color: text_gray_color,
fontFamily: "verdana_regular",
),
hint: Text(
"Select Bank",
style: TextStyle(
color: text_gray_color,
fontSize: 16,
fontFamily: "verdana_regular",
),
),
value: _bankChoose,
isExpanded: true,
isDense: true,
onChanged: (BankListDataModel newValue) {
setState(() {
_bankChoose = newValue.bank_name;
});
},
items: bankDataList
.map<DropdownMenuItem<BankListDataModel>>(
(BankListDataModel valueItem) {
return DropdownMenuItem(
value: valueItem,
child: Row(
// mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
new CircleAvatar(
backgroundImage: new NetworkImage(
valueItem.bank_logo),
),
// Icon(valueItem.bank_logo),
SizedBox(
width: 15,
),
Text(valueItem.bank_name),
],
),
);
}).toList(),
),
),
);
},
),
),
You want to choose drop down item of type BankListDataModel, then your _bankChoose variable should be of type BankListDataModel.
Try this one,
import 'package:flutter/material.dart';
class HomePage extends StatefulWidget {
#override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
//String _bankChoose;
BankListDataModel _bankChoose;
List<BankListDataModel> bankDataList=[
BankListDataModel("SBI","https://www.kindpng.com/picc/m/83-837808_sbi-logo-state-bank-of-india-group-png.png"),
BankListDataModel("HDFC","https://www.pngix.com/pngfile/big/12-123534_download-hdfc-bank-hd-png-download.png"),
BankListDataModel("ICICI","https://www.searchpng.com/wp-content/uploads/2019/01/ICICI-Bank-PNG-Icon-715x715.png"),
//BankListDataModel("Canara","https://bankforms.org/wp-content/uploads/2019/10/Canara-Bank.png")
];
#override
void initState() {
super.initState();
_bankChoose = bankDataList[0];
}
void _onDropDownItemSelected(BankListDataModel newSelectedBank) {
setState(() {
_bankChoose = newSelectedBank;
});
}
#override
Widget build(BuildContext context) {
return
Scaffold(
body: Form(
child: Center(
child: Container(
margin: EdgeInsets.only(left: 15, top: 10, right: 15),
child: FormField<String>(
builder: (FormFieldState<String> state) {
return InputDecorator(
decoration: InputDecoration(
contentPadding:
EdgeInsets.fromLTRB(12, 10, 20, 20),
// labelText: "hi",
// labelStyle: textStyle,
// labelText: _dropdownValue == null
// ? 'Where are you from'
// : 'From',
errorText: "Wrong Choice",
errorStyle: TextStyle(
color: Colors.redAccent, fontSize: 16.0),
border: OutlineInputBorder(
borderRadius:
BorderRadius.circular(10.0))),
child: DropdownButtonHideUnderline(
child: DropdownButton<BankListDataModel>(
style: TextStyle(
fontSize: 16,
color: Colors.grey,
fontFamily: "verdana_regular",
),
hint: Text(
"Select Bank",
style: TextStyle(
color: Colors.grey,
fontSize: 16,
fontFamily: "verdana_regular",
),
),
items: bankDataList
.map<DropdownMenuItem<BankListDataModel>>(
(BankListDataModel value) {
return DropdownMenuItem(
value: value,
child: Row(
// mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
new CircleAvatar(
backgroundImage: new NetworkImage(
value.bank_logo),
),
// Icon(valueItem.bank_logo),
SizedBox(
width: 15,
),
Text(value.bank_name),
],
),
);
}).toList(),
isExpanded: true,
isDense: true,
onChanged: (BankListDataModel newSelectedBank) {
_onDropDownItemSelected(newSelectedBank);
},
value: _bankChoose,
),
),
);
},
),
),
),
),
);
}
}
class BankListDataModel{
String bank_name;
String bank_logo;
BankListDataModel(this.bank_name,this.bank_logo);
}
sorry for formatting of images.
One way would be to use a TextEditingController and assign is to your TextField with controller: textEditingController for example
final textEditingController = TextEditingController();

Flutter show error when Password not match

I want to validate the passwords. The Widget _buildRepeatPasswordTF, has a TextFormField that validates if the value is not equal to the controller from _buildPasswordTF().
Widget _buildPasswordTF() {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: < Widget > [
Container(
alignment: Alignment.centerLeft,
decoration: kBoxDecorationStyle,
height: 60.0,
child: TextField(
controller: _passwordController,
obscureText: true,
style: TextStyle(
color: Color.fromRGBO(0, 128, 128, 1),
fontFamily: 'OpenSans',
),
decoration: InputDecoration(
border: InputBorder.none,
contentPadding: EdgeInsets.only(top: 14.0),
prefixIcon: Icon(
Icons.lock,
color: Color.fromRGBO(0, 128, 128, 1),
),
hintText: 'Password',
hintStyle: kHintTextStyle,
),
),
),
],
);
}
Widget _buildRepeatPasswordTF() {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
alignment: Alignment.centerLeft,
decoration: kBoxDecorationStyle,
height: 60.0,
child: TextFormField(
controller: _repeatPasswordController,
obscureText: true,
style: TextStyle(
color: Color.fromRGBO(0, 128, 128, 1),
fontFamily: 'OpenSans',
),
validator: (value) {
if (value != _passwordController.text) {
return 'The password doesnt match';
}
return null;
},
decoration: InputDecoration(
border: InputBorder.none,
contentPadding: EdgeInsets.only(top: 14.0),
prefixIcon: Icon(
Icons.lock,
color: Color.fromRGBO(0, 128, 128, 1),
),
hintText: 'Repeat password',
hintStyle: kHintTextStyle,
),
),
),
],
);
}
Widget _buildSignUpBtn() {
return Container(
padding: EdgeInsets.symmetric(vertical: 25.0),
width: double.infinity,
child: RaisedButton(
elevation: 5.0,
onPressed: () async {
var password = _passwordController.text;
var email = _emailController.text;
var nom = _nameController.text;
var cognoms = _cognomsController.text;
try {
var r = await _provider.attemptSignUp(email, password, nom, cognoms, interessos, sexe, dataNeixement);
if(r['success'] == false) {
displayDialog(context, "Error", r['data']['message']);
}
else {
displayDialog(context, "Registrat", "Verifica el correu.").then((val) {
Navigator.of(context).pop();
});
}
} catch (err) {
displayDialog(context, "Error", err.response.toString());
}
},
padding: EdgeInsets.all(15.0),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30.0),
),
color: Color.fromRGBO(0, 128, 128, 1),
child: Text(
'Registrar-se',
style: TextStyle(
color: Colors.white,
letterSpacing: 1.5,
fontSize: 18.0,
fontWeight: FontWeight.bold,
fontFamily: 'OpenSans',
),
),
),
);
}
You have missed a main Form and GlobalKeys.
Here a complete functional sample:
import 'package:flutter/material.dart';
class Password extends StatefulWidget {
#override
_PasswordState createState() => _PasswordState();
}
class _PasswordState extends State<Password> {
final TextEditingController _password = TextEditingController();
final TextEditingController _confirmPassword = TextEditingController();
final GlobalKey<FormState> _form = GlobalKey<FormState>();
#override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(30),
child: Form(
key: _form,
child: Column(children: <Widget>[
TextFormField(
controller: _password,
validator: (validator) {
if (validator.isEmpty) return 'Empty';
return null;
}),
TextFormField(
controller: _confirmPassword,
validator: (validator) {
if (validator.isEmpty) return 'Empty';
if (validator != _password.text)
return 'The passwords do not match';
return null;
}),
RaisedButton(
child: Text(
'Registrar-se',
),
elevation: 5.0,
onPressed: () async {
_form.currentState.validate();
})
])),
);
}
}
I also have made a validation more sophisticated using RxDart and Streams. Check it out on medium

Validating TextformField with two different key in Flutter

I'm trying to validate two different TextFormFields in two widgets (One for Email, another one for password) with a single _formkey in a flutter. it gave me this error: Multiple widgets used the same GlobalKey. So defined two _formkey but the problem is Flutter form validators don't validate, simultaneously:
class _RegisterState extends State<Register> {
String email = "";
String password = "";
String error = "";
final _formKey1 = GlobalKey<FormState>();
final _formKey2 = GlobalKey<FormState>();
// bool _rememberMe = false;
Widget _buildEmailTF() {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
'Email',
style: kLabelStyle,
),
SizedBox(height: 10.0),
Form(
key: _formKey1,
child: Container(
alignment: Alignment.centerLeft,
decoration: kBoxDecorationStyle,
height: 60.0,
child: TextFormField(
validator: (value) => value.isEmpty ? "Enter an Email" : null,
onChanged: (value) {
setState(() {
email = value;
});
},
style: TextStyle(
color: Colors.white,
fontFamily: 'OpenSans',
),
decoration: InputDecoration(
border: InputBorder.none,
contentPadding: EdgeInsets.only(top: 14.0),
prefixIcon: Icon(
Icons.email,
color: Colors.white,
),
hintText: 'Enter your Email',
hintStyle: kHintTextStyle,
),
),
),
),
],
);
}
Widget _buildPasswordTF() {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
'Password',
style: kLabelStyle,
),
SizedBox(height: 10.0),
Form(
key: _formKey2,
child: Container(
alignment: Alignment.centerLeft,
decoration: kBoxDecorationStyle,
height: 60.0,
child: TextFormField(
validator: (value) =>
value.length < 6 ? "More than 6 Character" : null,
onChanged: (value) {
setState(() {
password = value;
});
},
obscureText: true,
style: TextStyle(
color: Colors.white,
fontFamily: 'OpenSans',
),
decoration: InputDecoration(
border: InputBorder.none,
contentPadding: EdgeInsets.only(top: 14.0),
prefixIcon: Icon(
Icons.lock,
color: Colors.white,
),
hintText: 'Enter your Password',
hintStyle: kHintTextStyle,
),
),
),
),
],
);
}
and then :
onPressed: () async {
if (_formKey1.currentState.validate() &&
_formKey2.currentState.validate()) {
dynamic result =
await _auth.signUpWithEmailandPassword(email, password);
if (result == null) {
setState(() => error = "Something is wrong");
}
}
},
Just remember that you need one Form Widget above in the widget Tree.
And thus you can use the _formKey to validate multiple TextFormField below in the Widget Tree.
Modified Code
class _RegisterPageState extends State<RegisterPage> {
String email = "";
String password = "";
String error = "";
final _formKey1 = GlobalKey<FormState>();
// final _formKey2 = GlobalKey<FormState>();
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: Form(
key: _formKey1,
child: Container(
child: Column(
children: [
_buildEmailTF(),
SizedBox(
height: 20,
),
_buildPasswordTF(),
FlatButton(
onPressed: () async {
if (_formKey1.currentState.validate()) {
// dynamic result = await _auth.signUpWithEmailandPassword(
// email, password);
// if (result == null) {
// setState(() => error = "Something is wrong");
// }
print('DOne Working');
}
},
child: Text(
'Done',
))
],
),
),
),
);
}
// bool _rememberMe = false;
Widget _buildEmailTF() {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
'Email',
),
SizedBox(height: 10.0),
Container(
alignment: Alignment.centerLeft,
height: 60.0,
child: TextFormField(
validator: (value) => value.isEmpty ? "Enter an Email" : null,
onChanged: (value) {
setState(() {
email = value;
});
},
style: TextStyle(
color: Colors.white,
fontFamily: 'OpenSans',
),
decoration: InputDecoration(
border: InputBorder.none,
contentPadding: EdgeInsets.only(top: 14.0),
prefixIcon: Icon(
Icons.email,
color: Colors.white,
),
hintText: 'Enter your Email',
),
),
),
],
);
}
Widget _buildPasswordTF() {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
'Password',
),
SizedBox(height: 10.0),
Container(
alignment: Alignment.centerLeft,
height: 60.0,
child: TextFormField(
validator: (value) =>
value.length < 6 ? "More than 6 Character" : null,
onChanged: (value) {
setState(() {
password = value;
});
},
obscureText: true,
style: TextStyle(
color: Colors.white,
fontFamily: 'OpenSans',
),
decoration: InputDecoration(
border: InputBorder.none,
contentPadding: EdgeInsets.only(top: 14.0),
prefixIcon: Icon(
Icons.lock,
color: Colors.white,
),
hintText: 'Enter your Password',
),
),
),
],
);
}
}
I/flutter (24750): DOne Working

Flutter: Password Check

I will enter the password to the user 2 times. but i don't know how to check this. How can I be sure that both passwords are spelled the same? can i do this?
here is my code,(I just put the relevant parts in order not to be long)
class _RegisterPageState extends State<RegisterPage> {
#override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: SingleChildScrollView(
child: Column(
children: [
_paddingPasswordWidget('Password', 'Password'),
_paddingPasswordWidget('Password Again', 'Password Again'),
],
),
),
),
);
}
}
_paddingPasswordWidget(String hintTextStr, String labelTextStr) {
return Padding(
padding: EdgeInsets.only(top: 15, left: 22, right: 22),
child: TextFormField(
keyboardType: TextInputType.text,
style: TextStyle(
color: HexColor('#868686'),
),
decoration: CommonInputStyle.textFieldStyle(
hintTextStr: hintTextStr,
labelTextStr: labelTextStr,
),
obscureText: true,
),
);
}
class CommonInputStyle {
static InputDecoration textFieldStyle(
{String labelTextStr = "", String hintTextStr = ""}) {
return InputDecoration(
contentPadding: EdgeInsets.only(left: 20, top: 5, bottom: 5, right: 20),
labelText: labelTextStr,
hintText: hintTextStr,
labelStyle: TextStyle(fontSize: 14, color: HexColor('#868686')),
hintStyle: TextStyle(fontSize: 14, color: HexColor('#868686')),
filled: true,
fillColor: HexColor('#EEF2F4'),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(16),
borderSide: BorderSide.none,
),
);
}
}
You can try putting textEditingController in your fields, then you compare them. Something like this:
var textFieldPasswordController = TextEditingController();
var textFieldConfirmPasswordController = TextEditingController();
then you pass these as controllers in your TextFormField:
TextFormField(
controller: textFieldPasswordController //as an example
keyboardType: TextInputType.text,
style: TextStyle(
color: HexColor('#868686'),
),
Now you just need to check if textFieldPasswordController.text equals to textFieldConfirmPasswordController.text. You can check this in the onChange or validator function of TextFormField
if(textFieldPasswordController.text == textFieldConfirmPasswordController.text){
print("Access granted");
} else{
print("Try again");
}

Flutter replace "," with "."

I have a TextField where I allow numbers like 1,4. I am trying ti replace the , to a . so the number will be 1.4.
I am trying this
double alphareceipe = double.parse(_alphareceipe.text);
alphareceipe = alphareceipe.replace("," ".");
But get this error:
error: The method 'replace' isn't defined for the class 'double'. (undefined_method at [brewingapp] lib/screens/calculator/alphaacid.dart:227).
The total code looks like this:
import 'package:flutter/material.dart';
import 'package:firebase_admob/firebase_admob.dart';
import 'package:brewingapp/app_localizations.dart';
import 'package:flutter/services.dart';
import 'package:devicelocale/devicelocale.dart';
const String testDevice = "Mobile_id";
class AlphaAcid extends StatefulWidget {
#override
_AlphaAcidState createState() => _AlphaAcidState();
}
class _AlphaAcidState extends State<AlphaAcid> {
}
String _emiResult = "";
final TextEditingController _weight = TextEditingController();
final TextEditingController _alphareceipe = TextEditingController();
final TextEditingController _alphanew = TextEditingController();
#override
void initState() {
initPlatformState();
super.initState();
}
#override
String _locale;
Future<void> initPlatformState() async {
String currentLocale;
// Platform messages may fail, so we use a try/catch PlatformException.
try {
currentLocale = await Devicelocale.currentLocale;
print(currentLocale);
} on PlatformException {
print("Error obtaining current locale");
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(() {
_locale = currentLocale;
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(AppLocalizations.of(context).translate('Hopsalpha'),),
backgroundColor: Colors.green[800],
//elevation: 0.0,
),
body: Center(
child: Container(
child: Column(
children: <Widget>[
Container(
padding: EdgeInsets.all(20.0),
child: TextField(
cursorColor: Colors.green[800],
controller: _alphareceipe,
decoration: InputDecoration(
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.green[800], width: 2.0,),
),
filled: true,
fillColor: Colors.green[10],
labelText: _locale != 'da-DK'
? "Alpha acid receipe"
: "Alpha syre opskrift",
labelStyle: TextStyle(color: Colors.green[800])
),
keyboardType: TextInputType.numberWithOptions(decimal: true),
inputFormatters: <TextInputFormatter>[
// WhitelistingTextInputFormatter.digitsOnly
],
),
),
Container(
padding: EdgeInsets.all(20.0),
child: TextField(
cursorColor: Colors.green[800],
controller: _alphanew,
decoration: InputDecoration(
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.green[800], width: 2.0,),
),
filled: true,
fillColor: Colors.green[10],
//labelText: "Final Gravity (1018)",
labelText: _locale != 'da-DK'
? "Your alpha acid"
: "Din alpha syre",
labelStyle: TextStyle(color: Colors.green[800]),
),
keyboardType: TextInputType.numberWithOptions(decimal: true),
inputFormatters: <TextInputFormatter>[
//WhitelistingTextInputFormatter.digitsOnly
],
),
),
Container(
padding: EdgeInsets.all(20.0),
child: TextField(
cursorColor: Colors.green[800],
controller: _weight,
decoration: InputDecoration(
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.green[800], width: 2.0,),
),
filled: true,
fillColor: Colors.green[10],
//labelText: "Final Gravity (1018)",
labelText: _locale != 'da-DK'
? "Weight receipe"
: "Vægt i opskrift",
labelStyle: TextStyle(color: Colors.green[800]),
),
keyboardType: TextInputType.number,
inputFormatters: <TextInputFormatter>[
WhitelistingTextInputFormatter.digitsOnly
],
),
),
Flexible(
fit: FlexFit.loose,
child: FlatButton(
onPressed: _handleCalculation,
//child: Text("Calculate"),
child: Text(AppLocalizations.of(context).translate('calculate'),),
color: Colors.green[600],
textColor: Colors.white,
padding: EdgeInsets.only(top: 10.0, bottom: 10.0, left: 24.0, right: 24.0),
),
),
emiResultsWidget(_emiResult)
],
),
),
));
}
void _handleCalculation() {
double newWeight = 0.0;
//
//
//
//
//
double alphareceipe = double.parse(_alphareceipe.text);
alphareceipe = alphareceipe.replace("," ".");
int alphanew =int.parse(_alphanew.text);
int weightreceipe = int.parse(_weight.text);
//
try {
newWeight = (weightreceipe * alphareceipe) / alphanew;
_emiResult = newWeight.toStringAsFixed(0);
setState(() {
});
} //catch(e) {
on Exception catch(e){
print(e);
setState(() {
});
}
//_emiResult = A.toStringAsFixed(1);
//setState(() {
//});
}
Widget emiResultsWidget(emiResult) {
bool canShow = false;
String _emiResult = emiResult;
if(_emiResult.length > 0) {
canShow = true;
}
return
Container(
margin: EdgeInsets.only(top: 30.0, left: 20.0, right: 20.0),
alignment: FractionalOffset.center,
color: Colors.green[50],
child: canShow ? Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Row(
//crossAxisAlignment: CrossAxisAlignment.start,
//mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget> [
Padding(
padding: EdgeInsets.all(10.0),
child:
Text(AppLocalizations.of(context).translate('newweight'),
style: TextStyle(
fontSize: 20.0,
fontWeight: FontWeight.bold,
color: Colors.green[800],
),
)
),
SizedBox(width: 20.0,),
Text(_emiResult,
style: TextStyle(
fontSize: 20.0,
fontWeight: FontWeight.bold,
color: Colors.green[800],
)
),
SizedBox(width: 10.0,),
]),
]
) : Container(),
);
}
}
I would suggest to use an appropriate keyboard type in combination with an input formatter.
keyboardType: TextInputType.numberWithOptions(decimal: allowDecimal),
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.allow(RegExp(r'[0-9]+[,.]{0,1}[0-9]*')),
TextInputFormatter.withFunction(
(oldValue, newValue) => newValue.copyWith(
text: newValue.text.replaceAll(',', '.'),
),
),
],
This way, the user can only enter digits and ",", ".", "-" and even if a "," is entered, the second input formatter (they can be chained) replaces the ",". All this happens while this is still a String and not a number.
You cannot use a String function on a double. Replace the symbol on the String, then cast to double.
You are trying to replace the characters after the text has already been converted to a double. Try this instead:
double alphareceipe = double.parse(_alphareceipe.text.replaceAll(",", "."));