I am using the below code for the email field along with the email validator in flutter application, which is working fine until if the user gives the whitespace after entering the email in the textfield, which I am not able to trim using .trim(), how should I trim the whitespace if in case the user has entered it?
String emailValidator(String value) {
Pattern pattern =
r'^(([^<>()[\]\\.,;:\s#\"]+(\.[^<>()[\]\\.,;:\s#\"]+)*)|(\".+\"))#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$';
RegExp regex = new RegExp(pattern);
if (!regex.hasMatch(value)) {
return 'Email format is invalid';
} else {
return null;
}
}
final email = TextFormField(
decoration: InputDecoration(
labelText: "Email",
labelStyle: TextStyle(color: Colors.black),
prefixIcon: Icon(
LineIcons.envelope,
color: Colors.black38,
),
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.black38),
),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.orange),
),
),
keyboardType: TextInputType.text,
style: TextStyle(color: Colors.black),
cursorColor: Colors.black,
controller: emailInputController,
validator: emailValidator,
);
For Flutter 1.20.0 and highest:
TextFormField(
validator: (value),
inputFormatters: [
FilteringTextInputFormatter.deny(new RegExp(r"\s\b|\b\s"))
],
)
How about you prevent user input whitespace using 'inputFormatters and BlacklistingTextInputFormatter'?
TextFormField(
validator: _validateInput,
inputFormatters: [BlacklistingTextInputFormatter(
new RegExp(r"\s\b|\b\s")
)],
...
It can be done in two different ways.
1.Allowing only the email regex.
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.allow(regex) // your email regex here
]
2.Denying the whitespace
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.allow(r"\s\b|\b\s")
]
Note: If you want to deny some special characters use FilteringTextInputFormatter.deny()
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.deny(regex) // your regex here
]
you should use inputFormatter and don't allow any white space.
TextFormField(
decoration: InputDecoration(
labelText: "Email",
labelStyle: TextStyle(color: Colors.black),
prefixIcon: Icon(
LineIcons.envelope,
color: Colors.black38,
),
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.black38),
),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.orange),
),
),
keyboardType: TextInputType.text,
style: TextStyle(color: Colors.black),
cursorColor: Colors.black,
controller: emailInputController,
validator: emailValidator,
inputFormatters: [WhitelistingTextInputFormatter(RegExp(r'[a-zA-Z0-9]'))],
)
Related
Hi, In flutter, How to create a text field that allows only numbers?
I'm unable to find a way to create an input field in Flutter that would open up a numeric keyboard and should take numeric input only. I tried. But I am getting errors.
Container(
width: 310,
TextEditingController _numberController = TextEditingController();
child: TextField(
controller: _numberController,
keyboardType: TextInputType.number,
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.digitsOnly,
],
decoration: InputDecoration(
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Color(0xffbcbcbc)),
),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.cyan),
),
hintText: '+919876567876',
hintStyle: GoogleFonts.lato(
color: Color(0xff000000),
fontSize: 20,
letterSpacing: 0.5,
decorationThickness: 3)),
),
),
Add any of these line inside your TextField widget
keyboardType: TextInputType.number,
or
keyboardType: TextInputType.phone,
You should declare TextEditingController variable inside class, not inside Container Widget. For numeric input only, set ' keyboardType: TextInputType.number ' in TextField.
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
// ...
}
)
i want to make the text and icon inside TextFormField
this is my code
enter image description here
_FormField() {
return TextFormField(
validator: (value) {
if (value!.isEmpty) {
print("test");
}
},
decoration: InputDecoration(
fillColor: kPrimaryLightColor,
filled: true,
focusColor: kPrimaryLightColor,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(30),
borderSide: BorderSide.none),
//hintText: 'Email'
//hintStyle: TextStyle(color: Colors.black38),
labelText: 'Email',
labelStyle: TextStyle(color: kPrimaryColor),
//icon: Icon(Icons.email),
icon: Icon(Icons.email_outlined),
),
);
}
i want it like that
enter image description here
You can use prefixIcon and suffixIcon of inputDecoration to add icons inside the field. If you want the field label only within the field and not as a label above it, specify only hintText and leave out LabelText. Check the following code, you can wrap the icons inside a Padding if you need more space.
TextFormField(
validator: (value) {
if (value!.isEmpty) {
print("test");
}
},
decoration: InputDecoration(
prefixIcon: Icon(Icons.email_outlined),
fillColor: Colors.grey,
filled: true,
focusColor: Colors.blue,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(30),
borderSide: BorderSide.none),
hintText: 'Email',
suffixIcon: Icon(Icons.email_outlined),
),
)
your can use prefixIcon and suffixIcon which takes widget. In which you can give Icon to the prefixIcon and to the suffixIcon you can give IconButton in which you can give the logic to hide/show password. To implement the logic declare a boolean variable which is used for if the password is to be shown or hidden. the logic must be like this.
bool showPassword=false;
_FormField() {
return TextFormField(
obscureText: !showPassword,
validator: (value) {
if (value!.isEmpty) {
print("test");
}
},
decoration: InputDecoration(
suffixIcon:IconButton(icon:showPassword?Icon(Icons.lock):Icon(Icons.lock_open),
onPressed:(){
setState(() {
showPassword=!showPassword;
});
}
)
),
);
}
The code is not formatted well sorry for that I write it manual here 🙏
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
I would like to implement a Text Widget whose label works in a similar way to the TextField below. (I don't expect I can input a value to the Text Widget. The Text Widget will be only for showing a text.) Could anyone teach me this?
If the value is '' (empty string), then show "Output" in the box.
If the value is not empty, then show "Output" on the border.
TextField sample
Without value:
With value:
TextField(
keyboardType: TextInputType.number,
style: Theme.of(context).textTheme.display1,
decoration: InputDecoration(
labelText: 'Input',
labelStyle: Theme.of(context).textTheme.display1,
errorText: _hasInputError ? 'Invalid number entered' : null,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(0.0),
),
),
),
TextFormField(
keyboardType: TextInputType.number,
style: Theme.of(context).textTheme.bodyText1,
decoration: InputDecoration(
labelText: 'Input',
hintText: "Input",
labelStyle: Theme.of(context).textTheme.bodyText1,
errorText: _hasInputError ? 'Invalid number entered' : null,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(0.0),
),
),
),