json_dynamic_form 1.0.0 json_dynamic_form: ^1.0.0 copied to clipboard
A basic JSON-based form generator library. This is still a WIP and new features will be released
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:json_dynamic_form/JsonDynamicForm.dart';
import 'package:json_dynamic_form/models/Autogenerated.dart';
import 'package:json_dynamic_form/models/Values.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, this.title}) : super(key: key);
final String? title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
List<dynamic> data = [
{
"type": "text",
"label": "Text",
"description": "Enter your name",
"placeholder": "Enter your name",
"regex": "",
"errorText": "",
"required": true,
"name": "text-1622684775448"
},
{
"type": "email",
"label": "Email",
"description": "Enter your email",
"placeholder": "Enter your email",
"regex": "",
"errorText": "Please enter a valid email",
"required": true,
"name": "email-1622684776606"
},
{
"type": "phone",
"label": "Phone",
"description": "Enter your phone",
"placeholder": "Enter your phone",
"regex": "",
"errorText": "Please enter a valid phone number",
"required": false,
"name": "phone-1622684777910"
},
{
"type": "checkbox",
"label": "Checkbox",
"description": "Checkbox",
"values": [
{"label": "Option 1", "value": "option-1"},
{"label": "Option 2", "value": "option-2"}
],
"name": "checkbox-1622684784550"
},
{
"type": "radio",
"label": "Radio",
"description": "Radio boxes",
"values": [
{"label": "Option 1", "value": "option-1"},
{"label": "Option 2", "value": "option-2"}
],
"name": "radio-1622684785878"
},
{
"type": "number",
"label": "Number",
"description": "Age",
"placeholder": "Enter your age",
"min": 12,
"max": 90,
"name": "number-1622684779623"
},
{
"type": "autocomplete",
"label": "Select",
"description": "Select",
"placeholder": "Select",
"values": [
{"label": "Option 1", "value": "option-1"},
{"label": "Option 2", "value": "option-2"},
{"label": "Option 3", "value": "option-3"}
],
"name": "autocomplete-1622684787710"
},
{
"type": "autocomplete",
"label": "Select",
"description": "Select",
"placeholder": "Select",
"values": [
{"label": "Option 4", "value": "option-4"},
{"label": "Option 5", "value": "option-5"},
{"label": "Option 6", "value": "option-6"}
],
"name": "autocomplete-prueba"
}
];
late JsonDynamicForm jsonDynamicForm;
List<Widget>? fields = List<Widget>.empty(growable: true);
@override
void initState() {
super.initState();
jsonDynamicForm = JsonDynamicForm(data: data, setState: setState);
fields = jsonDynamicForm.generateFields();
}
@override
Widget build(BuildContext context) {
fields = jsonDynamicForm.generateFields(firstTime: false);
return Scaffold(
appBar: AppBar(
title: Text(widget.title!),
),
body: SingleChildScrollView(
child: Column(children: [
Column(
crossAxisAlignment: CrossAxisAlignment.start, children: fields!),
TextButton(
onPressed: getData,
child: Text("la sangre de cristo tiene poder"))
]),
),
);
}
getData() {
print(json.encode(jsonDynamicForm.printData()));
}
genericMethod(Autogenerated item, dynamic value) {
setState(() {
item.controller = value;
});
}
genericCheckMethod(Values values, dynamic value) {
setState(() {
values.controller = value;
});
}
}