Edit Record
Loading preview
Starting the Flutter app
A Flutter admin edit form with grouped fields, switches, and save and cancel actions.
import 'package:flutter/material.dart';
class EditForm extends StatefulWidget {
const EditForm({super.key});
@override
State<EditForm> createState() => _EditFormState();
}
class _EditFormState extends State<EditForm> {
final _nameController = TextEditingController(text: 'Alex Kim');
final _emailController = TextEditingController(text: 'alex@acme.io');
String _role = 'Editor';
bool _active = true;
bool _twoFactor = false;
@override
void dispose() {
_nameController.dispose();
_emailController.dispose();
super.dispose();
}
void _save() {
final name = _nameController.text.trim();
final email = _emailController.text.trim();
ScaffoldMessenger.of(context)
..hideCurrentSnackBar()
..showSnackBar(SnackBar(content: Text('Saved $name ($email) as $_role')));
}
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
return Scaffold(
appBar: AppBar(
title: const Text('Edit user'),
actions: [TextButton(onPressed: _save, child: const Text('Save'))],
),
body: ListView(
padding: const EdgeInsets.all(16),
children: [
Center(
child: Stack(
children: [
const CircleAvatar(radius: 40, child: Text('AK')),
Positioned(
right: 0,
bottom: 0,
child: CircleAvatar(
radius: 14,
backgroundColor: theme.colorScheme.primary,
child: Icon(Icons.edit,
size: 14, color: theme.colorScheme.onPrimary),
),
),
],
),
),
const SizedBox(height: 24),
_sectionLabel(context, 'Profile'),
TextFormField(
controller: _nameController,
decoration: const InputDecoration(
labelText: 'Full name', border: OutlineInputBorder()),
),
const SizedBox(height: 16),
TextFormField(
controller: _emailController,
decoration: const InputDecoration(
labelText: 'Email', border: OutlineInputBorder()),
),
const SizedBox(height: 24),
_sectionLabel(context, 'Access'),
DropdownButtonFormField<String>(
initialValue: _role,
decoration: const InputDecoration(
labelText: 'Role', border: OutlineInputBorder()),
items: const [
DropdownMenuItem(value: 'Admin', child: Text('Admin')),
DropdownMenuItem(value: 'Editor', child: Text('Editor')),
DropdownMenuItem(value: 'Viewer', child: Text('Viewer')),
],
onChanged: (v) => setState(() => _role = v ?? _role),
),
const SizedBox(height: 8),
SwitchListTile(
value: _active,
onChanged: (v) => setState(() => _active = v),
title: const Text('Account active'),
contentPadding: EdgeInsets.zero,
),
SwitchListTile(
value: _twoFactor,
onChanged: (v) => setState(() => _twoFactor = v),
title: const Text('Require two-factor auth'),
contentPadding: EdgeInsets.zero,
),
const SizedBox(height: 24),
Row(
children: [
Expanded(
child: OutlinedButton(
onPressed: () => _showDemo(context, 'Cancel (demo)'), child: const Text('Cancel')),
),
const SizedBox(width: 12),
Expanded(
child: FilledButton(
onPressed: _save, child: const Text('Save changes')),
),
],
),
],
),
);
}
Widget _sectionLabel(BuildContext context, String text) {
return Padding(
padding: const EdgeInsets.only(bottom: 12),
child: Text(text.toUpperCase(),
style: Theme.of(context).textTheme.labelSmall?.copyWith(
letterSpacing: 1,
color: Theme.of(context).colorScheme.onSurfaceVariant)),
);
}
}
void _showDemo(BuildContext context, String message) {
ScaffoldMessenger.of(context)
..hideCurrentSnackBar()
..showSnackBar(SnackBar(content: Text(message)));
}
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
The name and email TextFormFields use TextEditingControllers seeded with sample values, while the role dropdown and two SwitchListTiles keep state in _role, _active and _twoFactor. Both save buttons call _save, which reads the current values into a SnackBar. Nothing is validated or stored. Cancel shows a demo SnackBar, and the avatar’s edit badge is decoration with no tap target.
Adapting it
- Wrap the
ListViewin aFormwith aGlobalKey<FormState>, then validate the email in_save. - Replace the
SnackBarin_savewith your update request. - Pass the user through the
EditFormconstructor to replace ‘Alex Kim’ and the default_role.