How can i mkae a form in flutter
- User
How can i mkae a form in flutter
- Assistant
How can i mkae a form in flutter
Creating a form in Flutter is a straightforward process that involves using the Form widget along with various input widgets such as TextFormField. Below is a step-by-step guide on how to create a simple form in Flutter with validation.
Make sure you have Flutter installed on your computer and you have created a new Flutter project using the following command:
flutter create my_form_app
Navigate to the lib directory and open main.dart. Here is a simple example of how to create a form that consists of a text field for a name and an email address. It also includes basic validation.
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Form Example',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyForm(),
);
}
}
class MyForm extends StatefulWidget {
@override
_MyFormState createState() => _MyFormState();
}
class _MyFormState extends State<MyForm> {
final _formKey = GlobalKey<FormState>();
String _name = '';
String _email = '';
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Form Example'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Form(
key: _formKey,
child: Column(
children: <Widget>[
// Name Field
TextFormField(
decoration: InputDecoration(labelText: 'Name'),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter your name';
}
return null;
},
onSaved: (value) {
_name = value!;
},
),
// Email Field
TextFormField(
decoration: InputDecoration(labelText: 'Email'),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter your email';
}
if (!RegExp(r'^[^@]+@[^@]+\.[^@]+').hasMatch(value)) {
return 'Please enter a valid email address';
}
return null;
},
onSaved: (value) {
_email = value!;
},
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 16.0),
child: ElevatedButton(
onPressed: () {
if (_formKey.currentState!.validate()) {
// If the form is valid, save the state.
_formKey.currentState!.save();
// TODO: Process data (e.g., send to an API)
print('Name: $_name, Email: $_email');
}
},
child: Text('Submit'),
),
),
],
),
),
),
);
}
}
GlobalKey<FormState>: Used to uniquely identify the Form widget and allow validation.TextFormField: Input fields that are capable of validation.
validator: A function that returns a string if the input is invalid and returns null if the input is valid.onSaved: A function called when the user saves the form, useful for storing form data in variables.ElevatedButton: A button that triggers form validation and submission.Use the following command to run your application on your device or an emulator:
flutter run
Now you should see a simple form with fields for entering your name and email, along with basic validation feedback. You can customize this form by adding more fields, enhancing validation rules, and processing data as per your requirements.
Not yet included in a published release.