json_forms 1.0.1+7

  • Readme
  • Changelog
  • Example
  • Installing
  • 67

json_forms #

A package to make forms faster. Just write a JSON file and the Widgets will be generated

Getting Started #

Usage #

Widget widget = JsonForm.fromJson(yourJson);

Additional Information #

Available parameters for JsonForm.fromJson()

  • key : GlobalKey use to control form validation manually
    //Example
    import 'package:json_forms/json_forms.dart';
    
    final JsonFormController controller = JsonFormController();
    
    Widget widget = JsonForm.fromJson(
      myForm,
      controller : controller
    );
    
    //Inside a button
    onPressed: (){
      if(controller.validate()){
        // Do stuff
      }
    }
    
  • progressColor : Color used for the progress bar (not relevant if hideBar=true)
  • primaryColor : Color used for the accents & background of the bottom bar
  • decoration : BoxDecoration around each question
  • innerPadding : Padding between decoration and question content (fields, text, etc...)
  • outerPadding : Padding around the question element
  • useSwitch : Boolean to use switch instead of checkbox for boolean questions
  • hiderBar : If set to true, hide the bottom bar. Relevant when you have only one section in your form or if you want to validate using your own button or widget
  • showFormTitle : Boolean controlling if the form's title should be displayed or not
  • showSectionsTitles : Boolean controlling if the section's titles should be displayed or not
  • onValidation : Callback function when the form is validated with the bottom bar
  • translator : Function to interpret text and placeholder value. For example, use a key string in your json file and then translator : Applocalization.of(context).translate('key') to show different text depending of the language
  • readOnly : Boolean, if true, the form's values cannot be changed (quite self explanatory)

Json Formatting #

Example of JSON file

{ // Form Object
  "name": "The form's name",
  "sections": [ // List of Section objects
    {
      "name": "Name of the first section",
      "questions": [ // List of Question Object
      ...
      ]
    }
  ]
}

Available question's types : #

  • Text
  • Number
  • Bool
  • Radio
  • Pick
  • CheckTable
  • OptionalQuantityTable (yes, I could have found a better name :) )
Examples : #
  • Text
// Question object
{
  "id": 0,
  "text": "your question as a string",
  "type": "Text",
  "value": null, // Null or String
  "condition": {  // Condition to be satisfy to render this question
    "questionId": 1,
    "value": "required answer"
  }, 
 "isRequired": false // Boolean, set it to false if the condition is not null and/or if possibilities not null
},
  • Number
// Question object
{
  "id": 0,
  "text": "your question as a string",
  "type": "Number",
  "value": null, // Null or Int
   "condition": {  // Condition to be satisfy to render this question
    "questionId": 1,
    "value": "required answer"
  },
  "isRequired": false // Boolean, set it to false if the condition is not null and/or if possibilities not null
},
  • Bool
// Question object
{
  "id": 0,
  "text": "your question as a string",
  "type": "Bool",
  "value": false, // True or false
   "condition": {  // Condition to be satisfy to render this question
    "questionId": 1,
    "value": "required answer"
  },
  "isRequired": false // Boolean, set it to false if the condition is not null
},
  • Radio
// Question object
{
  "id": 0,
  "text": "your question as a string",
  "type": "Radio",
  "value": 4, // Null or Type corresponding to the possibilities
   "condition": {  // Condition to be satisfy to render this question
    "questionId": 1,
    "value": "required answer"
  },
  "possibilities": [1,2,3,4,5],
  "isRequired": false // Boolean, set it to false if the condition is not null and/or if possibilities not null
},
  • Pick
// Question object
{
  "id": 0,
  "text": "your question as a string",
  "type": "Bool",
  "value": null, // Null or Type corresponding to the possibilities
   "condition": {  // Condition to be satisfy to render this question
    "questionId": 1,
    "value": "required answer"
  },
  "possibilities": ["red", "green", "blue"],
  "isRequired": false // Boolean, set it to false if the condition is not null
},
  • CheckTable
// Question object
{
  "id": 0,
  "text": "your question as a string",
  "type": "Bool",
  "value": null, // Null or array of objects with type corresponding to the possibilities
   "condition": {  // Condition to be satisfy to render this question
    "questionId": 1,
    "value": "required answer"
  },
  "possibilities": ["cat", "dog", "fish"],
  "isRequired": false // Boolean, set it to false if the condition is not null
},
  • OptionalQuantityTable
// Question object
{
  "id": 0,
  "text": "your question as a string",
  "type": "Bool",
  "value": null, // Null or dictionary with possibility as key and number as value
   "condition": {  // Condition to be satisfy to render this question
    "questionId": 1,
    "value": "required answer"
  },
  "possibilities": ["potatoes", "carrots"],
  "isRequired": false // Boolean, set it to false if the condition is not null
},

[1.0.1+7] - 11/09

  • Fixed PickQuestion

Known bug :

  • Exception when using the progress/validation bar
    • Temporary fix : set hideBar: true in JsonForm.fromJson

[1.0.1+6] - 10/23

[1.0.1+5] - 10/23

[1.0.1+4] - 10/23

[1.0.1+3] - 10/22

Fixes

[1.0.1+2] - 10/22

Added controller to manage form validate

[1.0.1+1] - 10/22

[1.0.1] - 10/20

Rename FormState to JsonFormState to differentiate from the Flutter FormState class. Fix files/class visibility of package

[0.0.2+1] - 09/03

Update documentation

[0.0.2] - 08/05

Added a readonly feature

[0.0.1] - 08/02

First release of the package

example/main.dart

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:json_forms/json_forms.dart';

var json;
void main() {
  json = rootBundle.loadString("assets/exmaple.json");
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        body: JsonForm.fromJson(json, controller: JsonFormController()),
      )
    );
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  json_forms: ^1.0.1+7

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_forms/json_forms.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
39
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
90
Overall:
Weighted score of the above. [more]
67
Learn more about scoring.

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

  • Dart: 2.6.1
  • pana: 0.12.21
  • Flutter: 1.9.1+hotfix.6

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health suggestions

Fix lib/json_forms.dart. (-0.50 points)

Analysis of lib/json_forms.dart reported 1 hint:

line 59 col 9: 'builder' is deprecated and shouldn't be used.

Format lib/src/Classes.dart.

Run flutter format to format lib/src/Classes.dart.

Format lib/src/FormProvider.dart.

Run flutter format to format lib/src/FormProvider.dart.

Fix additional 3 files with analysis or formatting issues.

Additional issues in the following files:

  • lib/src/FormWidget.dart (Run flutter format to format lib/src/FormWidget.dart.)
  • lib/src/QuestionWidget.dart (Run flutter format to format lib/src/QuestionWidget.dart.)
  • lib/src/SectionWidget.dart (Run flutter format to format lib/src/SectionWidget.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 (provider).

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
json_annotation ^3.0.0 3.0.0
provider ^3.0.0+1 3.2.0 4.0.0-dev
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.7 1.1.8
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test