0% found this document useful (0 votes)
3 views

flutter labsession - 7

The document provides source code for two Flutter applications that create forms with various input fields. The first part includes a simple form with fields for name, email, a checkbox for newsletter subscription, and a dropdown for country selection. The second part enhances the form by implementing validation and error handling for fields such as name, email, password, phone, and address.

Uploaded by

Venky 12A
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

flutter labsession - 7

The document provides source code for two Flutter applications that create forms with various input fields. The first part includes a simple form with fields for name, email, a checkbox for newsletter subscription, and a dropdown for country selection. The second part enhances the form by implementing validation and error handling for fields such as name, email, password, phone, and address.

Uploaded by

Venky 12A
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

7) a)Design a form with various input fields

Source Code:
import 'package:flutter/material.dart';

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {


@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Form Example',
theme: ThemeData(primarySwatch: Colors.blue),
home: FormPage(),
);
}
}

class FormPage extends StatefulWidget {


@override
_FormPageState createState() => _FormPageState();
}

class _FormPageState extends State<FormPage> { final _formKey =


GlobalKey<FormState>();
String? _name;
String? _email;
bool _subscribeToNewsletter = false;
String _selectedCountry = 'USA';

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Form Example'),
),
body: Padding(
padding: EdgeInsets.all(20.0),
child: Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
TextFormField(
decoration: InputDecoration(labelText: 'Name'),
onSaved: (value) {
_name = value;
},
),
SizedBox(height: 20),
TextFormField(
decoration: InputDecoration(labelText: 'Email'),
onSaved: (value) {
_email = value;
},
),
SizedBox(height: 20),
Row(
children: <Widget>[
Checkbox(
value: _subscribeToNewsletter,
onChanged: (value) {
setState(() {
_subscribeToNewsletter = value!;
});
},
),
Text('Subscribe to Newsletter'),
],
),
SizedBox(height: 20),
Row(
children: <Widget>[
Text('Country:'),
SizedBox(width: 20),
DropdownButton<String>(
value: _selectedCountry,
onChanged: (value) {
setState(() {
_selectedCountry = value!;
});
},
items: <String>['USA', 'Canada', 'UK',
'Australia']
.map<DropdownMenuItem<String>>((String value)
{
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
),
],
), SizedBox(height: 20),
ElevatedButton(
onPressed: () {
_formKey.currentState?.save();
// Submit the form data
print('Name: $_name');
print('Email: $_email');
print('Subscribe to Newsletter:
$_subscribeToNewsletter');
print('Country: $_selectedCountry');
},
child: Text('Submit'),
),
],
),
),
),
);
}
}

Output:
7) b) Implement form validation and error handling
Source Code:
import 'package:flutter/material.dart';

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {


@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Form Example'),
),
body: SingleChildScrollView(
padding: EdgeInsets.all(16),
child: FormWidget(),
),
),
);
}
}

class FormWidget extends StatefulWidget {


@override
_FormWidgetState createState() => _FormWidgetState();
}

class _FormWidgetState extends State<FormWidget> {


final _formKey = GlobalKey<FormState>();
String _name = '';
String _email = '';
String _password = '';
String _phone = '';
String _address = '';

@override
Widget build(BuildContext context) {
return Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
TextFormField(
decoration: InputDecoration(labelText: 'Name'),
validator: (value) {
if (value!.isEmpty) {
return 'Please enter your name';
}
return null;
},
onSaved: (value) => _name = value!,
),
SizedBox(height: 16),
TextFormField(
decoration: InputDecoration(labelText: 'Email'),
keyboardType: TextInputType.emailAddress,
validator: (value) {
if (value!.isEmpty) {
return 'Please enter your email';
}
return null;
},
onSaved: (value) => _email = value!,
),
SizedBox(height: 16),
TextFormField(
decoration: InputDecoration(labelText: 'Password'),
obscureText: true,
validator: (value) {
if (value!.isEmpty) {
return 'Please enter a password';
}
return null;
},
onSaved: (value) => _password = value!,
),
SizedBox(height: 16),
TextFormField(
decoration: InputDecoration(labelText: 'Phone'),
keyboardType: TextInputType.phone,
validator: (value) {
if (value!.isEmpty) {
return 'Please enter your phone number';
}
return null;
},
onSaved: (value) => _phone = value!,
),
SizedBox(height: 16),
TextFormField(
decoration: InputDecoration(labelText: 'Address'),
maxLines: 3,
validator: (value) {
if (value!.isEmpty) {
return 'Please enter your address';
}
return null;
},
onSaved: (value) => _address = value!,
),
SizedBox(height: 16),
ElevatedButton(
onPressed: _submitForm,
child: Text('Submit'),
),
],
),
);
}

void _submitForm() {
if (_formKey.currentState!.validate()) {
_formKey.currentState!.save();
// Perform form submission with the saved form data
print('Form submitted:');
print('Name: $_name');
print('Email: $_email');
print('Password: $_password');
print('Phone: $_phone');
print('Address: $_address');
}
}
}

Output:

You might also like