The named parameter 'onSubmitted' isn't defined - flutter

The named parameter 'onSubmitted' isn't defined.
Try correcting the name to an existing named parameter's name, or defining a named parameter with the name 'onSubmitted
My Code :
TextFormField(
onSubmitted: (value) {
},
controller: _passcon,
decoration: InputDecoration(
labelText: 'Password',
prefixIcon: Icon(Icons.lock)),
obscureText: true,
validator: (value) {
if (value!.isEmpty) {
return 'Please enter password';
}
return null;
},
onSaved: (value) => _password = value!,
),
please help me sir

You need to replace onSubmit with onFieldSubmitted.
It will fix your problem

Related

How do I remove extra space between textField and error message in Flutter

I have a TextFormField with borders. I am using a validator with some conditions to show proper error messages. My error messages are shown exactly below the TextFormField border which is what I want. In my validator, I use an if statement that should return a String, so I have to add return '' after the if statement, as shown in my code:
TextFormField buildPasswordForm() {
return TextFormField(
//key: ValueKey('passwordKey'),
keyboardType: TextInputType.visiblePassword,
obscureText: _isHidden,
decoration: InputDecoration(
//labelText: 'Passowrd',
hintText: 'Password',
floatingLabelBehavior: FloatingLabelBehavior.never,
prefixIcon: Icon(
Icons.lock_sharp,
//color: kTextColor,
),
suffixIcon: Padding(
padding: EdgeInsets.symmetric(
horizontal: getProportionateScreenWidth(12),
),
child: GestureDetector(
onTapDown: inContact,
onTapUp: outContact,
child: Icon(
Icons.remove_red_eye,
size: 26,
//color: kTextColor,
),
),
),
),
validator: (value) {
if (value.isEmpty) {
addPasswordError(error: kPasswordNullError);
return '';
} else if (value.length < 8) {
addPasswordError(error: kShortPasswordError);
return '';
}
return null;
},
onChanged: (value) {
if (value.isNotEmpty) {
removePasswordError(error: kPasswordNullError);
} else if (value.length >= 8) {
removePasswordError(error: kShortPasswordError);
}
return null;
},
onSaved: (newValue) => password = newValue,
);
}
TextFormField buildEmailForm() {
return TextFormField(
keyboardType: TextInputType.emailAddress,
//autofocus: true,
decoration: InputDecoration(
//labelText: 'Email',
hintText: 'Enter your email',
floatingLabelBehavior: FloatingLabelBehavior.always,
prefixIcon: Icon(Icons.mail),
),
validator: (value) {
if (value.isEmpty) {
addEmailError(error: kEmailNullError);
return "";
} else if (!emailValidatorRegExp.hasMatch(value)) {
addEmailError(error: kInvalidEmailError);
return "";
}
return null;
},
onChanged: (value) {
if (value.isNotEmpty) {
removeEmailError(error: kEmailNullError);
} else if (emailValidatorRegExp.hasMatch(value)) {
emailErrors.remove(kInvalidEmailError);
addEmailError(error: kInvalidEmailError);
}
return null;
},
onSaved: (newValue) => email = newValue,
);
}
}
If I add return '', the space between the error message and the bottom board is added, but if the validation is not satisfied, that is: if neither email nor password entered satisfy requirements, the screen does not go to the next one as it should. If I remove return '', then the space between the error message and the bottom borders disappears, but when I hit continue, wether fields are properly filled or not, the screen just goes to the next one.
I would love to have a solution for, and explanation of this behavior.
in my case is like this i usually separate my validator to a different class
class Validator{
static String email(String value){
String pattern = r'^[a-zA-Z0-9.]+#[a-zA-Z0-9]+\.[a-zA-Z]';
RegExp regExp = RegExp(pattern);
if(!regExp.hasMatch(value)) return 'Email is invalid';
else if(value.isEmpty) return 'Email is Empty';
else return null;
}
}
then i create a widget for my textformfield and to call the validator like this
CustomTextFieldX(
obsecure: false,
hintText: 'Enter your email',
nameTop: 'Email',
controller: registerController.emails,
keyboardType: TextInputType.emailAddress,
validators: (value) =>Validator.email(registerController.emails.text = value),
),
i hope it helps. just comment below if their something to clarify at.

Int Value in a TextFormField Flutter

I wrote this textformfield for userdata
TextFormField(
initialValue: datiUtente.peso.toString(),
validator: (val) =>
val.isEmpty ? 'inserisci il tuo peso' : null,
onChanged: (val) => setState(() => _currentPeso = val),
),
For name ed surname is ok, but if I want to use this for an int value how can I do it? I tried to like this but doesn't work
You can specify the number as keyboardType for the TextField using:
keyboardType: TextInputType.number
And your field code look like below,
TextFormField(
decoration: new InputDecoration(labelText: "Enter your number"),
keyboardType: TextInputType.number,
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.digitsOnly
], // Only numbers can be entered
validator: (val) => val.isEmpty ? 'inserisci il tuo peso' : null,
onChanged: (val) => setState(() => _currentPeso = val),
)
Pass the keyboardType as in TextInputType.number TextFormField:- keyboardType: TextInputType.number

How to add a specific domain validator in a flutter form?

I have a login page but I only want the form to accept emails with a specific domain (e.g. #abc.net). Here is the code I have so far. How would I implement this?
Widget _showEmailInput() {
return new Padding(
padding: EdgeInsets.fromLTRB(15.0, 15.0, 15.0, 0.0),
child: TextFormField(
maxLines: 1,
keyboardType: TextInputType.emailAddress,
autofocus: false,
decoration: new InputDecoration(
hintText: 'Enter Email', icon: Icon(Icons.email, color: black)),
validator: (value) => value.isEmpty ? 'Email can not be empty' : null,
onSaved: (value) => _email = value.trim(),
));
}
Method 1
var isValid = value.toLowerCase().endsWith('your_domain.com');
Method 2 (RegEx)
validator: (value) {
if (value.isEmpty) return 'Email can not be empty'
var rx = RegExp("\b*#abc\.net\$",caseSensitive: false);
return rx.hasMatch(value) ? null : 'Invalid domain name';
}
Just replace abc.net to your desired domain name
print(rx.hasMatch('sdfsdf#sdf.net')); // false
print(rx.hasMatch('sdfsdf#abc.net')); // true
print(rx.hasMatch('sdfsdf#ABC.org')); // false
print(rx.hasMatch('sdfsdf#ABc.net')); // true

Custom money TextField in Flutter

I just started learning Flutter and i am trying to figure out how the user can enter an amount in a TextField ,for example 1234567 , should look like this when user is typing :
1.00
12.00
123.00
1 234.00
12 345.00
123 456.00
1 234 567.00
I tried using this library : mask_text_input_formatter, but i can't figure out how to do it. Here is my code
var maskFormatter = new MaskTextInputFormatter(
mask: '### ### ###.00', filter: {"#": RegExp(r'[0-9]')});
TextFormField(
keyboardType: TextInputType.number,
inputFormatters: [maskFormatter],
onChanged: (value) {},
decoration: InputDecoration(
prefixIcon: Icon(Icons.monetization_on),
hintText: 'Amount',
border: OutlineInputBorder(),
),
validator: (value) {
if (value.isEmpty) {
return 'Please enter some text';
}
return null;
},
),
Any idea on how to achieve that result.
Declare and initialize a text controller and assign it to the text field. Update its value on change in input!
TextEditingController _moneyController = TextEditingController();
#override
void initState() {
super.initState();
_moneyController.text = '.00';
}
TextFormField(
keyboardType: TextInputType.number,
inputFormatters: [maskFormatter],
onChanged: (value) {},
decoration: InputDecoration(
prefixIcon: Icon(Icons.monetization_on),
hintText: 'Amount',
border: OutlineInputBorder(),
),
validator: (value) {
if (value.isEmpty) {
return 'Please enter some text';
}
return null;
},
//added code here
controller: _moneyController,
),

How to display an empty string initially in a TextFormField allowing only Digits in flutter?

I have a text field that allows only digits. But when my user adds a new model then I need to preassign a value to the field numberOfPets.
Only 0-9 is allowed so I could use 0 but I would rather have an empty string in that text field but that is not possible as String is not a subtype of Int... I have also tried null but that is literally written as initialValue.
How would you display an empty string in that textfield ?
TextFormField(
keyboardType: TextInputType.number,
inputFormatters: [
LengthLimitingTextInputFormatter(3),
WhitelistingTextInputFormatter.digitsOnly,
],
decoration: const InputDecoration(
labelText: 'reps',
),
initialValue: widget.set.numberOfPets.toString() ,
validator: (value) {
return StringFieldValidator.validate(value);
},
onChanged: (value) {
setState(() {
widget.set.numberOfPets= int.parse(value);
});
},
),
Keep the numberOfPets as 0 but while assigning to initialValue check if 0 and change it to empty string
TextFormField(
keyboardType: TextInputType.number,
inputFormatters: [
LengthLimitingTextInputFormatter(3),
WhitelistingTextInputFormatter.digitsOnly,
],
decoration: const InputDecoration(
labelText: 'reps',
),
initialValue: widget.set.numberOfPets ==0 ? '' : widget.set.numberOfPets.toString() ,
validator: (value) {
return StringFieldValidator.validate(value);
},
onChanged: (value) {
setState(() {
widget.set.numberOfPets= int.parse(value);
});
},
),