搜索插件、代码仓与 APP 分类

插件详情v0.3.0

json_gen_form

使用 JSON 生成 Flutter 表单,包含多种内置表单控件、验证支持、自定义布局、自定义样式以及无限嵌套的表单分组。

AndroidiOSmacOS
近 30 天下载
28
喜欢数
5
Pub 分数
140

安装插件

flutter pub add json_gen_form

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

json_gen_form 是一个 Flutter 包,可从 JSON 动态生成表单,支持多种内置表单控件、验证规则、自定义布局和样式。它支持嵌套表单分组,非常适合用于服务器驱动的 UI。开发者可以在 JSON 中声明式地定义表单,减少样板代码,并在跨平台环境中实现灵活且可维护的表单结构。

适合场景

  • 从 JSON 动态渲染表单
  • 服务器驱动的 UI 与可配置表单
  • 嵌套表单结构
  • 实时表单验证
  • 可自定义的表单布局

主要特点

  • 内置表单控件
  • 验证支持
  • 自定义布局和样式
  • 嵌套表单分组
  • 基于 JSON 的表单定义

适合对象

  • 开发人员构建服务器驱动的 UI
  • 需要可重用表单逻辑的团队
  • 需要动态表单内容的应用程序
  • 使用 JSON 模式进行前端开发的开发人员
  • 频繁更改表单的项目

使用建议

  • 需要仔细设计JSON模式
  • 限于支持的控件类型
  • 可能需要额外的验证逻辑
  • 运行时解析开销
  • 依赖于准确的JSON输入

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

JSON_GEN_FORM

Generate Flutter forms using JSON, featuring a variety of built-in form controls,
support for validation, custom layouts, custom styles, and unlimited nested form grouping.

English · 中文

Features

  • Render form using JSON
  • Form validation
  • Custom form styles
  • Custom layouts (row, col)
  • Infinite form grouping and nesting (group)
  • Modify entire form values or individual form control values through methods
  • Form controls:
    • Text box
    • Password box
    • Textarea
    • Number
    • Radio button
    • Checkbox
    • Dropdown
    • Switch
    • Multimedia upload
    • Date picker
    • Time picker
    • DateTime picker

Usage

import 'package:flutter/material.dart';
import 'package:json_gen_form/json_gen_form.dart';

void main() {
  runApp(const MainApp());
}

class MainApp extends StatefulWidget {
  const MainApp({super.key});

  @override
  State<MainApp> createState() => _MainAppState();
}

class _MainAppState extends State<MainApp> {
  final GlobalKey _key = GlobalKey<JsonGenFormState>();
  final List<dynamic> _data = [
    {
      "type": "text",
      "field": "name",
      "label": "姓名",
      "value": "隔壁老王",
      "placeholder": "请输入姓名",
      "rules": [
        {"required": true, "message": "请输入姓名"}
      ]
    },
    {
      "type": "textarea",
      "field": "remark",
      "label": "自我介绍",
      "value": null,
      "placeholder": "请简单介绍一下你自己",
      "rules": []
    },
    {
      "type": "number",
      "field": "age",
      "label": "年龄",
      "value": null,
      "placeholder": "请输入年龄",
      "rules": [
        {"required": true, "message": "请输入年龄"},
        {"min": 18, "message": "年龄必须大于18岁"}
      ]
    }
  ];

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        brightness: Brightness.light,
        useMaterial3: true,
      ),
      home: Scaffold(
        appBar: AppBar(
          title: const Text('json gen form'),
        ),
        body: SafeArea(
          child: SingleChildScrollView(
            physics: const BouncingScrollPhysics(),
            padding: const EdgeInsets.symmetric(
              horizontal: 20.0,
              vertical: 20.0,
            ),
            child: Column(
              children: [
                JsonGenForm(
                  key: _key,
                  config: _data,
                  decoration: JsonGenFormDecoration(
                    // groupLabelWrap: (Widget child, dynamic data) {
                    //   return Container(
                    //     margin: const EdgeInsets.only(bottom: 5),
                    //     child: child,
                    //   );
                    // },
                    controlLabelWrap: (Widget child, dynamic data) {
                      return Container(
                        margin: const EdgeInsets.only(bottom: 5),
                        child: child,
                      );
                    },
                    controlWrap: (Widget child, dynamic data) {
                      return Container(
                        margin: const EdgeInsets.only(bottom: 10),
                        child: child,
                      );
                    },
                  ),
                ),
                ElevatedButton(
                  onPressed: () async {
                    final formState = _key.currentState as JsonGenFormState;
                    try {
                      final data = await formState.validate();
                      debugPrint('Validation success $data');
                    } catch (e) {
                      debugPrint('Validation error: $e');
                    }
                  },
                  child: const Text('Submit'),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

General Parameters

Except for group, row, and col, all types have the following general parameters. All types have extra.

Field Description Type Default Value Required
type Type String '' true
field Field Name String '' true
label Label String - -
hiddenLabel Hidden Label bool false -
value Default Value dynamic - -
disabled Disabled bool false -
readonly Read-Only bool false -
rules Validation Rules List [] -
extra Extra Params dynamic -

Form Types

group
Field Description Type Default Value Required
type Type String 'group' true
field Field Name String '' -
label Label String '' -
hiddenLabel Hidden Label bool '' -
children Sub-Forms List [] -
text
Field Description Type Default Value Required
type Type String 'text' true
placeholder Placeholder String - -
General Params - - - -
password
Field Description Type Default Value Required
type Type String 'password' true
placeholder Placeholder String - -
General Params - - - -
textarea
Field Description Type Default Value Required
type Type String 'textarea' true
placeholder Placeholder String - -
General Params - - - -
number
Field Description Type Default Value Required
type Type String 'number' true
placeholder Placeholder String - -
General Params - - - -
radio
Field Description Type Default Value Required
type Type String 'radio' true
options Options List [] -
direction Options Direction String 'horizontal' -
itemHorizontalSpace Item Horizontal Space double 16 -
itemVerticalSpace Item Vertical Space double 8 -
General Params - - - -
checkbox
Field Description Type Default Value Required
type Type String 'checkbox' true
options Options List [] -
direction Options Direction String 'horizontal' -
itemHorizontalSpace Item Horizontal Space double 16 -
itemVerticalSpace Item Vertical Space double 8 -
General Params - - - -
select
Field Description Type Default Value Required
type Type String 'select' true
options Options List [] -
multiple Multiple Select bool false -
General Params - - - -
switch
Field Description Type Default Value Required
type Type String 'switch' true
General Params - - - -
media
Field Description Type Default Value Required
type Type String 'media' true
mediaType Media Type (media, image, video) String 'media' -
multiple Multiple Uploads bool false -
General Params - - - -
ios Configuration info.plist
<key>NSCameraUsageDescription</key>
<string>Need camera for taking photos or scanning codes</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>Need access to photo library for selecting photos or videos</string>
<key>NSMicrophoneUsageDescription</key>
<string>Need microphone access for recording videos</string>
android Configuration AndroidManifest.xml
<uses-permission android:name="android.permission.CAMERA" />
date
Field Description Type Default Value Required
type Type String 'date' true
time
Field Description Type Default Value Required
type Type String 'time' true
datetime
Field Description Type Default Value Required
type Type String 'datetime' true

Layout Types

row

row's children must have type as col.

Field Description Type Default Value Required
type Type String 'row' true
gutter Spacing double 0 -
children Children List [] -
col

col's parent must be row.

Field Description Type Default Value Required
type Type String 'col' true
flex Flex int 1 -
child Child Config Map
group
Field Description Type Default Value Required
type Type String 'group' true
field Field Name String null -

Validation Rules

  • required
  • type: Shortcut validation type, only supported for text input
    • email
    • url
  • min: For string type, it's string length; for number type, it's a specific number; for array type, it's array length
  • max: For string type, it's string length; for number type, it's a specific number; for array type, it's array length
  • len: For string type, it's string length; for number type, it's a specific number; for array type, it's array length
  • pattern: Regex validation
  • message: Error message

所属分类

依赖

依赖清单 6 项

源码仓库

使用此插件的项目

暂无相关数据。