Skip to content

Commit f2d7498

Browse files
committed
first commit
0 parents  commit f2d7498

28 files changed

+1660
-0
lines changed

Diff for: .gitignore

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Miscellaneous
2+
*.class
3+
*.log
4+
*.pyc
5+
*.swp
6+
.DS_Store
7+
.atom/
8+
.buildlog/
9+
.history
10+
.svn/
11+
12+
# IntelliJ related
13+
*.iml
14+
*.ipr
15+
*.iws
16+
.idea/
17+
18+
# The .vscode folder contains launch configuration and tasks you configure in
19+
# VS Code which you may wish to be included in version control, so this line
20+
# is commented out by default.
21+
#.vscode/
22+
23+
# Flutter/Dart/Pub related
24+
**/doc/api/
25+
.dart_tool/
26+
.flutter-plugins
27+
.flutter-plugins-dependencies
28+
.packages
29+
.pub-cache/
30+
.pub/
31+
build/
32+
33+
# Android related
34+
**/android/**/gradle-wrapper.jar
35+
**/android/.gradle
36+
**/android/captures/
37+
**/android/gradlew
38+
**/android/gradlew.bat
39+
**/android/local.properties
40+
**/android/**/GeneratedPluginRegistrant.java
41+
42+
# iOS/XCode related
43+
**/ios/**/*.mode1v3
44+
**/ios/**/*.mode2v3
45+
**/ios/**/*.moved-aside
46+
**/ios/**/*.pbxuser
47+
**/ios/**/*.perspectivev3
48+
**/ios/**/*sync/
49+
**/ios/**/.sconsign.dblite
50+
**/ios/**/.tags*
51+
**/ios/**/.vagrant/
52+
**/ios/**/DerivedData/
53+
**/ios/**/Icon?
54+
**/ios/**/Pods/
55+
**/ios/**/.symlinks/
56+
**/ios/**/profile
57+
**/ios/**/xcuserdata
58+
**/ios/.generated/
59+
**/ios/Flutter/App.framework
60+
**/ios/Flutter/Flutter.framework
61+
**/ios/Flutter/Flutter.podspec
62+
**/ios/Flutter/Generated.xcconfig
63+
**/ios/Flutter/ephemeral
64+
**/ios/Flutter/app.flx
65+
**/ios/Flutter/app.zip
66+
**/ios/Flutter/flutter_assets/
67+
**/ios/Flutter/flutter_export_environment.sh
68+
**/ios/ServiceDefinitions.json
69+
**/ios/Runner/GeneratedPluginRegistrant.*
70+
71+
# Exceptions to above rules.
72+
!**/ios/**/default.mode1v3
73+
!**/ios/**/default.mode2v3
74+
!**/ios/**/default.pbxuser
75+
!**/ios/**/default.perspectivev3

Diff for: .metadata

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# This file tracks properties of this Flutter project.
2+
# Used by Flutter tool to assess capabilities and perform upgrades etc.
3+
#
4+
# This file should be version controlled and should not be manually edited.
5+
6+
version:
7+
revision: 18116933e77adc82f80866c928266a5b4f1ed645
8+
channel: stable
9+
10+
project_type: package

Diff for: CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## 0.0.1
2+
3+
* TODO: Describe initial release.

Diff for: LICENSE

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
TODO: Add your license here.

Diff for: README.md

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<p align="center">
2+
3+
<h3 align="center">flutter-jsonschema-form</h3>
4+
5+
<p align="center">
6+
A simple <a href="https://flutter.dev/">Flutter</a> component capable of using <a href="http://json-schema.org/">JSON Schema</a> to declaratively build and customize web forms.
7+
<br />
8+
Inspired by <a href="https://github.com/rjsf-team/react-jsonschema-form">react-jsonschema-form</a>
9+
<br />
10+
</p>
11+
12+
## Installation
13+
14+
Add dependency to pubspec.yaml
15+
16+
```
17+
dependencies:
18+
...
19+
flutter_jsonschema_form:
20+
git:
21+
url: git://github.com/edlose16b/flutter_jsonschema_form.git
22+
ref: main
23+
```
24+
25+
Run in your terminal
26+
27+
```
28+
flutter packages get
29+
```
30+
31+
See the [File Picker Installation](https://github.com/miguelpruivo/plugins_flutter_file_picker) for file fields.
32+
33+
## Usage
34+
35+
```dart
36+
import 'package:flutter_jsonschema_form/flutter_jsonschema_form.dart';
37+
38+
39+
final jsonSchema = {
40+
"title": "A registration form",
41+
"description": "A simple form example.",
42+
"type": "object",
43+
"required": [
44+
"firstName",
45+
"lastName"
46+
],
47+
"properties": {
48+
"firstName": {
49+
"type": "string",
50+
"title": "First name",
51+
"default": "Chuck"
52+
},
53+
"lastName": {
54+
"type": "string",
55+
"title": "Last name"
56+
},
57+
"telephone": {
58+
"type": "string",
59+
"title": "Telephone",
60+
"minLength": 10
61+
}
62+
}
63+
}
64+
65+
66+
@override
67+
Widget build(BuildContext context) {
68+
return Scaffold(
69+
body: JsonForm(
70+
jsonSchema: jsonSchema,
71+
onFormDataSaved: (data) {
72+
inspect(data);
73+
},
74+
),
75+
);
76+
}
77+
```
78+
79+
## Additional information
80+
81+
WIP

Diff for: analysis_options.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
include: package:flutter_lints/flutter.yaml
2+
3+
# Additional information about this file can be found at
4+
# https://dart.dev/guides/language/analysis-options

Diff for: lib/flutter_jsonschema_form.dart

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
library flutter_jsonschema_form;
2+
3+
export './src/widget_builder.dart';

Diff for: lib/src/fields/checkbox_form_field.dart

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import 'package:flutter/material.dart';
2+
import '../models/models.dart';
3+
4+
class CheckboxJFormField extends StatefulWidget {
5+
const CheckboxJFormField({
6+
Key? key,
7+
required this.property,
8+
required this.onSaved,
9+
}) : super(key: key);
10+
11+
final SchemaProperty property;
12+
final void Function(bool?)? onSaved;
13+
14+
@override
15+
_CheckboxJFormFieldState createState() => _CheckboxJFormFieldState();
16+
}
17+
18+
class _CheckboxJFormFieldState extends State<CheckboxJFormField> {
19+
@override
20+
Widget build(BuildContext context) {
21+
return FormField<bool>(
22+
initialValue: widget.property.defaultValue ?? false,
23+
autovalidateMode: AutovalidateMode.onUserInteraction,
24+
validator: (value) {
25+
if (widget.property.required) {
26+
if (value != null && !value) {
27+
return "Required";
28+
}
29+
}
30+
return null;
31+
},
32+
onSaved: (newValue) {
33+
widget.onSaved!(newValue);
34+
},
35+
builder: (field) {
36+
return CheckboxListTile(
37+
value: field.value,
38+
title: Text(widget.property.title),
39+
subtitle: const Text(
40+
'Required',
41+
style: TextStyle(color: Color(0xFFd32f2f)),
42+
),
43+
controlAffinity: ListTileControlAffinity.leading,
44+
onChanged: (bool? value) {
45+
field.didChange(value);
46+
},
47+
);
48+
},
49+
);
50+
}
51+
}

Diff for: lib/src/fields/date_form_field.dart

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:intl/intl.dart';
3+
import 'package:extended_masked_text/extended_masked_text.dart';
4+
5+
import '../models/models.dart';
6+
import '..//utils/date_text_input_json_formatter.dart';
7+
8+
class DateJFormField extends StatefulWidget {
9+
const DateJFormField({
10+
Key? key,
11+
required this.property,
12+
required this.onSaved,
13+
}) : super(key: key);
14+
15+
final SchemaProperty property;
16+
final void Function(DateTime?)? onSaved;
17+
18+
@override
19+
_DateJFormFieldState createState() => _DateJFormFieldState();
20+
}
21+
22+
class _DateJFormFieldState extends State<DateJFormField> {
23+
final txtDateCtrl = MaskedTextController(mask: '00-00-0000');
24+
final formatter = DateFormat('dd-MM-yyyy');
25+
26+
@override
27+
Widget build(BuildContext context) {
28+
return Column(
29+
children: [
30+
TextFormField(
31+
controller: txtDateCtrl,
32+
keyboardType: TextInputType.phone,
33+
validator: (value) {
34+
if (widget.property.required && (value == null || value.isEmpty)) {
35+
return 'Required';
36+
}
37+
},
38+
inputFormatters: [DateTextInputJsonFormatter()],
39+
decoration: InputDecoration(
40+
hintText: 'DD-MM-YYYY',
41+
labelText: widget.property.required
42+
? widget.property.title + ' *'
43+
: widget.property.title,
44+
helperText: widget.property.help,
45+
suffixIcon: IconButton(
46+
icon: const Icon(Icons.date_range_outlined),
47+
onPressed: () async {
48+
final date = await showDatePicker(
49+
context: context,
50+
initialDate: DateTime.now(),
51+
firstDate: DateTime(1998),
52+
lastDate: DateTime.now(),
53+
);
54+
if (date != null) txtDateCtrl.text = formatter.format(date);
55+
56+
widget.onSaved!(date);
57+
},
58+
),
59+
),
60+
),
61+
],
62+
);
63+
}
64+
}

Diff for: lib/src/fields/dropdown_form_field.dart

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import 'package:flutter/material.dart';
2+
import '../models/models.dart';
3+
4+
class DropDownJFormField extends StatefulWidget {
5+
const DropDownJFormField({
6+
Key? key,
7+
required this.property,
8+
required this.onSaved,
9+
}) : super(key: key);
10+
11+
final SchemaProperty property;
12+
final void Function(dynamic)? onSaved;
13+
14+
@override
15+
_DropDownJFormFieldState createState() => _DropDownJFormFieldState();
16+
}
17+
18+
class _DropDownJFormFieldState extends State<DropDownJFormField> {
19+
@override
20+
Widget build(BuildContext context) {
21+
assert(widget.property.enumm != null, 'enum is required');
22+
assert(() {
23+
if (widget.property.enumNames != null) {
24+
return widget.property.enumNames!.length ==
25+
widget.property.enumm!.length;
26+
}
27+
return true;
28+
}(), '[enumNames] and [enum] must be the same size ');
29+
30+
return Column(
31+
crossAxisAlignment: CrossAxisAlignment.start,
32+
children: [
33+
Text(
34+
widget.property.required
35+
? widget.property.title + ' *'
36+
: widget.property.title,
37+
style: Theme.of(context).textTheme.caption,
38+
),
39+
DropdownButtonFormField<dynamic>(
40+
value: widget.property.defaultValue,
41+
autovalidateMode: AutovalidateMode.onUserInteraction,
42+
hint: const Text('Seleccione'),
43+
isExpanded: false,
44+
validator: (value) {
45+
if (widget.property.required && value == null) {
46+
return 'required';
47+
}
48+
},
49+
items: _buildItems(),
50+
onChanged: (value) {},
51+
onSaved: widget.onSaved,
52+
),
53+
const SizedBox(height: 15),
54+
],
55+
);
56+
}
57+
58+
List<DropdownMenuItem> _buildItems() {
59+
final w = <DropdownMenuItem>[];
60+
for (var i = 0; i < widget.property.enumm!.length; i++) {
61+
final value = widget.property.enumm![i];
62+
final text = widget.property.enumNames?[i] ?? value;
63+
w.add(
64+
DropdownMenuItem(
65+
child: Text(text.toString()),
66+
value: value,
67+
),
68+
);
69+
}
70+
return w;
71+
}
72+
}

Diff for: lib/src/fields/fields.dart

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export 'checkbox_form_field.dart';
2+
export 'date_form_field.dart';
3+
export 'dropdown_form_field.dart';
4+
export 'file_form_field.dart';
5+
export 'number_form_field.dart';
6+
export 'text_form_field.dart';

0 commit comments

Comments
 (0)