Check Textfield Text Input Is Empty Or Not Using Flutter App

admin_img Posted By Bajarangi soft , Posted On 13-10-2020

We can do this by getting entered value from TextField and check whether the entered value is equal equal to empty or not and on certain task we can print Alert dialog box to notifying user that you have not filled all the TextField boxes.

Check Textfield Text Input Is Empty Or Not Using Flutter App

Check Textfield Text Input Is Empty Or Not
Complete Code For Check Textfield Text Input Is Empty Or Not In Flutter 
main.dart

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        debugShowCheckedModeBanner: false,
        home: Scaffold(
            appBar: AppBar(
                centerTitle: true,
                backgroundColor: Colors.white,
                title: Text('Get Text Field Value',style: TextStyle(
                    color: Colors.black
                ),)),
            body: Center(
                child: CheckTextField()
            )
        )
    );
  }
}

class CheckTextField extends StatefulWidget {
  CheckTextFieldWidget createState() => CheckTextFieldWidget();
}
class CheckTextFieldWidget extends State {
  final textController_1 = TextEditingController();
  final textController_2 = TextEditingController();
  final textController_3 = TextEditingController();
  checkTextFieldEmptyOrNot(){
    String text1,text2,text3 ;
    text1 = textController_1.text ;
    text2 = textController_2.text ;
    text3 = textController_3.text ;
    if(text1 == '' || text2 == '' || text3 == '')
    {
      print('Text Field is empty, Please Fill All Data');
    }
    else{
      print('Not Empty, All Text Input is Filled.');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Container(
                  width: 280,
                  padding: EdgeInsets.all(10.0),
                  child: TextField(
                    controller: textController_1,
                    autocorrect: true,
                    decoration: InputDecoration(hintText: 'Enter First Name'),
                  )
              ),

              Container(
                  width: 280,
                  padding: EdgeInsets.all(10.0),
                  child: TextField(
                    controller: textController_2,
                    autocorrect: true,
                    decoration: InputDecoration(hintText: 'Enter Last Name'),
                  )
              ),

              Container(
                  width: 280,
                  padding: EdgeInsets.all(10.0),
                  child: TextField(
                    controller: textController_3,
                    autocorrect: true,
                    decoration: InputDecoration(hintText: 'Enter Email'),
                  )
              ),

              RaisedButton(
                onPressed: checkTextFieldEmptyOrNot,
                color: Colors.white,
                textColor: Colors.black,
                padding: EdgeInsets.fromLTRB(10, 10, 10, 10),
                child: Text('Check TextField is Empty or Not'),
              ),

            ],
          ),
        ));
  }
}

Related Post