搜尋套件、儲存庫與 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 項

原始碼儲存庫

使用此套件的專案

暫無相關資料。