Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

flutter_external forms

a collection of flutter and dart libraries allowing you to dynamicaly define your external forms outside the app and consume it at runtime.

main goal

the idea behind this project is to be able define your forms with all the inputs, validation logic and other rules on the server and consume it in the flutter client without redeploying the app.

this is achieved by defining the form via xml using xaml like syntax or json. it has its own expression language to describe all the relationships between properties of each element.

see example project which contains working demo.

form definition example

<?xml version="1.0" encoding="utf-8"?>
<form id="form1">
    <text
        id="firstname"
        label="enter your first name">
    </text>
    <text
        id="lastname"
        label="enter your last name">
        <text.validations>
            <requiredvalidation
                message="last name is required"/>
        </text.validations>
    </text>
    <label
        id="fullnamelabel">
        <label.value>
            <expression>
                <![cdata[
                    @firstname + (length(@firstname) > 0 && length(@lastname) > 0 ? " " : "") + @lastname
                ]]>
            </expression>
        </label.value>
    </label>
    <label>
        <label.value>
            <expression>
                <![cdata[
                    "welcome " + @fullnamelabel + "!"
                ]]>
            </expression>
        </label.value>
        <label.isvisible>
            <expression>
                <![cdata[
                    [email protected] && length(@fullnamelabel) > 0
                ]]>
            </expression>
        </label.isvisible>
    </label>
    <checkbox
        id="hidewelcomecheckbox"
        value="false"
        label="hide welcome message"/>
</form>

if you prefer json to describe your form please check json example.

external forms
example output

simple usage

installation

add following dependencies to your pubspec.yaml file:

flutter_dynamic_forms: ^0.5.0
flutter_dynamic_forms_components: ^0.4.0

displaying the form

the flutter_dynamic_forms_components library contains set of predefined components like text input, label, checkbox, radiobuttongroup etc. to make your app work with those components you need to perform the following steps:

first you need to create object called formmanager. you can put it inside the initstate method in your state of your statefulwidget:

//get your data somewhere, for demo purposes we use local assets
var data = await rootbundle.loadstring("assets/test_form1.xml");

//use either xmlformparserservice or jsonparserservice depending on your form format.
//for default component set use the predefined parser list.
var formmanagerbuilder = formmanagerbuilder(xmlformparserservice(getdefaultparserlist()));


//store the _formmanager in your state.
_formmanager = formmanagerbuilder.build(data);

the formmanager has a getter form which is the object representation of your xml/json form in dart. formmanager can also perform some useful operation on the form, like manipulating the state of the form when something happens in the ui, validating the form or collecting all the data from the form so it can be for example sent back to the server.

before you can render your form, you also need to initialize formrenderservice. this service gets list of renderers, where each renderer controls how each component would be rendered on the screen:

_formrenderservice = formrenderservice(
    renderers: getreactiverenderers(),
    dispatcher: _onformelementevent,
);

in this example we use set of predefined renderers. the word reactive means that each component will listen to the changes in the form model and will update itself. the dispatcher parameter is the callback method which is sent from the renderers when some action is performed (like checkbox checked). we will just delegate this action to our formmanager:

void _onformelementevent(formelementevent event) {
    if (event is changevalueevent) {
        _formmanager.changevalue(
            value: event.value, elementid: event.elementid, propertyname: event.propertyname);
    }
}

since we are using reactive renderes, we don’t need to call setstate() at the end of this method to re-render the form. the library itself will ensure that only the right properties of the form elements will be updated according to this change.

after that you must inform the widget that your form is ready to use:

setstate(() {
    _form = _formmanager.form;
});

and finally define the build method:

@override
widget build(buildcontext context) {
    if (_form == null) {
        return center(
            child: circularprogressindicator(),
        );
    }
    return center(
        child: singlechildscrollview(
            child: _formrenderservice.render(_form, context),
        ),
    );
}

and that’s it! now you can see your form in the action.

collect data from the form

the idea behind the process of sending data back to the server is that we shouldn’t send back the whole form but only values changed by the user.

to collect the data simply call:

list<formitemvalue> data = formmanager.getformdata()

it contains list of all the properties which were marked as a mutable in a component parser definition. in default components those are the properties that are expected to be changed by a user. each item contains id of the source element, property name and property value.
to submit the form you usually want to serialize this list and send it back to your server.


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

Top