Big forms, made easy.
keyed_form handles nested fields, dependent rules and rebuilds, so big forms stay fast.
keyed_form handles nested fields, dependent rules and rebuilds, so big forms stay fast.
One schema file; the generator does the rest; your widgets stay plain Flutter. The demo runs this exact schema and controller — submit it empty.
@keyedSchema
library;
import 'package:keyed_form_schema/keyed_form_schema.dart';
part 'login_schema.kfg.dart';
final _loginSchema = ks.object({
'email': ks
.string(error: .text('Enter your email'))
.email(error: .text('That does not look like an email')),
'password': ks
.string(error: .text('Enter your password'))
.min(8, error: .text('At least 8 characters')),
});
import 'package:flutter/material.dart';
import 'package:keyed_form_flutter/keyed_form_flutter.dart';
import 'login_schema.dart';
part 'login_text_field.dart';
class LoginForm extends StatefulWidget {
const LoginForm({super.key, this.onSignedIn});
final void Function(LoginSchema value)? onSignedIn;
@override
State<LoginForm> createState() => _LoginFormState();
}
class _LoginFormState extends State<LoginForm> {
final form = KeyedFormController<LoginSchema>(
initialValue: LoginSchema.create(),
mode: KeyedFormMode.onTouched,
resolver: LoginSchema.validateData,
);
@override
void dispose() {
form.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return KeyedForm<LoginSchema>(
controller: form,
child: Column(
children: [
_LoginTextField(field: LoginFields.email, label: 'Email'),
const SizedBox(height: 16),
_LoginTextField(
field: LoginFields.password,
label: 'Password',
obscureText: true,
),
const SizedBox(height: 16),
// handleSubmit needs a context below KeyedForm.
Builder(
builder: (context) => FilledButton(
onPressed: () => form.handleSubmit(
context,
(value) => widget.onSignedIn?.call(value),
),
child: const Text('Sign in'),
),
),
],
),
);
}
}
part of 'login_form.dart';
class _LoginTextField extends StatelessWidget {
const _LoginTextField({
required this.field,
required this.label,
this.obscureText = false,
});
final FieldRef<LoginSchema, String> field;
final String label;
final bool obscureText;
@override
Widget build(BuildContext context) {
return KeyedFormField.text<LoginSchema>(
field: field,
builder: (context, f, controller) => TextField(
controller: controller,
obscureText: obscureText,
onTapOutside: (_) => FocusScope.of(context).unfocus(),
decoration: InputDecoration(
labelText: label,
errorText: f.errorText,
border: const OutlineInputBorder(),
),
),
);
}
}
{
"email": "",
"password": ""
}
{}
LoginFields.email is a typed field ref — rename a field and the compiler finds every use.
KeyedFormField hands you the value, the error and a text controller; render with Material, Cupertino or your own design system.
A ready-made Agent Skill teaches your coding agent the schema DSL, field refs, dynamic lists and submit flow — so "add a list of stops with a required city" comes out idiomatic on the first try. Works with any agent that supports Agent Skills.
Read SKILL.md →mkdir -p .agents/skills/keyed-form/references && \
for f in SKILL.md references/schema.md references/controller.md references/widgets.md; do \
curl -fsSL https://raw.githubusercontent.com/iamv4g/keyed_form/main/skills/keyed-form/$f -o .agents/skills/keyed-form/$f; \
done
Using Claude Code? Link it:
mkdir -p .claude/skills && ln -s ../../.agents/skills/keyed-form .claude/skills/keyed-form
Rows are addressed by a stable id, never by index. Type in row 2, then drag it to the top — the text, the error and the focus go with it.
@keyedSchema
library;
import 'package:keyed_form_schema/keyed_form_schema.dart';
part 'packing_schema.kfg.dart';
final _packingSchema = ks.object({
'items': ks
.list(
ks.object(className: 'PackingItemSchema', {
'label': ks.string(error: .text('Give it a name')).min(1),
'packed': ks.boolean().defaultTo(false),
}),
)
.min(1, error: .text('Add at least one item')),
});
import 'package:dnd_kit_flutter/dnd_kit_flutter.dart';
import 'package:flutter/material.dart';
import 'package:keyed_form_flutter/keyed_form_flutter.dart';
import 'packing_schema.dart';
part 'packing_row.dart';
class PackingList extends StatefulWidget {
const PackingList({super.key});
@override
State<PackingList> createState() => _PackingListState();
}
class _PackingListState extends State<PackingList> {
final dnd = DndController();
@override
void dispose() {
dnd.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return KeyedFieldList<PackingSchema, PackingItemSchema>(
field: PackingFields.items,
builder: (context, items, list) => Stack(
children: [
SortableScope(
controller: dnd,
strategy: SortableStrategies.dropOnOver,
offsetResolver: SortableOffsets.verticalList,
itemIds: [for (final item in items) DndId(item.clientId)],
onMove: (d) => list.move(d.fromIndex, d.toIndex),
child: Column(
children: [
for (final item in items)
SortableItem(
key: ValueKey(item.clientId),
id: DndId(item.clientId),
activationConstraint: const DndSensorActivationConstraint(
distance: 4,
),
builder: (context, drag, child) => AnimatedContainer(
duration: const Duration(milliseconds: 150),
transform: Matrix4.translationValues(0, drag.offset.y, 0),
child: Opacity(
opacity: drag.isDragging ? 0 : 1,
child: child,
),
),
child: _PackingRow(
fields: PackingFields.item(itemClientId: item.clientId),
onRemove: () => list.removeById(item.clientId),
),
),
TextButton.icon(
onPressed: () => list.append(PackingItemSchema.create()),
icon: const Icon(Icons.add),
label: const Text('Add item'),
),
],
),
),
DndDragOverlay(
controller: dnd,
builder: (context, drag) => Material(
elevation: 4,
child: ListTile(
leading: const Icon(Icons.drag_indicator),
title: Text(list.byId(drag.activeId.value)?.label ?? ''),
),
),
),
],
),
);
}
}
part of 'packing_list.dart';
class _PackingRow extends StatelessWidget {
const _PackingRow({required this.fields, required this.onRemove});
final ItemFieldRefs fields;
final VoidCallback onRemove;
@override
Widget build(BuildContext context) {
return Row(
children: [
const DndDragHandle(child: Icon(Icons.drag_indicator)),
KeyedFormField<PackingSchema, bool>(
field: fields.packed,
anchor: false,
builder: (context, f) => Checkbox(
value: f.value ?? false,
onChanged: (v) => f.onChanged(v ?? false),
),
),
Expanded(
child: KeyedFormField.text<PackingSchema>(
field: fields.label,
builder: (context, f, controller) => TextField(
controller: controller,
onTapOutside: (_) => FocusScope.of(context).unfocus(),
decoration: InputDecoration(
labelText: 'Item',
errorText: f.errorText,
),
),
),
),
IconButton(onPressed: onRemove, icon: const Icon(Icons.close)),
],
);
}
}
0 9b1215ee… Passport 1 8481ff00… Charger 2 860d536b… Sunscreen
Drag & drop by dnd_kit (Flutter · Jaspr) ↗
clientId; field refs, errors and touched state are keyed by it, so a reorder moves them with the row.
list.move, append, removeById — the list editor works the same whether a drag, a button or a server patch asked for it.
.min(1) and cross-row checks sit on the list itself, beside the per-row rules.
One keystroke into one field, counted in the benchmark/ harness in the repository — reproducible, JIT debug numbers.
One keystroke rebuilds one field's subtree — 44 widgets, whether the form has 10 fields or 250.
Server checks with a timeout. A check that throws or times out marks the field failed, not invalid, so retrying is the obvious next step.
validateAsyncisFailedValidationRules that read the whole draft, switch on and off with a condition, and pin their error to one field.
.refine(when:, path:)One field writes another. The text binding tells a derived write from the user's own typing, so the caret never jumps.
addRelationObjects, lists of objects and discriminated unions, with typed refs all the way down to the leaf.
ks.objectks.listks.discriminatedUnionOne call freezes a field, a row or a whole section against writes. Frozen fields still validate.
markReadOnlyWorks in lazy lists too: it jumps to the right section first, then reveals the exact field.
revealFirstValidate everything, reveal errors, scroll to the first one or run your callback, and track submitting — in one call.
handleSubmitPut a 422's messages on the right fields, by key or by wire path — shown at once, touched or not.
setServerErrorsThe controller is plain Dart. Drive a whole form in a unit test, with no widget tree to pump.
KeyedFormControllerOne schema, one controller, and a form that knows exactly which field changed.