dynamic_forms_generator 0.1.2 dynamic_forms_generator: ^0.1.2 copied to clipboard
Library for generating model and parser part of the dynamic forms.
dynamic_forms_generator #
Dart library for generating model and parser part of the flutter_dynamic_forms.
Main goal #
There is a lots of boilerplate code needed to write a single component for the flutter_dynamic_forms. Especially in the model and parser classes. To simplify this task we can describe components in a simple Yaml format and let this generator create model and parser for us.
Usage #
To use this generator you need to have all the Yaml files for the related components in your project.
After that add this package as a dev dependency together with build_runner
:
dev_dependencies:
build_runner: ^1.0.0
dynamic_forms_generator: <latest version>
Optionally add a build.yaml to the root of your project to override default imports that should be generated in your files:
targets:
$default:
builders:
dynamic_forms_generator:dynamicFormsBuilder:
options:
default_imports:
- "../components.dart"
components_to_ignore:
- "form_element.yaml"
- "validation.yaml"
default_imports
will generate this import line to the both of model and parser files.
components_to_ignore
will not generate anything for the listed components. Specifically form_element.yaml
and validation.yaml
are the default option so you don't need to explicitly list them. Reason why you don't want to generate anything for those classes is that they already have their models and parsers in the dynamic_forms
library but other components can extend them and generator needs to have all the Yaml definitions in the project so it can correctly analyse their structure.
Other options are model_imports
and parser_imports
which generates imports at the beginning of the model or parser file.
Now you simply run
pub run build_runner build
or if you are using Flutter
flutter packages pub run build_runner build
Instead of build
you can use watch
to track changes. See build_runner package for more information.
Component description structure #
Each component should contain all the necessary info to be able to represent it in xml format.
Example component definitions:
name: checkBox
parentType: formElement
properties:
label:
type: string
value:
type: bool
default: false
isMutable: true
name: container
parentType: formElement
properties:
children:
type: formElement[]
contentProperty: children
name: singleSelectGroup<tSingleSelectChoice extends singleSelectChoice>
parentType: formElement
properties:
value:
type: string
isMutable: true
choices:
type: tSingleSelectChoice[]
name: dropdownButton
parentType: singleSelectGroup<dropdownOption>
- Required
type
contains unique value which identifies the component.- Can contain optional generic parameters.
- Parameters are wrapped between
<
>
and delimited by comma. - Each parameter can be followed by the
extend
keyword and other type which will further constraint the type.
- Parameters are wrapped between
- Can contain optional generic parameters.
- Optional
parentType
contains name of other element and it inherits all its properties and thecontentProperty
.- Can contain optional generic parameters.
- Parameters are wrapped between
<
>
and delimited by comma.
- Parameters are wrapped between
- Can contain optional generic parameters.
- Optional
properties
section contains map of all the component properties.- Each key in this map corresponds to the property name and value contains other property descriptions.
- Required
type
represents type of the property. - Optional
default
contains default value of the property when it is not explicitly set. - Optional
isMutable
contains optional info wheter state of the property can be mutated. Default isfalse
.- When set to
true
it is expected to be sent from a client back to a server.
- When set to
- Required
- Each key in this map corresponds to the property name and value contains other property descriptions.
- Optional
contentProperty
contains name of the property, which can be written directly as a child of the component element.
Types #
There are multiple types that can be set to the type
in the property description:
int
- 64-bit two's complement integerdecimal
bool
string
dateTime
- string as standardized format of date-time (ISO 8601)customformElement
- name of the other componentcustomformElement[]
- array of the other components
Each type is nullable and when property is not set it defaults to the null
(unless default
key is defined).
YAML -> XML #
In XML the name
corresponds to the element name.
parentType
is not represented in the xml.
Property can be written directly as an attribute of the element
<element propertyName="property value"/>
and in case of complex values it can also be written as:
<element>
<element.propertyName>
property value
</element.propertyName>
</element>
Each property which is not type formElement
can also contain expression as the element value:
<element>
<element.propertyName>
<expression>
1 + 1
</expression>
</element.propertyName>
</element>
See https://github.com/OndrejKunc/flutter_dynamic_forms to learn more about expressions.
contentProperty
allows to write the selected property directly inside the element. In case of container it looks like this:
<container>
<child1/>
<child2/>
</container>
YAML -> JSON #
JSON has very similar mapping except that contentProperty
is ignored.