Textfield validation in Flutter


I am working on Flutter TextField widget. I want to show an error message below the TextField widget if the user does not fill that TextField. I only have to use TextField Widget not TextFormField in this case.


A Minimal Example of what you Want:

class MyHomePage extends StatefulWidget {
  MyHomePageState createState() {
    return new MyHomePageState();

class MyHomePageState extends State<MyHomePage> {
  final _text = TextEditingController();
  bool _validate = false;

  void dispose() {

  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'),
              controller: _text,
              decoration: InputDecoration(
                labelText: 'Enter the Value',
                errorText: _validate ? 'Value Can\'t Be Empty' : null,
              onPressed: () {
                setState(() {
                  _text.text.isEmpty ? _validate = true : _validate = false;
              child: Text('Submit'),
              textColor: Colors.white,
              color: Colors.blueAccent,

Answered By – anmol.majhail

Answer Checked By – Timothy Miller (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *