import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: HomePage(), debugShowCheckedModeBanner: false, ); } } class HomePage extends StatefulWidget { @override _HomePageState createState() => _HomePageState(); } class _HomePageState extends State<HomePage> { var _formKey = GlobalKey<FormState>(); // Function to handle form submission void _submit() { // Validate the form fields final isValid = _formKey.currentState!.validate(); if (!isValid) { // If the form is not valid, return without doing anything return; } else { // If the form is valid, show a success message ScaffoldMessenger.of(context).showSnackBar( SnackBar( backgroundColor: Colors.green, content: Text( "Successfully logged in", style: TextStyle( color: Colors.white, ), ), ), ); } // Save the form state _formKey.currentState!.save(); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text("Form Validation"), leading: Icon(Icons.filter_vintage), ), body: Padding( padding: const EdgeInsets.all(16.0), child: Form( key: _formKey, child: Column( children: <Widget>[ Text( "Form-Validation In Flutter", style: TextStyle(fontSize: 24.0, fontWeight: FontWeight.bold), ), SizedBox( height: MediaQuery.of(context).size.width * 0.1, ), TextFormField( decoration: InputDecoration(labelText: 'E-Mail'), keyboardType: TextInputType.emailAddress, validator: (value) { if (value!.isEmpty || !RegExp(r"^[a-zA-Z0-9.a-zA-Z0-9.!#$%&'*+-/=?^_`{|}~]+@[a-zA-Z0-9]+\.[a-zA-Z]+") .hasMatch(value)) { return 'Enter a valid email!'; } return null; }, ), SizedBox( height: MediaQuery.of(context).size.width * 0.1, ), TextFormField( decoration: InputDecoration(labelText: 'Password'), obscureText: true, validator: (value) { if (value!.isEmpty) { return 'Enter a valid password!'; } return null; }, ), SizedBox( height: MediaQuery.of(context).size.width * 0.1, ), SizedBox( width: double.infinity, // Set the width to fill the parent child: ElevatedButton( style: ButtonStyle( backgroundColor: WidgetStateProperty.all(Colors.green), ), child: Padding( padding: const EdgeInsets.all(8.0), child: Text( "Submit", style: TextStyle( color: Colors.white, fontSize: 20, ), ), ), onPressed: () => _submit(), ), ), ], ), ), ), ); } }