Login and Signup Form in Flutter

 import 'package:flutter/material.dart';


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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    const appTitle = 'Login';

    return MaterialApp(
      title: appTitle,
      home: Scaffold(
        appBar: AppBar(title: const Text(appTitle), backgroundColor: Colors.blue,),
       
        body: const MyCustomForm(),
      ),
    );
  }
}

// Create a Form widget.
class MyCustomForm extends StatefulWidget {
  const MyCustomForm({super.key});

  @override
  MyCustomFormState createState() {
   
    return MyCustomFormState();
  }
}

// Create a corresponding State class.
// This class holds data related to the form.
class MyCustomFormState extends State<MyCustomForm> {
  // Create a global key that uniquely identifies the Form widget
  // and allows validation of the form.
  //
  // Note: This is a GlobalKey<FormState>,
  // not a GlobalKey<MyCustomFormState>.
  final _formKey = GlobalKey<FormState>();
 bool isStorageFull=false;
  @override
  Widget build(BuildContext context) {
    // Build a Form widget using the _formKey created above.
    return Form(
      key: _formKey,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          SizedBox(
            width: 500,
            child: TextFormField(
              autofocus: true,

              decoration: InputDecoration(
                border: OutlineInputBorder(),
                hintText: 'Type Email Or Mobile',
              ),
              // The validator receives the text that the user has entered.
             
              validator: (value) {
               
                if (value == null || value.isEmpty) {
                  return 'Please enter email or mobile';
                }

                if (
                    RegExp(
                      r'(^(?:[+0]9)?[0-9]{10,12}$)',
                    ).hasMatch(value)) {
                  //return 'Please enter email or mobile';
                  isStorageFull=true;
                }
               
                if (
                    RegExp(
                      r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$",
                    ).hasMatch(value)) {
                 
                  isStorageFull=true;
                 
                }
                if(isStorageFull==false)
                {
                  return 'Please enter email or mobile';
                }

               

                return null;
               
              },
             
            ),
          ),

          SizedBox(height: 20),

          // text field 1 end
          SizedBox(
            width: 500,
            child: TextFormField(
              // for password field
              obscureText: true,
              enableSuggestions: false,
              autocorrect: false,
               
              // for password field
              decoration: InputDecoration(
                border: OutlineInputBorder(),
                hintText: 'Password',
              ),
              // The validator receives the text that the user has entered.
              validator: (value) {
                if (value == null || value.isEmpty) {
                  return 'Please enter password';
                }
                return null;
              },
            ),
          ),

          // text field 2 end
         
         
         Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          // button1
          Padding(
            padding: const EdgeInsets.symmetric(vertical: 16),

            child: ElevatedButton(
              onPressed: () {
                // Validate returns true if the form is valid, or false otherwise.
                if (_formKey.currentState!.validate()) {
                  // If the form is valid, display a snackbar. In the real world,
                  // you'd often call a server or save the information in a database.
                  ScaffoldMessenger.of(context).showSnackBar(
                    const SnackBar(content: Text('Processing Data')),
                  );
                  isStorageFull=false;
                }
              },
              child: const Text('Login'),
            ),
          ),
          // button1
          // button2
Padding(
            padding: const EdgeInsets.symmetric(vertical: 16),

            child: ElevatedButton(
              onPressed: () {
                // Validate returns true if the form is valid, or false otherwise.
                if (_formKey.currentState!.validate()) {
                  // If the form is valid, display a snackbar. In the real world,
                  // you'd often call a server or save the information in a database.
                  ScaffoldMessenger.of(context).showSnackBar(
                    const SnackBar(content: Text('Processing Data')),
                  );
                  isStorageFull=false;
                }
              },
              child: const Text('Forgot Password'),
            ),
          ),
          // button2
         


         ]
             ),
 ElevatedButton(
          child: const Text('New User? Register Now.'),
          onPressed: () {
            Navigator.push(
              context,
              MaterialPageRoute(builder: (context) => const MyApp_register()),
            );
          },
        ),
 

         
         
         
         
         
         
         
         
         
         
         
           
         
         
         
         
         
         
         
         
         
        ],
      ),
    );
  }
}
// register page
class MyApp_register extends StatelessWidget {
  const MyApp_register({super.key});

  @override
  Widget build(BuildContext context) {
    const appTitle = 'Register';

    return Scaffold(
        appBar: AppBar(title: const Text(appTitle), backgroundColor: Colors.blue,),
       
        body: const MyCustomForm_register(),
     
    );
  }
}

// Create a Form widget.
class MyCustomForm_register extends StatefulWidget {
  const MyCustomForm_register({super.key});

  @override
  MyCustomFormState_register createState() {
   
    return MyCustomFormState_register();
  }
}

// Create a corresponding State class.
// This class holds data related to the form.
class MyCustomFormState_register extends State<MyCustomForm_register> {
  // Create a global key that uniquely identifies the Form widget
  // and allows validation of the form.
  //
  // Note: This is a GlobalKey<FormState>,
  // not a GlobalKey<MyCustomFormState>.
  final _formKey = GlobalKey<FormState>();
 bool isStorageFull=false;
  @override
  Widget build(BuildContext context) {
    // Build a Form widget using the _formKey created above.
    return Form(
      key: _formKey,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [

        SizedBox(
            width: 500,
            child: TextFormField(
              autofocus: true,

              decoration: InputDecoration(
                border: OutlineInputBorder(),
                hintText: 'Type Your Name',
              ),
              // The validator receives the text that the user has entered.
             
              validator: (value) {
               
                if (value == null || value.isEmpty) {
                  return 'Please enter name';
                }
                return null;
               
              },
             
            ),
          ),

          SizedBox(
            width: 500,
            child: TextFormField(
              autofocus: true,

              decoration: InputDecoration(
                border: OutlineInputBorder(),
                hintText: 'Email',
              ),
              // The validator receives the text that the user has entered.
             
              validator: (value) {
               
                if (value == null || value.isEmpty) {
                  return 'Please enter email';
                }

               
               
                if (
                    RegExp(
                      r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$",
                    ).hasMatch(value)) {
                 
                  isStorageFull=true;
                 
                }
                if(isStorageFull==false)
                {
                  return 'Please enter email';
                }

               

                return null;
               
              },
             
            ),
          ),

          SizedBox(
            width: 500,
            child: TextFormField(
              autofocus: true,

              decoration: InputDecoration(
                border: OutlineInputBorder(),
                hintText: 'Mobile Number',
              ),
              // The validator receives the text that the user has entered.
             
              validator: (value) {
               
                if (value == null || value.isEmpty) {
                  return 'Please enter mobile number';
                }

                if (
                    RegExp(
                      r'(^(?:[+0]9)?[0-9]{10,12}$)',
                    ).hasMatch(value)) {
                  //return 'Please enter email or mobile';
                  isStorageFull=true;
                }
               
               
                if(isStorageFull==false)
                {
                  return 'Please enter mobile number';
                }

               

                return null;
               
              },
             
            ),
          ),


            SizedBox(
            width: 500,
            child: TextFormField(
              autofocus: true,

              decoration: InputDecoration(
                border: OutlineInputBorder(),
                hintText: 'Pincode',
              ),
              // The validator receives the text that the user has entered.
             
              validator: (value) {
               
                if (value == null || value.isEmpty) {
                  return 'Please enter pincode';
                }
                return null;
               
              },
             
            ),
          ),


          SizedBox(height: 20),

          // text field 1 end
          SizedBox(
            width: 500,
            child: TextFormField(
              // for password field
              obscureText: true,
              enableSuggestions: false,
              autocorrect: false,
               
              // for password field
              decoration: InputDecoration(
                border: OutlineInputBorder(),
                hintText: 'Password',
              ),
              // The validator receives the text that the user has entered.
              validator: (value) {
                if (value == null || value.isEmpty) {
                  return 'Please enter password';
                }
                return null;
              },
            ),
          ),

          // text field 2 end
         
         
         Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          // button1
          Padding(
            padding: const EdgeInsets.symmetric(vertical: 16),

            child: ElevatedButton(
              onPressed: () {
                // Validate returns true if the form is valid, or false otherwise.
                if (_formKey.currentState!.validate()) {
                  // If the form is valid, display a snackbar. In the real world,
                  // you'd often call a server or save the information in a database.
                  ScaffoldMessenger.of(context).showSnackBar(
                    const SnackBar(content: Text('Processing Data')),
                  );
                  isStorageFull=false;
                }
              },
              child: const Text('Login'),
            ),
          ),
          // button1
          // button2
Padding(
            padding: const EdgeInsets.symmetric(vertical: 16),

            child: ElevatedButton(
              onPressed: () {
                // Validate returns true if the form is valid, or false otherwise.
                if (_formKey.currentState!.validate()) {
                  // If the form is valid, display a snackbar. In the real world,
                  // you'd often call a server or save the information in a database.
                  ScaffoldMessenger.of(context).showSnackBar(
                    const SnackBar(content: Text('Processing Data')),
                  );
                  isStorageFull=false;
                }
              },
              child: const Text('Forgot Password'),
            ),
          ),
          // button2
         


         ]
             ),
 
 

      ElevatedButton(
          onPressed: () {
            Navigator.pop(context);
          },
          child: const Text('Go back!'),
        ),
 
         
               
         
       
         
        ],
      ),

    );
  }
}






Comments