json_schema_form 0.8.5

  • Readme
  • Changelog
  • Example
  • Installing
  • 76

Flutter JSON Form #

Flutter test

This is the plugin for flutter using JSON Schema to define the form itself. It supports:

  • Textfield
  • Selection field
  • forignkey field
  • qrcode scanning
  • DateTime Field
  • custom field icon and action

Getting start #

Important! The forign key field is using Django Rest Framework with DRF-schema-adapter. All the schema's structures are based on it. You can take a look what the structure is on this page

First prepare data #

Map<String, dynamic> itemJSONData = {
  "fields": [
    {
      "label": "ID",
      "readonly": true,
      "extra": {},
      "name": "id",
      "widget": "number",
      "required": false,
      "translated": false,
      "validations": {}
    },
    {
      "label": "Item Name",
      "readonly": false,
      "extra": {"help": "Please Enter your item name", "default": ""},
      "name": "name",
      "widget": "text",
      "required": true,
      "translated": false,
      "validations": {
        "length": {"maximum": 1024}
      }
    }]

put the data into JSONSchemaForm #


JSONSchemaForm(
                schema: (snapshot.data['fields'] as List)
                    .map((s) => s as Map<String, dynamic>)
                    .toList(),
                icons: [
                  FieldIcon(schemaName: "name", iconData: Icons.title),
                ],
                actions: [
                  FieldAction(
                      schemaName: "qr_code",
                      actionTypes: ActionTypes.qrScan,
                      actionDone: ActionDone.getInput)
                ],
                onSubmit: (value) {
                  print(value);
                },
              ),

As you can see, you can provide actions and icons based on the name property in the schema's data.

More tutorials #

read more on this

[0.5.1] - TODO: Add release date. #

  • added tests
  • added basic supports for fields

[0.5.3] #

  • added theme data

[0.5.5] #

  • fixed bugs
  • add tests

[0.5.6] #

  • fixed lost text problem

[0.6.7] #

  • Added more tests
  • Added URL param

[0.6.8] #

  • Added more tests

[0.6.9] #

  • Fixed QR scanning issues

[0.7.3] #

  • Added on action done callback
  • Added actions for forign key
  • Added get image action

[0.8.0] #

  • Added Datetime Field
  • Added Example for Datetime Field

[0.8.3] #

  • Added controller
  • Fixed bugs
  • Added docs

[0.8.4] #

  • update dependencies

[0.8.5] #

  • Fixed bugs

example/lib/main.dart

import 'dart:io';
import 'package:flutter/material.dart';
import 'package:json_schema_form/json_textform/JSONSchemaForm.dart';
import 'package:json_schema_form/json_textform/models/Action.dart';
import 'package:json_schema_form/json_textform/models/Controller.dart';
import 'package:json_schema_form/json_textform/models/Icon.dart';

import 'data/sample_data.dart';

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  JSONSchemaController controller = JSONSchemaController();

  Future<Map<String, dynamic>> getSchema() async {
    await Future.delayed(Duration(milliseconds: 100));
    return itemJSONData;
  }

  ThemeData buildTheme() {
    final ThemeData base = ThemeData();
    return base.copyWith(
      iconTheme: IconThemeData(color: Colors.white),
      inputDecorationTheme: InputDecorationTheme(
        fillColor: Colors.blue,
        border: OutlineInputBorder(
          borderSide: BorderSide(color: Colors.green),
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text("Form"),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () async {
          var value = await this.controller.onSubmit(context);
          print(value);
        },
      ),
      body: FutureBuilder<Map<String, dynamic>>(
          future: getSchema(),
          builder: (context, snapshot) {
            if (!snapshot.hasData) {
              return Container();
            }
            return Theme(
              data: buildTheme(),
              child: JSONSchemaForm(
                rounded: true,
                controller: controller,
                schema: (snapshot.data['fields'] as List)
                    .map((s) => s as Map<String, dynamic>)
                    .toList(),
                icons: [
                  FieldIcon(schemaName: "name", iconData: Icons.title),
                  FieldIcon(
                      schemaName: "description", iconData: Icons.description),
                  FieldIcon(schemaName: "price", iconData: Icons.attach_money),
                  FieldIcon(schemaName: "column", iconData: Icons.view_column),
                  FieldIcon(schemaName: "row", iconData: Icons.view_list),
                  FieldIcon(schemaName: "qr_code", iconData: Icons.scanner),
                  FieldIcon(schemaName: "unit", iconData: Icons.g_translate)
                ],
                actions: [
                  FieldAction<File>(
                      schemaName: "qr_code",
                      actionTypes: ActionTypes.image,
                      actionDone: ActionDone.getImage,
                      onDone: (File file) async {
                        if (file is File) {
                          print(file);
                        }
                        return file;
                      }),
                  FieldAction<File>(
                      schemaName: "name",
                      schemaFor: "category_id",
                      actionTypes: ActionTypes.image,
                      actionDone: ActionDone.getInput,
                      onDone: (File file) async {
                        if (file is File) {
                          print(file);
                        }
                        return "abc";
                      })
                ],
                onSubmit: (value) async {
                  print(value);
                },
                url: "http://192.168.1.114:8000",
                values: {
                  "author_id": {"label": "sdfsdf", "value": 2},
                  "name": "abc",
                  "time": DateTime(2016, 1, 2, 1).toIso8601String(),
                },
              ),
            );
          }),
    );
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  json_schema_form: ^0.8.5

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:json_schema_form/json_schema_form.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
57
Health:
Code health derived from static analysis. [more]
98
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
90
Overall:
Weighted score of the above. [more]
76
Learn more about scoring.

We analyzed this package on Apr 7, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.7
  • Flutter: 1.12.13+hotfix.9

Health suggestions

Fix lib/json_textform/JSONSchemaForm.dart. (-0.50 points)

Analysis of lib/json_textform/JSONSchemaForm.dart reported 1 hint:

line 74 col 9: The value of the field '_formKey' isn't used.

Fix lib/json_textform/components/JSONForignKeyField.dart. (-0.50 points)

Analysis of lib/json_textform/components/JSONForignKeyField.dart reported 1 hint:

line 1 col 8: Unused import: 'package:dio/dio.dart'.

Fix lib/json_textform/components/JSONTextFormField.dart. (-0.50 points)

Analysis of lib/json_textform/components/JSONTextFormField.dart reported 1 hint:

line 35 col 10: This function has a return type of 'String', but doesn't end with a return statement.

Fix additional 4 files with analysis or formatting issues. (-1 points)

Additional issues in the following files:

  • lib/json_textform/models/Action.dart (1 hint)
  • lib/json_textform/models/Schema.dart (1 hint)
  • lib/json_textform/components/JSONSelectField.dart (Run flutter format to format lib/json_textform/components/JSONSelectField.dart.)
  • lib/json_textform/components/SelectionPage.dart (Run flutter format to format lib/json_textform/components/SelectionPage.dart.)

Maintenance issues and suggestions

Support latest dependencies. (-10 points)

The version constraint in pubspec.yaml does not support the latest published versions for 1 dependency (permission_handler).

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
barcode_scan ^2.0.1 2.0.1
dio ^3.0.1 3.0.9
flutter 0.0.0
image_picker ^0.6.2+3 0.6.4
permission_handler ^4.4.0+hotfix.1 4.4.0+hotfix.4 5.0.0+hotfix.3
progress_indicators ^0.1.4 0.1.5
provider ^4.0.1 4.0.4 4.1.0-dev+2
Transitive dependencies
charcode 1.1.3
collection 1.14.11 1.14.12
flutter_plugin_android_lifecycle 1.0.6
http_parser 3.1.4
meta 1.1.8
nested 0.0.4
path 1.6.4
permission_handler_platform_interface 1.0.0 2.0.0
plugin_platform_interface 1.0.2
sky_engine 0.0.99
source_span 1.7.0
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test
mockito ^4.1.1