To design and implement a Flutter app that performs form validation and error handling.
To understand the concept of form validation and error handling in Flutter.
- Flutter SDK: version 2.0.0 or higher
- Dart SDK: version 2.12.0 or higher
- IDE: Visual Studio Code (latest version) or android studio (latest version)
- Operating System: Windows (7 or higher), macOS (10.12 or higher), or Linux (Ubuntu, Debian, Fedora, CentOS, or similar)
-
Create a new Flutter project by running the following command in your terminal:
flutter create my_flutter_app
The command creates a Flutter project directory called
my_flutter_app
that contains a simple demo app that uses Material Components. -
Change to the Flutter project directory.
cd my_flutter_app
-
Open the
lib/main.dart
file in your Flutter project. -
Replace the existing code with the following code snippet:
import 'package:flutter/material.dart'; void main() => runApp(const MainApp()); class MainApp extends StatelessWidget { const MainApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: const Text('Form Validation')), body: const SingleChildScrollView( child: RegisterForm(), ), ), ); } } class RegisterForm extends StatefulWidget { const RegisterForm({super.key}); @override State<RegisterForm> createState() => _RegisterFormState(); } class _RegisterFormState extends State<RegisterForm> { final _formKey = GlobalKey<FormState>(); final _nameController = TextEditingController(), _emailController = TextEditingController(); @override Widget build(BuildContext context) { return Form( key: _formKey, child: Column( children: <Widget>[ Padding( padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 16), child: TextFormField( controller: _nameController, decoration: const InputDecoration(labelText: 'Name'), validator: (value) { if (value == null || value.isEmpty) { return 'Please enter your name'; } if (!RegExp(r'^[a-zA-Z][a-zA-Z\s]*$').hasMatch(value)) { return 'Name can only contain letters and spaces in between'; } return null; }, ), ), Padding( padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 16), child: TextFormField( controller: _emailController, decoration: const InputDecoration(labelText: 'Email'), validator: (value) { if (value == null || value.isEmpty) { return 'Please enter your email'; } if (!RegExp( r'^[a-zA-Z0-9](?!.*\+\+)[a-zA-Z0-9._%+-]*[a-zA-Z0-9]@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$') .hasMatch(value)) { return 'Please enter a valid email address'; } return null; }, ), ), const SizedBox(height: 20), ElevatedButton( onPressed: () { if (_formKey.currentState!.validate()) { ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text('Data is valid')), ); } }, child: const Text('Submit'), ), ], ), ); } }
-
Save the file.
-
Run your Flutter project using the following command:
flutter run
Select the appropriate device to run the app.
-
During the app execution, you can use the following commands:
- Enter
r
to hot reload the app and see the changes you made to the code. - Enter
q
to quit the app.
- Enter
exp_7_b_output.mp4
In this experiment, we learned how to perform form validation and error handling in a Flutter app. We created a simple form that takes the user's name and email address as input and validates the input data. If the input data is valid, a success message is displayed; otherwise, an error message is displayed.