Contact Form
Loading preview
Starting the Flutter app
Name, email, subject, message, and attachment placeholder
import 'package:flutter/material.dart';
/// The validated form values passed to [ContactForm.onSubmit].
class ContactMessage {
final String name;
final String email;
final String subject;
final String message;
const ContactMessage({
required this.name,
required this.email,
required this.subject,
required this.message,
});
}
class ContactForm extends StatefulWidget {
/// Called with the message once every field validates. Without it the demo
/// shows a SnackBar.
final ValueChanged<ContactMessage>? onSubmit;
const ContactForm({super.key, this.onSubmit});
@override
State<ContactForm> createState() => _ContactFormState();
}
class _ContactFormState extends State<ContactForm> {
final _formKey = GlobalKey<FormState>();
final _nameController = TextEditingController();
final _emailController = TextEditingController();
final _subjectController = TextEditingController();
final _messageController = TextEditingController();
@override
void dispose() {
_nameController.dispose();
_emailController.dispose();
_subjectController.dispose();
_messageController.dispose();
super.dispose();
}
String? _required(String? value) =>
(value ?? '').trim().isEmpty ? 'Required' : null;
String? _validateEmail(String? value) {
final email = value?.trim() ?? '';
if (email.isEmpty) return 'Required';
final valid = RegExp(r'^[^@\s]+@[^@\s]+\.[^@\s]+$').hasMatch(email);
return valid ? null : 'Enter a valid email';
}
void _submit() {
if (!_formKey.currentState!.validate()) return;
final onSubmit = widget.onSubmit;
if (onSubmit != null) {
onSubmit(
ContactMessage(
name: _nameController.text.trim(),
email: _emailController.text.trim(),
subject: _subjectController.text.trim(),
message: _messageController.text.trim(),
),
);
return;
}
ScaffoldMessenger.of(context)
..hideCurrentSnackBar()
..showSnackBar(const SnackBar(content: Text('Message sent (demo)')));
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Contact Us')),
body: Form(
key: _formKey,
child: ListView(
padding: const EdgeInsets.all(24),
children: [
TextFormField(
controller: _nameController,
validator: _required,
decoration: const InputDecoration(
labelText: 'Name',
border: OutlineInputBorder(),
),
),
const SizedBox(height: 16),
TextFormField(
controller: _emailController,
validator: _validateEmail,
decoration: const InputDecoration(
labelText: 'Email',
border: OutlineInputBorder(),
),
keyboardType: TextInputType.emailAddress,
),
const SizedBox(height: 16),
TextFormField(
controller: _subjectController,
validator: _required,
decoration: const InputDecoration(
labelText: 'Subject',
border: OutlineInputBorder(),
),
),
const SizedBox(height: 16),
TextFormField(
controller: _messageController,
validator: _required,
decoration: const InputDecoration(
labelText: 'Message',
border: OutlineInputBorder(),
alignLabelWithHint: true,
),
maxLines: 5,
),
const SizedBox(height: 16),
Row(
children: [
const Icon(Icons.attach_file),
const SizedBox(width: 8),
Expanded(
child: Text(
'Attachment (placeholder)',
style: Theme.of(context).textTheme.bodyMedium,
),
),
],
),
const SizedBox(height: 24),
FilledButton(onPressed: _submit, child: const Text('Send')),
],
),
),
);
}
}
Add it to your project
Copies the Dart source into your app, so you own it. Flutter SDK only, no extra packages.
First time? Run dart pub global activate fluttertemplates_dev, or read the CLI guide.
How it works
Four TextFormFields sit in a Form: all are required, and the email must look like an address. The Message field pairs maxLines: 5 with alignLabelWithHint to keep its label at the top. The attachment row is only an Icon and wrapping text marked as a placeholder, and Send validates, then passes a ContactMessage to onSubmit, or shows a demo SnackBar without it.
Adapting it
- POST to your endpoint from
onSubmit, with loading and error states. - Replace the attachment
Rowwith a real picker, for example thefile_pickerpackage.