Sanitize TextFormField value before validation in flutter - flutter

I want to use do some sanitization like trimming a string before the validator function is called. I don't want to do it in the onChange method but wait until the user finsehes his input.
TextFormField(
decoration: InputDecoration(
labelText: 'Email*', hintText: "john.doe#gmail.com"),
keyboardType: TextInputType.emailAddress,
validator: emailValidator,
onChanged: (String str) {
}
),

Related

Flutter TextFormField with non-const decoration

I want to write something like a function that generates TextFormFields with different names, but I don't want the labelText attribute to be const. That way I can easily rubber stamp out a bunch of similar fields that have different names.
For example
TextFormField myFormField(myMapType myMap, String fieldName) {
return TextFormField(
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: fieldName,
),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter your $fieldName';
}
return null;
},
initialValue: myMap.attribute[fieldName],
onSaved: (val) {
setState(
() {
myMap.setAttribute(fieldName, val!);
},
);
});
}
But that gives an error "Invalid constant value" at the line "labelText: fieldName". What's the trick needed to accomplish what I'm trying to do? Or what dumb mistake did I make?
fieldName will get on runtime rather than compile time. Therefore you cant use const
You can do
return TextFormField(
decoration: InputDecoration(
border: const OutlineInputBorder(), //this can be const
labelText: fieldName,
),
validator: (value) {
You can check What is the difference between the "const" and "final" keywords in Dart?

Remove error message in TextFormField in Flutter

Hi I want to remove error message caused by validator in TextFormField widget after I submit a form in flutter.
Ps : I want the error message disappeared after I select again the TextFormField to write into it again.
TextFormField(
controller: _emailController,
keyboardType: TextInputType.emailAddress,
textInputAction: TextInputAction.next,
validator: (value) {
if (value == null ||
value.isEmpty ||
!EmailValidator.validate(value)) {
return nullEmailMsg;
}
return null;
},
decoration: InputDecoration(
hintText: emailHint,
prefixIcon: Icon(Icons.mail),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
),
),
),
If you put your TextFormField inside a Form with the property autovalidateMode: AutovalidateMode.onUserInteraction, the validation function should trigger when you select again or change the text.
If the validation function is satisfied, the error message will be removed.
You have to use the autoValidateMode method in TextFormField.
Here, is the some of the reference:
https://api.flutter.dev/flutter/widgets/AutovalidateMode-class.html

Flutter : external barcode scanner continuous

I am developing a flutter application with below process
scan number (external Bluetooth barcode scanner)
upload barcode data
repeat 1,2 step c
I could able to scan and upload the first data. then I cleared the text. but i could not place the cursor at the _text controller.
I dont want to press the text field every time before scan the textfield.
TextField(
controller: _text,
textInputAction: TextInputAction.go,
onSubmitted: (value) {
print(submit online using function");
_text.clear();
_text.selection= TextSelection.collapsed(offset: -1);
},
decoration: const InputDecoration(
icon: Icon(Icons.person),
hintText: 'Enter ID',
labelText: 'Enter ID',
),
autofocus: true,
keyboardType: TextInputType.number,
inputFormatters: <TextInputFormatter>[
WhitelistingTextInputFormatter.digitsOnly
],
),
TextFormField(
enabled: true,
autofocus: true,
autocorrect: false,
textInputAction: TextInputAction.done,
keyboardType: TextInputType.text,
focusNode: focusBarCode,
onFieldSubmitted: (val) {
print(val); // the scan value
//process the val
barCodecontroller.text =""; // set to blank again
focusBarCode.requestFocus();//set focus again, so u can
//scan again
`enter code here`},
controller: barCodecontroller,
),

How to add the typed value in textfield as in update

I am creating a firebase CRUD flutter app. I want to show the typed value in textfield as in update. So that users can update the value by erasing the typed value.
//define
var _fNameController = TextEditingController();
//set value which one you set
#override
void initState() {
_fNameController.text = 'jayesh';
super.initState();
}
// textfield
TextFormField(
keyboardType: TextInputType.text,
controller: _fNameController,
textInputAction: TextInputAction.next,
textCapitalization: TextCapitalization.words,
decoration: InputDecoration(
hintText: 'First Name',
//border: OutlineInputBorder(),
),
validator: validator.validatefName,
),

Insert prefix text into database in flutter

can I insert prefix text into the database? prefix text like this:
I think prefix text (+62) entered the database when it was submitted but no ... is there another way to have +62 auto in TextFormField and can it enter the database ??
and this is my code `
TextFormField(
controller: controllerPhone,
decoration: InputDecoration(
labelText: "Phone",
icon: Icon(Icons.phone),
prefixText: '+62',
),
validator: validatePhoneNumber,
onSaved: (String value) {
phone = value;
}, ),
`
Make the prefix a variable and concatenate it in your onSaved():
String _prefix = '+62'; // somewhere in your code
TextFormField(
controller: controllerPhone,
decoration: InputDecoration(
labelText: "Phone",
icon: Icon(Icons.phone),
prefixText: _prefix,
),
validator: validatePhoneNumber,
onSaved: (String value) {
phone = '$_prefix $value';
},
Let me know how this works out for you.