In my code I validate phone number. If phone number is incorrect - I show error message. But, when user starts to edit number I want to hide this error message.
I've found the solution with currentState.reset(), but it seems not the good one. I have to handle issues with saving text and cursor position. And I still have one small artifact. Normally when I press and hold backspace - it deletes symbols one by one. If I do it when error message is shown - then error message disappears and only one symbol is deleted.
Does anybody know the right solution for this case?
final TextEditingController controller = TextEditingController();
final RegExp _phoneRegex = RegExp(r"^\+{1}\d{10, 15}\$");
bool isError = false;
TextSelection currentPosition;
return Column(
children: <Widget>[
Form(
key: _textKey,
child: TextFormField(
controller: controller,
validator: (str) {
isError = true;
if (str.isEmpty) {
return err_empty_field;
} else if (!_phoneRegex.hasMatch(str)) {
return err_invalid_phone;
}
isError = false;
},
),
onChanged: () {
if (controller.selection.start < 0 &&
controller.text.length > 0) {
TextSelection position =
controller.text.length > currentPosition.start
? currentPosition
: TextSelection.fromPosition(
TextPosition(offset: controller.text.length));
controller.selection = position;
}
if (isError) {
isError = false;
currentPosition = controller.selection;
if (currentPosition.start > controller.text.length) {
currentPosition = TextSelection.fromPosition(
TextPosition(offset: controller.text.length));
}
String currentText = controller.text;
_textKey.currentState.reset();
controller.text = currentText;
controller.selection = currentPosition;
}
},
),
RaisedButton(
onPressed: () {
_textKey.currentState.validate();
},
child: Text(login),
)
],
);
EDIT (Nov 2020)
autovalidate was deprecated after v1.19.0.
Instead use autovalidateMode:
Form(
autovalidateMode: AutovalidateMode.onUserInteraction`.
...
)
Original post
here is a suitable solution to this problem.
You don't actually need to use onChanged or any tips causing side-effects, I solved it by creating a class property which is initialized to false:
bool _autovalidate = false;
The Form Widget has a named property autovalidate. You should pass it the previous boolean:
Form(
key: _textKey,
autovalidate: _autovalidate,
...
)
And in your Submit button onPressed() method, you should update the _autovalidate boolean to be true if the form is invalid, this will make the form to auto validate the TextFormField on every onChanged call:
RaisedButton(
onPressed: () {
if (_textKey.currentState.validate()) {
print('valid');
} else {
print('invalid');
setState(() => _autoValidate = true);
}
},
child: Text(login),
)
I hope it helped Somebody.
January 2021
...
AutovalidateMode _autoValidate = AutovalidateMode.disabled;
Form(
key: _textKey,
autovalidateMode: _autovalidate,
...
)
RaisedButton(
onPressed: () {
if (_textKey.currentState.validate()) {
print('valid');
} else {
print('invalid');
setState(() => _autoValidate = AutovalidateMode.always);
}
},
child: Text("login"),
)
The problem here is errorText is automatically managed by the validator field of the TextFormField. At the same time, the simple solution is to handle the errorText manually.
Step 1: Create
String field, _errorText initialised to null. The field will hold the error message that needs to be shown.
Boolean field, _error initialised to false. The filed is true if there is an error otherwise false.
Step 2:
Assign _errorText to TextFormField
Step 3 (Important):
Make sure that TextFormField validator returns a null value.
Handle the validation here and assign the proper error message to _errorText.
Update _error state correspondingly.
Step 4 (Important):
Reset _errorText and _error. This will remove the error from field soon as you start editing.
Step 5:
Trigger field validation in the onFieldSubmitted and manage your code flow...
import 'package:flutter/material.dart';
class WorkGround extends StatefulWidget {
#override
_WorkGroundState createState() => _WorkGroundState();
}
class _WorkGroundState extends State<WorkGround> {
final _formKey = GlobalKey<FormState>();
final _usernameFocusNode = FocusNode();
final _phoneNumberFocusNode = FocusNode();
/*
* Step 1.
* */
String _userNameErrorText;
bool _userNameError = false;
String _phoneNumberErrorText;
bool _phoneNumberError = false;
#override
Widget build(BuildContext context) {
return Form(
key: _formKey,
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
TextFormField(
focusNode: _usernameFocusNode,
decoration: InputDecoration(
labelText: 'Username',
/*
* Step 2
* */
errorText: _userNameErrorText, // Handling error manually
),
textInputAction: TextInputAction.next,
/*
* Step 3
* */
validator: (value) {
setState(() {
if(value.isEmpty) {
_userNameError = true;
_userNameErrorText = 'Enter Username';
}
});
return null; // Return null to handle error manually.
},
/*
* Step 4
* */
onChanged: (value) {
setState(() {
_userNameError = false;
_userNameErrorText = null; // Resets the error
});
},
/*
* Step 5
* */
onFieldSubmitted: (value) {
_formKey.currentState.validate(); // Trigger validation
if(!_userNameError) {
FocusScope.of(context).requestFocus(_phoneNumberFocusNode);
}
},
),
TextFormField(
focusNode: _phoneNumberFocusNode,
decoration: InputDecoration(
labelText: 'Phone Number',
/*
* Step 2
* */
errorText: _phoneNumberErrorText, // Handling error manually
),
textInputAction: TextInputAction.done,
/*
* Step 3
* */
validator: (value) {
setState(() {
if(value.isEmpty) {
_phoneNumberError = true;
_phoneNumberErrorText = 'Enter Phone number';
} else if( value.length < 10) {
_phoneNumberError = true;
_phoneNumberErrorText = 'Invalid Phone number';
}
});
return null; // Return null to handle error manually.
},
/*
* Step 4
* */
onChanged: (value) {
setState(() {
_phoneNumberError = false;
_phoneNumberErrorText = null; // Resets the error
});
},
/*
* Step 5
* */
onFieldSubmitted: (value) {
_formKey.currentState.validate(); // Trigger validation
if(!_phoneNumberError) {
// submit form or whatever your code flow is...
}
},
),
],
),
),
);
}
}
I have achieved your both below functionality:
1) Hide error message when editing
2) validate input field when login button pressed
Note: i have commented phone number regex and put validation for
string length < 10 digit for testing.
import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
#override
void initState(){
super.initState();
}
final TextEditingController controller = TextEditingController();
// final RegExp _phoneRegex = RegExp(r"^\+{1}\d{10, 15}\$");
bool isError = false;
bool isWriting = false;
bool isLoginPressed = false;
int counter = 0;
String myErrorString = "";
TextSelection currentPosition;
final _textKey = GlobalKey<FormState>();
#override
Widget build(BuildContext ctx) {
return Scaffold(
appBar: AppBar(
title: Text('MapSample'),
),
body: Container(
child: Column(
children: <Widget>[
Form(
key: _textKey,
child: TextFormField(
controller: controller,
validator: (str) {
myErrorString = "";
if(isLoginPressed){
isError = true;
if (str.isEmpty) {
myErrorString = 'err_empty_field';
return myErrorString;
}
else if (str.length < 10) {
myErrorString = 'err_invalid_phone';
validateMe();
return myErrorString;
}
/*else if (!_phoneRegex.hasMatch(str)) {
myErrorString = 'err_invalid_phone';
validateMe();
return myErrorString;
}*/
isError = false;
myErrorString = "";
}else{
myErrorString = "";
}
},
),
onChanged: () {
counter++;
if(counter == 9){
counter = 0;
isLoginPressed = false;
}
if(isLoginPressed){
}else{
isWriting = true;
isLoginPressed = false;
myErrorString = "";
_textKey.currentState.validate();
}
},
),
RaisedButton(
onPressed: () {
counter = 1;
isWriting = false;
isLoginPressed = true;
_textKey.currentState.validate();
},
child: Text('login'),
)
],
),
),
);
}
void validateMe() {
if(isLoginPressed){
currentPosition = TextSelection.fromPosition(
TextPosition(offset: controller.text.length));
String currentText = controller.text;
_textKey.currentState.reset();
controller.text = currentText;
controller.selection = currentPosition;
isWriting = false;
isLoginPressed = true;
}
}
}
I've found working and easier way
final _textKey = GlobalKey<FormState>();
final TextEditingController _controller = TextEditingController();
Widget _getPhoneInputForm() {
final RegExp _phoneRegex = RegExp(r"^\+{1}\d{10,17}");
bool isError = false;
bool isButtonPressed = false;
return Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(
padding: EdgeInsets.symmetric(horizontal: 36.0),
child: Form(
key: _textKey,
child: TextFormField(
keyboardType: TextInputType.phone,
decoration: InputDecoration(
hintText: hint_enter_phone,
contentPadding: EdgeInsets.all(24.0),
fillColor: Colors.blueGrey.withOpacity(0.3),
filled: true,
border: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(16.0)),
borderSide: BorderSide(color: Colors.blueGrey))),
controller: _controller,
validator: (str) {
if (!isButtonPressed) {
return null;
}
isError = true;
if (str.isEmpty) {
return err_empty_field;
} else if (!_phoneRegex.hasMatch(str)) {
return err_invalid_phone;
}
isError = false;
},
onFieldSubmitted: (str) {
if (_textKey.currentState.validate()) _phoneLogin();
},
),
onChanged: () {
isButtonPressed = false;
if (isError) {
_textKey.currentState.validate();
}
},
),
),
RaisedButton(
color: Colors.teal,
textColor: Colors.white,
onPressed: () {
isButtonPressed = true;
if (_textKey.currentState.validate()) _phoneLogin();
},
child: Text(login),
)
],
);
}
This is an exemple , i think its not necessary to do onchange() , the function validate name do the work ...
String validateName(String value) {
String patttern = r'(^[a-zA-Z ]*$)';
RegExp regExp = new RegExp(patttern);
if (value.length == 0) {
return "Name is Required";
} else if (!regExp.hasMatch(value)) {
return "Name must be a-z and A-Z";
}
return null;
}
TextFormField(
controller: _lastname, validator: validateName ,
//initialValue: widget.contact.last_name,
decoration:
InputDecoration(labelText: 'Last name'),
),
void Save() {
if (_keyForm.currentState.validate()) {
// No any error in validation
_keyForm.currentState.save();
................
}
i have found that using a combination of FocusNode and AtuoValidateMode.onUserInteraction does the trick.
class _TextAutoValidateModeExampleState extends State<TextAutoValidateModeExample> {
FocusNode node = FocusNode();
#override
Widget build(BuildContext context) {
return Container(
child: TextFormField(
focusNode: node,
autovalidateMode: AutovalidateMode.onUserInteraction,
validator: (value) {
if(node.hasFocus) return null;
if (value!.isEmpty) return "value cannot be empty";
if (!value.isEmail) return "not a valid email";
},
),
);
}
}
// Call this method inside onChanged() and when its focusnode hasFocus
void formReset(GlobalKey<FormState> formKey, TextEditingController controller) {
String stringValue = controller.text;
TextPosition textPosition = controller.selection.base;
formKey.currentState.reset();
controller.text = stringValue;
controller.selection = TextSelection.fromPosition(textPosition);
}
this format worked for me, Hope it helps someone....
validator: (value){
bool emailValid = RegExp(r"^[a-zA-Z0-9.a-zA-Z0-9.!#$%&'*+-/=?^_`{|}~]+#[a-zA-Z0-9]+\.[a-zA-Z]+").hasMatch(value);
isError = true;
if(value.isEmpty){
return "Provide an email";
}else if(!emailValid){
return "Enter a valid email";
}
isError = false;
return null;
},
Related
I want to limit the value that user input not to over a expected value in flutter text field.
Example,
If there is a value come from API is 10 and want to limit the input not over 10, if client type 11 or something over 10, want to show alert or make user not to type.
How to control this?
TextFormField(
style: TextStyle(fontSize: 20),
onChanged: (value) {
if (value != "") {
int _checkValue = int.parse(value);
if (_checkValue >
Provider.of<SaleProvider>(context, listen: false)
.remainNewQuantity(
this.currentProductItemSelected.id)) {
return 'error';
} else {
setState(() {
this.qty = int.parse(value);
updateByQty();
});
}
} else {
setState(() {
});
}
},
),
This is my trying, but can't do that I want.
Please check below method. I think this will resolve your issue. If still not work, please let me know
Widget getTextField({required int maxValue}) {
return TextFormField(
controller: _textController,
keyboardType: TextInputType.number,
onChanged: (text) {
if (int.parse(text) > maxValue) {
// show popup here.
_textController.text = validText;
_textController.selection = TextSelection.fromPosition(TextPosition(offset: _textController.text.length));
}else{
validText = text;
}
},
);
}
As an exercise for my own learning I gave it a go and came up with the following approach; creating a bespoke widget which admittedly looks like a lot of code for something so simple... but it appears to work as expected I think and one could modify, expand and integrate it with other elements in various ways.
Usage: const MaxIntField(max: 100),
Implementation:
class MaxIntField extends StatefulWidget {
const MaxIntField({Key? key, this.max = 1}) : super(key: key);
final int max;
#override
State<MaxIntField> createState() => _MaxIntFieldState();
}
class _MaxIntFieldState extends State<MaxIntField> {
final TextEditingController _controller = TextEditingController();
#override
void initState() {
super.initState();
_controller.value.copyWith(text: '0');
_controller.addListener(() {
if (_controller.text.isNotEmpty && _controller.text != '0') {
int intVal = int.parse(_controller.text);
if (intVal > widget.max) {
setState(() {
_controller.value =
_controller.value.copyWith(text: widget.max.toString());
_showMyDialog();
});
} else if (_controller.text != intVal.toString()) {
//remove leading '0'
setState(() {
_controller.value =
_controller.value.copyWith(text: intVal.toString());
});
}
}
});
}
// assuming using Material
_showMyDialog() async {
showDialog<String>(
context: context,
builder: (BuildContext context) => AlertDialog(
title: const Text('AlertDialog Title'),
content: Text('This field is limited to ${widget.max}'),
actions: <Widget>[
TextButton(
onPressed: () => Navigator.pop(context, 'OK'),
child: const Text('OK'),
),
],
),
);
}
#override
void dispose() {
_controller.dispose();
super.dispose();
}
#override
Widget build(BuildContext context) {
return TextFormField(
controller: _controller,
keyboardType: TextInputType.number,
);
}
}
below is my code. i have 6 fields. all of them are showing error all at once. even after i submit the form. once it gets cleared. it would show the validation message as field cannot be empty /Name is required.
how do i validate each field at a time. i don't want all the fields to show validate message as in the below image.
import 'package:flutter/material.dart';
class FormScreen extends StatefulWidget {
#override
State<StatefulWidget> createState() {
return FormScreenState();
}
}
class FormScreenState extends State<FormScreen> {
String _name;
String _email;
String _password;
String _url;
String _phoneNumber;
String _calories;
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
Widget _buildName() {
return TextFormField(
decoration: InputDecoration(labelText: 'Name'),
maxLength: 10,
validator: (String value) {
if (value.isEmpty) {
return 'Name is Required';
}
return null;
},
onSaved: (String value) {
_name = value;
},
);
}
Widget _buildEmail() {
return TextFormField(
decoration: InputDecoration(labelText: 'Email'),
validator: (String value) {
if (value.isEmpty) {
return 'Email is Required';
}
if (!RegExp(
r"[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*#(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?")
.hasMatch(value)) {
return 'Please enter a valid email Address';
}
return null;
},
onSaved: (String value) {
_email = value;
},
);
}
Widget _buildPassword() {
return TextFormField(
decoration: InputDecoration(labelText: 'Password'),
keyboardType: TextInputType.visiblePassword,
validator: (String value) {
if (value.isEmpty) {
return 'Password is Required';
}
return null;
},
onSaved: (String value) {
_password = value;
},
);
}
Widget _builURL() {
return TextFormField(
decoration: InputDecoration(labelText: 'Url'),
keyboardType: TextInputType.url,
validator: (String value) {
if (value.isEmpty) {
return 'URL is Required';
}
return null;
},
onSaved: (String value) {
_url = value;
},
);
}
Widget _buildPhoneNumber() {
return TextFormField(
decoration: InputDecoration(labelText: 'Phone number'),
keyboardType: TextInputType.phone,
validator: (String value) {
if (value.isEmpty) {
return 'Phone number is Required';
}
return null;
},
onSaved: (String value) {
_url = value;
},
);
}
Widget _buildCalories() {
return TextFormField(
decoration: InputDecoration(labelText: 'Calories'),
keyboardType: TextInputType.number,
validator: (String value) {
int calories = int.tryParse(value);
if (calories == null || calories <= 0) {
return 'Calories must be greater than 0';
}
return null;
},
onSaved: (String value) {
_calories = value;
},
);
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Form Demo")),
body: Container(
margin: EdgeInsets.all(24),
child: Form(
key: _formKey,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
_buildName(),
_buildEmail(),
_buildPassword(),
_builURL(),
_buildPhoneNumber(),
_buildCalories(),
SizedBox(height: 100),
RaisedButton(
child: Text(
'Submit',
style: TextStyle(color: Colors.blue, fontSize: 16),
),
onPressed: () {
if (!_formKey.currentState.validate()) {
return;
}
_formKey.currentState.save();
print(_name);
print(_email);
print(_phoneNumber);
print(_url);
print(_password);
print(_calories);
//Send to API
},
)
],
),
),
),
);
}
}
Use FocusNode with autovalidateMode: tempFocusNode.hasFocus? AutovalidateMode.always:AutovalidateMode.disabled as follows,in TextFormField this will solve your problem.
import 'package:flutter/material.dart';
class FormScreen extends StatefulWidget {
#override
State<StatefulWidget> createState() {
return FormScreenState();
}
}
class FormScreenState extends State<FormScreen> {
String _name;
String _email;
String _password;
String _url;
String _phoneNumber;
String _calories;
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
//create focusnodes here
FocusNode nameFocusNode= FocusNode();
FocusNode emailFocusNode= FocusNode();
Widget _buildName() {
return TextFormField(
focusNode:nameFocusNode,
autovalidateMode: nameFocusNode.hasFocus
? AutovalidateMode.always
: AutovalidateMode.disabled,
decoration: InputDecoration(labelText: 'Name'),
maxLength: 10,
validator: (String value) {
if (value.isEmpty) {
return 'Name is Required';
}
return null;
},
onSaved: (String value) {
_name = value;
},
);
}
............
Widget _buildEmail() {
return TextFormField(
focusNode:emailFocusNode,
autovalidateMode: nameFocusNode.hasFocus
? AutovalidateMode.always
: AutovalidateMode.disabled,
decoration: InputDecoration(labelText: 'Email'),
validator: (String value) {
if (value.isEmpty) {
return 'Email is Required';
}
if (!RegExp(
r"[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*#(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?")
.hasMatch(value)) {
return 'Please enter a valid email Address';
}
return null;
},
onSaved: (String value) {
_email = value;
},
);
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Form Demo")),
body: Container(
margin: EdgeInsets.all(24),
child: Form(
key: _formKey,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
_buildName(),
_buildEmail(),
_buildPassword(),
_builURL(),
_buildPhoneNumber(),
_buildCalories(),
SizedBox(height: 100),
RaisedButton(
child: Text(
'Submit',
style: TextStyle(color: Colors.blue, fontSize: 16),
),
onPressed: () {
if (!_formKey.currentState.validate()) {
return;
}
_formKey.currentState.save();
print(_name);
print(_email);
print(_phoneNumber);
print(_url);
print(_password);
print(_calories);
//Send to API
},
)
],
),
),
),
);
}
}
You should be not use Form widget and TextFormField if you want to display error one at a time. Instead use validation by controllers.
For Example
class MyHomePage extends StatefulWidget {
#override
MyHomePageState createState() {
return new MyHomePageState();
}
}
class MyHomePageState extends State<MyHomePage> {
final _text = TextEditingController();
bool _validate = false;
#override
void dispose() {
_text.dispose();
super.dispose();
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('TextField Demo'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('Error Showed if Field is Empty on Submit button Pressed'),
TextField(
controller: _text,
decoration: InputDecoration(
labelText: 'Enter the Value',
),
),
RaisedButton(
onPressed: () {
if(_text.text.length<=5){
// open dialog
}
},
child: Text('Submit'),
textColor: Colors.white,
color: Colors.blueAccent,
)
],
),
),
);
}
}
Form's validate() method will validate all the fields in it and it can't break. This is the source code in FormState.
bool _validate() {
bool hasError = false;
for (final FormFieldState<dynamic> field in _fields)
hasError = !field.validate() || hasError;
return !hasError;
}
If you want to validate the field one by one, you should use TextFormField's validate() manually.
import 'package:flutter/material.dart';
class FormScreen extends StatefulWidget {
#override
State<StatefulWidget> createState() {
return FormScreenState();
}
}
class FormScreenState extends State<FormScreen> {
String _name;
String _email;
String _password;
String _url;
String _phoneNumber;
String _calories;
List<GlobalKey<FormFieldState>> fieldKeys;
GlobalKey<FormFieldState> nameKey;
GlobalKey<FormFieldState> emailKey;
GlobalKey<FormFieldState> passwordKey;
GlobalKey<FormFieldState> urlKey;
GlobalKey<FormFieldState> phoneNumberKey;
GlobalKey<FormFieldState> caloriesKey;
#override
void initState() {
super.initState();
nameKey = GlobalKey<FormFieldState>();
emailKey = GlobalKey<FormFieldState>();
passwordKey = GlobalKey<FormFieldState>();
urlKey = GlobalKey<FormFieldState>();
phoneNumberKey = GlobalKey<FormFieldState>();
caloriesKey = GlobalKey<FormFieldState>();
fieldKeys = [
nameKey,
emailKey,
passwordKey,
urlKey,
phoneNumberKey,
caloriesKey,
];
}
bool validate() {
return fieldKeys.every((element) => element.currentState.validate());
}
void save() {
fieldKeys.forEach((element) => element.currentState.save());
}
Widget _buildName() {
return TextFormField(
key: nameKey,
// if you use autovalidateMode:AutovalidateMode.onUserInteractionit is showing validation message for the remaining fields too even before user starts entering the other fields
// then don't use any autovalidateMode.
// autovalidateMode: AutovalidateMode.onUserInteraction,
decoration: InputDecoration(labelText: 'Name'),
maxLength: 10,
validator: (String value) {
if (value.isEmpty) {
return 'Name is Required';
}
return null;
},
onSaved: (String value) {
_name = value;
},
);
}
Widget _buildEmail() {
return TextFormField(
key: emailKey,
decoration: InputDecoration(labelText: 'Email'),
validator: (String value) {
if (value.isEmpty) {
return 'Email is Required';
}
if (!RegExp(
r"[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*#(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?")
.hasMatch(value)) {
return 'Please enter a valid email Address';
}
return null;
},
onSaved: (String value) {
_email = value;
},
);
}
Widget _buildPassword() {
return TextFormField(
key: passwordKey,
decoration: InputDecoration(labelText: 'Password'),
keyboardType: TextInputType.visiblePassword,
validator: (String value) {
if (value.isEmpty) {
return 'Password is Required';
}
return null;
},
onSaved: (String value) {
_password = value;
},
);
}
Widget _buildURL() {
return TextFormField(
key: urlKey,
decoration: InputDecoration(labelText: 'Url'),
keyboardType: TextInputType.url,
validator: (String value) {
if (value.isEmpty) {
return 'URL is Required';
}
return null;
},
onSaved: (String value) {
_url = value;
},
);
}
Widget _buildPhoneNumber() {
return TextFormField(
key: phoneNumberKey,
decoration: InputDecoration(labelText: 'Phone number'),
keyboardType: TextInputType.phone,
validator: (String value) {
if (value.isEmpty) {
return 'Phone number is Required';
}
return null;
},
onSaved: (String value) {
_url = value;
},
);
}
Widget _buildCalories() {
return TextFormField(
key: caloriesKey,
decoration: InputDecoration(labelText: 'Calories'),
keyboardType: TextInputType.number,
validator: (String value) {
int calories = int.tryParse(value);
if (calories == null || calories <= 0) {
return 'Calories must be greater than 0';
}
return null;
},
onSaved: (String value) {
_calories = value;
},
);
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Form Demo")),
body: Container(
margin: EdgeInsets.all(24),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
_buildName(),
_buildEmail(),
_buildPassword(),
_buildURL(),
_buildPhoneNumber(),
_buildCalories(),
SizedBox(height: 100),
RaisedButton(
child: Text(
'Submit',
style: TextStyle(color: Colors.blue, fontSize: 16),
),
onPressed: () {
if (!validate()) {
return;
}
save();
print(_name);
print(_email);
print(_phoneNumber);
print(_url);
print(_password);
print(_calories);
//Send to API
},
)
],
),
),
);
}
}
I really liked Thusithas idea but since the widget was not rebuilt when the focus was changed, the autoValidateMode was not changed either.
So I created an observable variable hasFocus, integrated a listener on focus changes (see https://stackoverflow.com/a/68675855/17203788) and wrapped the widget with Obx so it would re-render on focus changes.
In my implementation I used flutter_form_builder but the concept is also applicable to the classic Material form.
import 'package:flutter/material.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:get/get.dart';
class FormInputField extends StatelessWidget {
/// Unique name of this input field
final String name;
/// Validator for this input field
final FormFieldValidator<String>? validator;
/// Observable boolean whether this text field currently has focus
final RxBool _hasFocus = false.obs;
FormInputField({
required this.name,
this.validator,
Key? key,
}) : super(key: key);
#override
Widget build(BuildContext context) {
return Focus(
onFocusChange: (hasFocus) {
// Update observable
_hasFocus(hasFocus);
},
child: Obx(() {
return FormBuilderTextField(
name: name,
autovalidateMode: _hasFocus.value
? AutovalidateMode.always
: AutovalidateMode.disabled,
validator: validator,
);
}),
);
}
}
Alternatively you could also use a stateful widget and call setState in the onFocusChanged-callback.
Usage:
import 'package:flutter/material.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
class Home extends StatefulWidget {
const Home({Key? key}) : super(key: key);
#override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
/// Key to identify the form
final GlobalKey<FormBuilderState> _formKey = GlobalKey();
#override
Widget build(BuildContext context) {
return Scaffold(
body: FormBuilder(
key: _formKey,
child: Column(
children: [
FormInputField(
name: 'username',
validator: FormBuilderValidators.compose([
FormBuilderValidators.required(
context,
errorText: 'Name is required',
),
]),
),
FormInputField(
name: 'email',
validator: FormBuilderValidators.compose([
FormBuilderValidators.required(
context,
errorText: 'Email is required',
),
FormBuilderValidators.email(
context,
errorText: 'This is not a valid email',
),
]),
),
ElevatedButton(
onPressed: () {
// Save and validate the form
if (!_formKey.currentState!.saveAndValidate()) {
return;
}
// Extract values from form
final Map<String, dynamic> values = _formKey.currentState!.value;
String username = values['username'];
String email = values['email'];
print(username);
print(email);
},
child: const Text('Submit'),
),
],
),
),
);
}
}
One of the requirements of my current project is a multi-page sign-up form, with each page performing its own independent validation. The way I ended up deciding to implement this is by splitting it up into several smaller forms, which you can pass some functions in as parameters to be executed when the form validation and/or value change runs. Below is an example of one of the "mini-forms" I created to implement this. This one collects information about the user's name.
import "package:flutter/material.dart";
class SignUpNameForm extends StatefulWidget {
final Function(String) onFirstNameChange;
final Function(String) onLastNameChange;
final Function(bool) onFirstNameValidationStatusUpdate;
final Function(bool) onLastNameValidationStatusUpdate;
final String firstNameInitialValue;
final String lastNameInitialValue;
SignUpNameForm(
{this.onFirstNameChange,
this.onLastNameChange,
this.onFirstNameValidationStatusUpdate,
this.onLastNameValidationStatusUpdate,
this.firstNameInitialValue,
this.lastNameInitialValue});
#override
_SignUpNameFormState createState() => _SignUpNameFormState();
}
class _SignUpNameFormState extends State<SignUpNameForm> {
final _formKey = GlobalKey<FormState>();
TextEditingController _firstName;
TextEditingController _lastName;
bool _editedFirstNameField;
bool _editedLastNameField;
#override
Widget build(BuildContext context) {
_firstName.text = widget.firstNameInitialValue;
_lastName.text = widget.lastNameInitialValue;
return Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
TextFormField(
controller: _firstName,
autovalidate: true,
decoration: InputDecoration(
hintText: "First Name",
),
keyboardType: TextInputType.text,
onChanged: (String value) {
_editedFirstNameField = true;
widget.onFirstNameChange(value);
},
validator: (String value) {
String error;
if (_editedFirstNameField) {
error = value.isEmpty ? "This field is required" : null;
bool isValid = error == null;
widget.onFirstNameValidationStatusUpdate(isValid);
}
return error;
},
),
TextFormField(
controller: _lastName,
autovalidate: true,
decoration: InputDecoration(
hintText: "Last Name",
),
keyboardType: TextInputType.text,
onChanged: (String value) {
_editedLastNameField = true;
widget.onLastNameChange(value);
},
validator: (String value) {
String error;
if (_editedLastNameField) {
error = value.isEmpty ? "This field is required" : null;
bool isValid = error == null;
widget.onLastNameValidationStatusUpdate(isValid);
}
return error;
},
),
],
),
);
}
#override
void initState() {
super.initState();
_firstName = new TextEditingController();
_lastName = new TextEditingController();
_editedFirstNameField = false;
_editedLastNameField = false;
}
#override
void dispose() {
super.dispose();
_firstName.dispose();
_lastName.dispose();
}
}
Then, in my widget that displays the form components, I do something like this.
class _SignUpFormState extends State<SignUpForm> {
String _firstName;
bool _firstNameIsValid;
String _lastName;
bool _lastNameIsValid;
int current;
final maxLength = 4;
final minLength = 0;
#override
Widget build(BuildContext context) {
// create the widget
return Container(
child: _showFormSection(),
);
}
/// Show the appropriate form section
Widget _showFormSection() {
Widget form;
switch (current) {
case 0:
form = _showNameFormSection();
break;
case 1:
form = _showEmailForm();
break;
case 2:
form = _showPasswordForm();
break;
case 3:
form = _showDobForm();
break;
}
return form;
}
// shows the name section of the form.
Widget _showNameFormSection() {
return Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
child: SignUpNameForm(
firstNameInitialValue: _firstName,
lastNameInitialValue: _lastName,
onFirstNameChange: (String value) {
setState(() {
_firstName = value;
});
},
onFirstNameValidationStatusUpdate: (bool value) {
setState(() {
_firstNameIsValid = value;
});
},
onLastNameChange: (String value) {
setState(() {
_lastName = value;
});
},
onLastNameValidationStatusUpdate: (bool value) {
setState(() {
_lastNameIsValid = value;
});
},
),
),
Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
FlatButton(
child: Text("Next"),
onPressed: (_firstNameIsValid && _lastNameIsValid)
? _showNextSection
: null,
),
],
),
)
],
),
);
}
/// Shows the next section in the form
void _showNextSection() {
print("Current Section: " + current.toString());
setState(() {
if (current >= maxLength) {
current = maxLength;
} else {
current++;
}
});
print("Next Section: " + current.toString());
}
/// Show the previous section of the form
void _showPreviousSection() {
print("Current Section:" + current.toString());
setState(() {
if (current <= minLength) {
current = minLength;
} else {
current--;
}
print("Previous Section: " + current.toString());
});
}
#override
void initState() {
super.initState();
current = 0;
_firstName = "";
_firstNameIsValid = false;
_lastName = "";
_lastNameIsValid = false;
// other initializations
}
}
As you can see here, I pass in functions to extract the values of the user's name, as well as the status of the validation, and use that to determine whether or not I should enable the "next" button in the form handler widget. This is now causing a problem, specifically because the functions I pass into the "mini-form" invokes initState() while the widget is being rebuilt.
How might I go about handling this? Or, is there a better way I can go about implementing this multi-Page form that is cleaner?
Thanks.
build() should be fast and idempotent. You should not be calling setState() inside a build. Imagine build() is being called 60 times a second (although it won't be thanks to optimizations) and you'll have the proper mindset.
I'm new on Flutter.
I'm trying to disabled button while I compile textFormField and it is invalid.
My problem is that it works only if I click on "confirm" on keyboard and not while I compile my input.
So would like disable button while I write the input.
I've done a pastebin for example:
https://pastebin.com/q5WuwrCm
class AddCartButton extends StatefulWidget {
#override
State<StatefulWidget> createState() {
return AddCartButtonState();
}
}
class AddCartButtonState extends State<AddCartButton>{
TextEditingController myController = TextEditingController();
bool isValid = false;
#override
Widget build(BuildContext context) {
void _addToCart(){
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text("QUANTITY"),
content: Column(
children: <Widget>[
TextFormField(
controller: myController,
decoration: new InputDecoration(labelText: "quantity"),
keyboardType: TextInputType.numberWithOptions(decimal: true),
inputFormatters: <TextInputFormatter>[],
autovalidate: true,
validator: (value) {
if (value.isEmpty) {
isValid = false;
return "the quantity cannot be empty";
} else if (double.tryParse(value) == null) {
isValid = false;
return "the quantity must be valid number";
} else {
isValid = true;
return null;
}
}
)
],
),
actions: <Widget>[
FlatButton(
disabledTextColor: Colors.grey,
child: Text("add"),
onPressed: isValid ? () { print("is valid"); }: null
)
],
);
},
);
}
}
}
You can use addListener() function on your TextEditingController myController.
myController.addListener((){
//With this, you can "listen" all the changes on your text while
//you are typing on input
print("value: ${myController.text}");
//use setState to rebuild the widget
setState(() {
//you can check here if your text is valid or no
//_isValidText() is just an invented function that returns
//a boolean representing if the text is valid or not
if(_isValidText(myController.text)) isValid = true;
else isValid = false;
});
});
If I understood what you want to achieve the following code works for you. The conditions I set are the same as your validation rules:
class MyApp extends StatefulWidget {
#override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
TextEditingController _textEditingController = TextEditingController();
Function _submitFunction;
// We need a reusable timer and it needs to not be null so that we can cancel it from th start.
Timer _timer = Timer(Duration(milliseconds: 1), () => print('initialTimer'));
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Auto disable button'),
),
body: Center(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextFormField(
controller: _textEditingController,
onChanged: (text) {
setState(() {
_submitFunction = null;
});
restartTimer();
},
),
RaisedButton(
child: Text('Submit'),
onPressed: _submitFunction,
),
],
),
),
)
);
}
void submitForm(){
print(_textEditingController.text);
}
void enableButton(){
if(_textEditingController.text != ''){
setState(() {
_submitFunction = submitForm;
});
}
}
void restartTimer(){
_timer.cancel();
_timer = Timer(Duration(milliseconds: 1500), enableButton);
}
}
You can use this method .
bool isEnable = false;
void validateButton() {
bool isValid = true;
isValid = userEmail.isNotEmpty &&
userPassword.isNotEmpty &&
validateEmail(userEmail) &&
userPassword.length >= 8;
setState(() {
isEnable = isValid;
});
}
now in your Textfield onChanged method you have to call this function
like This
onChanged: (email) {
userEmail = email;
setState(() {});
validateButton();
},
and in your Login Button
isEnable?ActiveButton():DisableButton()
I have TextFormField which has its own controller. I also have a disabled button that I would like to enable after any input inside TextFormField. The problem is that I managed to enable the button however only when I close the keyboard. For example, if I'm typing a word in a session it won't enable the button unless I close the keyboard. I would like to enable the button after the first character.
This is the code:
final TextEditingController passwordController = new TextEditingController();
TextFormField(
keyboardType: TextInputType.visiblePassword,
controller: passwordController,
),
RaisedButton(
onPressed: (passwordController.text== "") ? null : () {
setState(() {
_isLoading = true;
});
signIn(email, passwordController.text);
},
),
Is this what you're looking for?
final TextEditingController _controller = TextEditingController();
bool _isLoading = true;
// it tracks if TextField is enabled or disabled
bool _enabled = false;
#override
void initState() {
super.initState();
_controller.addListener(() { // you need to add listener like this
setState(() {
if (_controller.text.length > 0)
_enabled = true;
else
_enabled = false;
});
});
}
Widget build(context) {
return Scaffold(
body: Column(
children: <Widget>[
SizedBox(height: 50),
TextFormField(controller: _controller),
RaisedButton(
onPressed: !_enabled ? null : () => setState(() => _isLoading = true),
),
],
),
);
}
Based on what you're looking for, I created a boolean called isEnabled and set the initial value to false. This should work for you!
bool isEnabled;
final TextEditingController passwordController = new TextEditingController();
#override
void initState() {
super.initState();
passwordController.addListener(() {
setState(() {
if (passwordController.text.length > 0) {
isEnabled = true;
} else {
isEnabled = false;
}
});
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: <Widget>[
TextFormField(
keyboardType: TextInputType.visiblePassword,
controller: passwordController,
),
RaisedButton(
child: Text('Click Me'),
onPressed: isEnabled ? () {
//print('Button has been pressed');
setState(() {
_isLoading = true;
});
signIn(email, passwordController.text);
} : null,
),
],
),
);
}
This worked:
var password = '';
TextFormField(
keyboardType: TextInputType.visiblePassword,
controller: passwordController,
onChanged: (value){
setState(() {
password = value;
});
},
),
RaisedButton(
onPressed: (password == "") ? null : () {
setState(() {
_isLoading = true;
});
signIn(email, password);
},
),