flutter_form_schema
A production-ready, open-source Flutter package for schema-driven form generation and validation. Inspired by Yup/Zod from the JavaScript ecosystem.
Overview
Manually wiring up forms, validation logic, and error handling in Flutter can be repetitive and error-prone. flutter_form_schema allows you to define your form schema once (in Dart or JSON) and automatically generate a fully validated, accessible form widget.
Ideal for dynamic CMS-driven apps, admin panels, and configuration-heavy UIs.
Features
- Schema-Driven: Define forms using a declarative schema.
- JSON Support: Load forms dynamically from a CMS or API.
- Built-in Validators: Comprehensive set of validators (Email, Min/Max length, Regex, etc.).
- Cross-Field Validation: Easily implement logic like "Passwords must match".
- Customizable UI: Support for different layouts and custom field widgets.
- Controller-Based: Programmatically control form state, values, and errors.
Installation
Add the following to your pubspec.yaml:
dependencies:
flutter_form_schema: ^1.0.2
Usage
1. Defining a Schema in Dart
final schema = FormSchema(
id: 'user_registration',
fields: [
StringFieldSchema(
key: 'username',
label: 'Username',
required: true,
validators: [MinLengthValidator(4)],
),
StringFieldSchema(
key: 'email',
label: 'Email',
type: StringFieldType.email,
required: true,
validators: [EmailValidator()],
),
],
);
2. Generating a Form with SchemaForm
SchemaForm(
schema: schema,
onSubmit: (values) async {
print('Form values: $values');
},
onValidationError: (errors) {
print('Validation errors: $errors');
},
submitButtonLabel: 'Submit',
)
3. JSON-Driven Schema
final jsonSchema = {
"id": "profile_form",
"fields": [
{"key": "name", "type": "string", "label": "Full Name", "required": true},
{"key": "age", "type": "number", "label": "Age", "min": 18}
]
};
final schema = FormSchema.fromJson(jsonSchema);
4. Cross-Field Validation
SchemaForm(
schema: mySchema,
crossFieldValidators: [
CrossFieldValidator(
fields: ['password', 'confirmPassword'],
validate: (values) {
if (values['password'] != values['confirmPassword']) {
return 'Passwords must match';
}
return null;
},
),
],
onSubmit: (values) async => print(values),
)
5. Programmatic Control
final controller = FormSchemaController(schema: mySchema);
// Later...
controller.setValue('username', 'george');
bool isValid = await controller.validate();
print(controller.values);
Field Types
| Type | Schema Class | Widget | Config Options |
|---|---|---|---|
| String | StringFieldSchema |
TextField |
minLength, maxLength, pattern, type (email, password, etc.) |
| Number | NumberFieldSchema |
TextField |
min, max, isInteger |
| Boolean | BoolFieldSchema |
Checkbox/Switch |
widgetType (checkbox, toggle) |
| Select | SelectFieldSchema |
Dropdown |
options, multiSelect |
| Date | DateFieldSchema |
DatePicker |
minDate, maxDate, type (date, time, datetime) |
Built-in Validators
| Validator | Target Type | Parameters |
|---|---|---|
RequiredValidator |
Any | message |
MinLengthValidator |
String |
minLength, message |
MaxLengthValidator |
String |
maxLength, message |
EmailValidator |
String |
message |
PatternValidator |
String |
RegExp, message |
MinValueValidator |
num |
minValue, message |
MaxValueValidator |
num |
maxValue, message |
CustomValidator |
Any | String? Function(T? value) |
API Reference
SchemaForm
schema: TheFormSchemadefinition.onSubmit: Callback triggered when the form is submitted and valid.onValidationError: Callback triggered if validation fails on submit.controller: OptionalFormSchemaControllerfor programmatic control.crossFieldValidators: List ofCrossFieldValidators.submitButtonLabel: Text for the default submit button.
FormSchemaController
values: Current map of field values.errors: Current map of field errors.isValid: Boolean indicating if the form is currently valid.validate(): Triggers validation for all fields.setValue(key, value): Updates a field value and triggers validation.
Running Tests
To run the unit tests:
flutter test
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
MIT License
Copyright (c) 2025 George Ikwegbu
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Author
George Ikwegbu (2025)
Libraries
- flutter_form_schema
- A production-ready, schema-driven form generation and validation library for Flutter.