Flutter Auto Form 是一个旨在简化 Flutter 应用程序中用户友好表单创建的包。它通过根据数据模型自动生成表单字段来减少样板代码,从而提高开发速度和一致性。内置验证、错误处理和可自定义的控件使其在提升开发者体验和最终用户体验方面表现出色。该包支持所有主要平台,非常适合跨平台表单实现。
适合场景
从数据模型构建动态表单
减少重复的表单代码
跨平台创建一致的用户界面
轻松实现表单验证
输入屏幕的快速原型设计
主要特点
自动表单字段生成
内置验证支持
可自定义的表单控件
跨平台兼容性
所需的样板代码最少
适合对象
寻求更快表单创建的开发者
需要一致表单设计的项目
跨平台 Flutter 应用
需要快速表单实现的原型
注重可维护表单代码的团队
使用建议
对高度复杂的表单支持有限的自定义
可能需要额外配置以进行高级验证
不适合具有独特、非标准布局的表单
取决于模型结构以进行自动生
新用户对自动表单模式不熟悉时的学习曲线
原始 README
以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。
Flutter Auto Form
The easiest way to create fully customizable forms with only a tiny amount of code.
Introduction
Are you tired of writing endless lines of code to create forms in your Flutter apps? Look no further! Our Flutter Auto
Form package is here to revolutionize your form-building experience.
Our primary goal is to significantly reduce the amount of code required to create forms in Flutter. We achieve this by
embracing a clear separation of concerns between form logic and form display.
With Flutter Auto Form, you write the form logic in pure Dart, without any direct mention of widgets. Instead, we
introduce the AFWidget, which dynamically renders all the necessary widgets based on a straightforward form
declaration—a simple Dart class that extends the TemplateForm class.
But that's not all! Our package empowers you to design your custom fields and linked widgets, making nearly any form
conceivable. Whether you're working on basic forms or tackling complex ones, Flutter Auto Form has got you covered.
The true magic of Flutter Auto Form shines when you're dealing with multiple forms, especially complex ones. All your
validators are automatically called when you submit a form, and you can even create sub-forms that cascade into one
another.
Dynamic form generation is also feasible but still in experimental mode. It is accessible through the
JsonSchemaForm class.
dependencies:flutter:sdk:flutter# Your other packages ...flutter_auto_form:^1.0.6
Support
Platforms: All platforms currently supported
Autofill hints: Automatic support through AFTextFieldType
Validators: Email, Url, Hex colour, Not null, Minimum string length, Same as another field, Alphanumeric.
Fields: Password (auto obscure toggle), Text, Number, Model (built-in support for search through an api),
Boolean, Sub-form (cascading forms), Select (dropdown field allowing only predefined values)
Custom code: You can customize and create new fields, validators, widgets as you please without even touching the
source code of this package !
Usage
The first step in creating a form with Flutter Auto Form is to create a class inheriting from the TemplateForm class.
The second & (already) last step is to add the AFWidget wherever you would like to display a form.
AFWidget<RegistrationForm>(
formBuilder: () => RegistrationForm(),
submitButton: (Function({bool showLoadingDialog}) submit) => Padding(
padding: constEdgeInsets.only(top: 32),
child: MaterialButton(
child: Text('Submit'),
onPressed: () => submit(showLoadingDialog: true),
),
),
onSubmitted: (RegistrationForm form) {
// do whatever you want when the form is submittedprint(form.toMap());
},
);
Example
A demo video can be found here. The
four forms displayed in the video are all created with Flutter Auto Form, even the model search field.
The source code is located in the example folder.
Advanced usage
If you need to create your own field (color field, image field, ...) you can always create it by overriding the Field
class. Then to display a custom widget, create a stateful widget that extends the FieldStatefulWidget, and of which the
state extends the FieldState class.
This package is still under construction ! Do not hesitate to create an issue on the GitHub page if you find any bug
or if you would like to see a new type of validator, field !