How to perform text field validation in Flutter

In Flutter, we can achieve text field validation by using TextEditingController class. TextEditingController in a text field can gain control over the input text and implement various validation techniques. It is a valuable tool for ensuring data integrity, handling user input errors, and creating interactive forms in Flutter applications.

Implementation

We can follow the instructions given below to implement validation in the text field using TextEditingController

Create basic UI

Firstly, we can create a StatefulWidget subclass that will contain both TextField and ElevatedButton

class ValidationExample extends StatefulWidget {
  const ValidationExample({Key key, this.onSubmit}) : super(key: key);
  final ValueChanged<String> onSubmit;

  @override
  State<ValidationExample> createState() => _ValidationExampleState();
}

class _ValidationExampleState extends State<ValidationExample> {
  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: [
        TextField(
          decoration: InputDecoration(
            labelText: 'Enter your Name',
            // TODO: add errorHint
          ),
        ),
        ElevatedButton(
          // TODO: implement callback
          onPressed: () {},
          style: ButtonStyle(
                 
backgroundColor:MaterialStateProperty.all(Colors.red),
                ),
          child: Text(
            'Submit',
            style: Theme.of(context).textTheme.headline6,
          ),
        )
      ],
    );
  }
}

If we run the above code in a Flutter project, we will get the following output with a simple TextField and ElevatedButton without any validation.

Simple UI without Validation
Simple UI without Validation

Create a TextEditingController

We can create a variable of TextEditingController class by adding the following lines in the state subclass:

class _ValidationExampleState extends State<ValidationExample> {
  // create a TextEditingController
  final _controller = TextEditingController();

  // dispose it when the widget is unmounted
  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  ...
}

After creating a TextEditingController variable, we can add it to the TextField just by adding the following lines.

TextField(
  // use this to control the text field
  controller: _controller,
  decoration: InputDecoration(
    labelText: 'Enter your Name',
  ),
),

Define the logic

We can define a getter variable  _errorText where we will define all the logic for validation.

String get _errorText {
  // at any time, we can get the text from _controller.value.text
  final text = _controller.value.text;
  // Note: you can do your own custom validation here
  // Move this logic this outside the widget for more testable code
  if (text.isEmpty) {
    return 'Name can\'t be empty';
  }
  if (text.length < 3) {
    return 'Name is too short. Minimum 3 characters required.';
  }
  // return null if the text is valid
  return null;
}

Now we can pass it on to the TextField.

After placing _errorText() we can apply some logic to onPressed the callback function of the elevated button.

onPressed: _controller.value.text.isNotEmpty
      ? _submit
      : null,

To apply the above logic, we have to define the _submit() function as well.

void _submit() {
  // if there is no error text
  if (_errorText == null) {
    // notify the parent widget via the onSubmit callback
    widget.onSubmit(_controller.value.text);
  }
}

Reflect the changes

Now, we have to rebuild the whole widget tree to reflect the changes whenever the text changes in the text field by adding the following line in the TextField widget.

onChanged: (_) => setState(() {}),

Output

We get the following output by putting together the code explained above. 

bmFtZTogbmV3YXBwCmRlc2NyaXB0aW9uOiBBIG5ldyBGbHV0dGVyIHByb2plY3QuCiMgVGhlIGZvbGxvd2luZyBsaW5lIHByZXZlbnRzIHRoZSBwYWNrYWdlIGZyb20gYmVpbmcgYWNjaWRlbnRhbGx5IHB1Ymxpc2hlZCB0bwojIHB1Yi5kZXYgdXNpbmcgYGZsdXR0ZXIgcHViIHB1Ymxpc2hgLiBUaGlzIGlzIHByZWZlcnJlZCBmb3IgcHJpdmF0ZSBwYWNrYWdlcy4KcHVibGlzaF90bzogJ25vbmUnICMgUmVtb3ZlIHRoaXMgbGluZSBpZiB5b3Ugd2lzaCB0byBwdWJsaXNoIHRvIHB1Yi5kZXYKCiMgVGhlIGZvbGxvd2luZyBkZWZpbmVzIHRoZSB2ZXJzaW9uIGFuZCBidWlsZCBudW1iZXIgZm9yIHlvdXIgYXBwbGljYXRpb24uCiMgQSB2ZXJzaW9uIG51bWJlciBpcyB0aHJlZSBudW1iZXJzIHNlcGFyYXRlZCBieSBkb3RzLCBsaWtlIDEuMi40MwojIGZvbGxvd2VkIGJ5IGFuIG9wdGlvbmFsIGJ1aWxkIG51bWJlciBzZXBhcmF0ZWQgYnkgYSArLgojIEJvdGggdGhlIHZlcnNpb24gYW5kIHRoZSBidWlsZGVyIG51bWJlciBtYXkgYmUgb3ZlcnJpZGRlbiBpbiBmbHV0dGVyCiMgYnVpbGQgYnkg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MgZnJvbSBwYWNrYWdlIGRlcGVuZGVuY2llcywKICAjIHNlZSBodHRwczovL2ZsdXR0ZXIuZGV2L2N1c3RvbS1mb250cy8jZnJvbS1wYWNrYWdlcwo=

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved