Flutter JSON表单生成插件simple_json_form的使用

Flutter JSON表单生成插件simple_json_form的使用

一个完整的表单生成器仍在开发中。此项目基于以下仓库:

  • 简单Json构建器:Tanmoy Karmakar
  • Json Schema表单:Instituto Iracema

规格

此库允许你从JSON文件创建一个包含多种字段类型的完整表单(如textcheckboxmultiselectdatetimeformat1)。该包还提供了额外的备注选项。

它完全用Dart编写。❤️

安装

在你的pubspec.yaml文件中添加以下依赖:

dependencies:
  simple_json_form: ^0.0.3

简单使用

要集成简单的表单生成器,只需遵循给定的JSON模式并将其传递给formBuilder小部件即可。

JSON模式

// 完整示例可以在全局文件夹中找到,可以作为参考

// 多选(选项按钮)
{
  "form": [
    {
      "key": "informations",
      "properties": [
        "key": "identifier",
        "fields": ["1", "2", "3", "4", "5"],
        "title": "这是我的标题",
        "description": "这是我的描述",
        "type": "multiple",
      ]
    }
  ]
}

// 复选框
{
  "form": [
    {
      "key": "informations",
      "properties": [
        "key": "identifier",
        "fields": ["1", "2", "3", "4", "5"],
        "title": "这是我的标题",
        "description": "这是我的描述",
        "type": "checkbox"
      ]
    }
  ]
}

// 文本 - 数字
{
  "form": [
    {
      "key": "informations",
      "properties": [
        "key": "identifier",
        "title": "这是我的标题",
        "description": "这是我的描述",
        "type": "text",
        "is_mandatory": false,
        "readOnly": true,
        "validations": {
              "message": "这是我的消息",
          "length": {"min": 10, "max": 20},
        }
      ]
    }
  ]
}

// 日期
{
  "form": [
    {
      "key": "informations",
      "properties": [
        "key": "identifier",
        "title": "这是我的标题",
        "description": "这是我的描述",
        "type": "date",
        "is_mandatory": false,
      ]
    }
  ]
}

// 时间
{
  "form": [
    {
      "key": "informations",
      "properties": [
      "key": "identifier_boat",
      "title": "这是我的标题",
      "description": "这是我的描述",
      "type": "time",
      "is_mandatory": false,
    ]
  }
]

// 格式1
{
  "form": [
    {
      "key": "informations",
      "properties": [
        "key": "identifier_boat",
        "fields": ["1", "2", "3", "4", "5"],
        "title": "这是我的标题",
        "description": "这是我的描述",
        "type": "format1",
        "raw": [
          {
            "title": "菜单1",
            "description": "描述1",
            "properties": [
              {
                "key": "key_date",
                "title": "日期",
                "type": "date",
              },
            ]
          },
          {
            "title": "菜单2",
            "properties": [
              {
                "key": "key_trips",
                "title": "旅行",
                "type": "text",
              },
            ]
          },
        ]
      ]
    }
  ]
}

小部件实现

import 'dart:convert';

import 'package:example/constants/constants.dart';
import 'package:flutter/material.dart';
import 'package:simple_json_form/simple_json_form.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      //theme: ThemeData.dark(),
      title: '表单生成器示例',
      home: Scaffold(
        appBar: AppBar(
          title: const Text('材料应用栏'),
        ),
        body: SingleChildScrollView(
          child: Column(
            children: [
              SimpleJsonForm(
                jsonSchema: sampleData, // 使用你的JSON数据
                title: "员工绩效评估",
                titleStyle: const TextStyle(
                  fontWeight: FontWeight.bold,
                  fontSize: 15,
                ),
                description: "绩效评估",
                crossAxisAlignment: CrossAxisAlignment.center,
                index: 0,
                imageUrl: '',
                defaultValues: DefaultValues().copyWith(
                  nextButtonText: '下一步',
                  hintDropdownText: '请选择一个选项',
                  previousButtonText: '上一步',
                  submitButtonText: '提交',
                  validationDescription: '缺少一些必填字段',
                  validationTitle: '验证失败',
                  fieldRequired: '字段是必填项',
                ),
                descriptionStyleText: const TextStyle(
                  color: Colors.lightBlue,
                ),
                titleStyleText: const TextStyle(
                  fontWeight: FontWeight.bold,
                  fontSize: 16,
                  color: Colors.blue,
                ),
                onSubmit: (val) {
                  if (val == null) {
                    print("没有数据");
                  } else {
                    var json = jsonEncode(val.toJson());
                    print(json);
                  }
                },
              ),
            ],
          ),
        ),
      ),
    );
  }
}

更多关于Flutter JSON表单生成插件simple_json_form的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter JSON表单生成插件simple_json_form的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


simple_json_form 是一个用于在 Flutter 中根据 JSON 数据动态生成表单的插件。它可以帮助开发者快速构建表单界面,而无需手动编写大量的表单代码。下面是如何使用 simple_json_form 的基本步骤。

1. 添加依赖

首先,你需要在 pubspec.yaml 文件中添加 simple_json_form 的依赖:

dependencies:
  flutter:
    sdk: flutter
  simple_json_form: ^1.0.0  # 请使用最新版本

然后运行 flutter pub get 来安装依赖。

2. 导入包

在你的 Dart 文件中导入 simple_json_form

import 'package:simple_json_form/simple_json_form.dart';

3. 定义 JSON 表单结构

你需要定义一个 JSON 对象来描述表单的结构。以下是一个简单的例子:

final jsonForm = [
  {
    "type": "text",
    "label": "Name",
    "key": "name",
    "placeholder": "Enter your name",
    "required": true
  },
  {
    "type": "email",
    "label": "Email",
    "key": "email",
    "placeholder": "Enter your email",
    "required": true
  },
  {
    "type": "password",
    "label": "Password",
    "key": "password",
    "placeholder": "Enter your password",
    "required": true
  },
  {
    "type": "dropdown",
    "label": "Gender",
    "key": "gender",
    "options": ["Male", "Female", "Other"],
    "required": true
  },
  {
    "type": "date",
    "label": "Date of Birth",
    "key": "dob",
    "required": true
  },
  {
    "type": "checkbox",
    "label": "Subscribe to newsletter",
    "key": "subscribe",
    "default": false
  }
];

4. 使用 SimpleJsonForm 组件

在你的 Flutter 应用中使用 SimpleJsonForm 组件来渲染表单:

class MyFormPage extends StatelessWidget {
  final List<Map<String, dynamic>> jsonForm;

  MyFormPage({required this.jsonForm});

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Simple JSON Form'),
      ),
      body: SimpleJsonForm(
        form: jsonForm,
        onSubmit: (Map<String, dynamic> formData) {
          // 处理表单提交
          print('Form Data: $formData');
        },
      ),
    );
  }
}

5. 处理表单提交

SimpleJsonForm 组件的 onSubmit 回调函数会在用户提交表单时触发,并将表单数据作为一个 Map<String, dynamic> 对象返回。你可以在这个回调函数中处理表单数据,例如将其发送到服务器或保存到本地存储。

6. 运行应用

最后,运行你的 Flutter 应用,你将看到一个根据 JSON 数据动态生成的表单界面。

完整示例

以下是一个完整的示例代码:

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

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyFormPage(
        jsonForm: [
          {
            "type": "text",
            "label": "Name",
            "key": "name",
            "placeholder": "Enter your name",
            "required": true
          },
          {
            "type": "email",
            "label": "Email",
            "key": "email",
            "placeholder": "Enter your email",
            "required": true
          },
          {
            "type": "password",
            "label": "Password",
            "key": "password",
            "placeholder": "Enter your password",
            "required": true
          },
          {
            "type": "dropdown",
            "label": "Gender",
            "key": "gender",
            "options": ["Male", "Female", "Other"],
            "required": true
          },
          {
            "type": "date",
            "label": "Date of Birth",
            "key": "dob",
            "required": true
          },
          {
            "type": "checkbox",
            "label": "Subscribe to newsletter",
            "key": "subscribe",
            "default": false
          }
        ],
      ),
    );
  }
}

class MyFormPage extends StatelessWidget {
  final List<Map<String, dynamic>> jsonForm;

  MyFormPage({required this.jsonForm});

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Simple JSON Form'),
      ),
      body: SimpleJsonForm(
        form: jsonForm,
        onSubmit: (Map<String, dynamic> formData) {
          // 处理表单提交
          print('Form Data: $formData');
        },
      ),
    );
  }
}
回到顶部