Search
 
SCRIPT & CODE EXAMPLE
 

DART

text field validation in flutter

TextFormField(
  // The validator receives the text that the user has entered.
  validator: (value) {
    if (value == null || value.isEmpty) {
      return 'Please enter some text';
    }
    return null;
  },
),

/*
	The TextFormField widget renders a material design 
    text field and can display validation errors when 
    they occur.

	Validate the input by providing a validator() function 
    to the TextFormField. If the user’s input isn’t valid, 
    the validator function returns a String containing an 
    error message. If there are no errors, the validator 
    must return null.

	For this example, We create a validator that ensures the TextFormField 
    isn’t empty. If it is empty, then we return a friendly error message.
*/
Comment

flutter input validation


// First Add this 4 line to create contoller

final TextEditingController _emailController = TextEditingController();
final TextEditingController _passwordController = TextEditingController();

String get _email => _emailController.text;
String get _password => _passwordController.text;

//Validator

bool submitEnabled =
        _email.isNotEmpty && _password.isNotEmpty && (_password.length >= 5);
        
//*****************
// Inputs ***set as you want

TextField(
          controller: _passwordController,
           onChanged: (email) => _updateState(),
           )

TextField(
          controller: _emailController,
           onChanged: password => _updateState(),
           )
//****************

// for Button
onPressed: submitEnabled ? _submit : null,

//check textinput state every change for the enable button
 _updateState() {
    setState(() {});
  }
Comment

PREVIOUS NEXT
Code Example
Dart :: floting action button small size 
Dart :: dart codeunits 
Dart :: dart convert int to string 
Dart :: round off in dart 
Dart :: how to give bottom padding in Listview in flutter 
Dart :: dart super constructor 
Dart :: dart compare two lists 
Dart :: Dart set list spread operator 
Dart :: flutter iOS & Android chnage package name & app name 
Dart :: flutter listtile selected 
Dart :: drawerheader height flutter 
Dart :: flutter add text on image 
Dart :: flutter inner box shadow plugin 
Dart :: flutter animatedalign 
Dart :: flutter debugprint 
Dart :: flutter copy file 
Dart :: listview space between items flutter 
Dart :: dartlang tuple 
Dart :: flutter single line list 
Dart :: textbutton flutter 
Dart :: flutter compute 
Dart :: dart check runtime type 
Dart :: how to pass a double value from text field using flutter 
Dart :: dart keybord input 
Dart :: runapp in flutter 
Dart :: flutter containerborder 
Dart :: flutter logo duration 
Dart :: how to color text in flutter 
Dart :: install fvm in flutter using pub package 
Dart :: flutter add checkbox 
ADD CONTENT
Topic
Content
Source link
Name
5+6 =